问题
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