1

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

enter image description here

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

enter image description here

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.

enter image description here

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:')
Alexey Gavrilov
  • 10,593
  • 2
  • 38
  • 48
Magno C
  • 1,922
  • 4
  • 28
  • 53
  • My fault. It somehow starts to work... I'm sure I changed nothing! Sorry guys. – Magno C Jul 17 '15 at 14:00
  • Hi @Magno-c , I am also working on creating a web ssh terminal using sshj and expectit but not able to send the expectit command outputs to my frontend. Can you help me in understanding how to send expectit out to a variable ? or if you can share an end to end input and output example with me. – Abhimanyu Garg Sep 10 '18 at 17:05
  • Hi I have not much time now to explain but you can start here for now. https://github.com/icemagno/sagitarii/blob/master/src/main/java/br/cefetrj/sagitarii/action/NodeSSHTerminalAction.java – Magno C Sep 14 '18 at 10:22

1 Answers1

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

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