ExpectIt : Trouble implementing a sudo -i

北战南征 提交于 2019-12-04 05:03:18

问题


I'm creating a web shell client and successfully created a simple terminal.

I can do basic commands, but I need to do a sudo -i and pass a password.

After send the "sudo -i" command, I "expect" the new user (as root) prompt, but the "expect" waits forever. I can see the prompt is as expected.

This code is only for the 'sudo' opperation and works fine. Common commands are made at separated method.

expect.sendLine( "sudo -i" );
expect.expect( Matchers.contains( ":" ) );
expect.sendLine( password );

// Old prompt was user prompt :  user + "@"
// Now I need the root prompt :  user + ":"
PROMPT = user + ":";

Common commands ( run( String command) method ): This will block at expect.expect() ONLY IF I DO A SUDO ( if I try this BEFORE sudo, all works fine too ) ...

expect.sendLine( command );
String result = expect.expect( Matchers.contains( PROMPT ) ).getInput();

The error (expecting the sudo prompt):

net.sf.expectit.ExpectIOException: Expect operation fails (timeout: 30000 ms) for matcher: contains('sadlog:')

回答1:


expect.sendLine( command );
String result = expect.expect( Matchers.contains(":")).getInput();
int pos = result.indexOf(PROMPT);

if (pos > -1) {
    // SUCCESS
} else {
    // FAILURE
}


来源:https://stackoverflow.com/questions/31477489/expectit-trouble-implementing-a-sudo-i

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!