Using ExpectIt for Exit Value

166 views
Skip to first unread message

rabbi....@gmail.com

unread,
Apr 19, 2016, 11:14:15 AM4/19/16
to Yet another Expect for Java
Hi There,

This is my first time using expectit - and I am trying to write a code in groovy which connects to a remote ssh Session and execute a few commands. I have 2 sets of questions.

1) After Using jsch to create the session, and defining the ExpectBuilder; the script just hangs after the prompt.

channel.connect()
//Should we be using regular expression to get the prompt, could you provide some examples ? And is this necessary before a command is sent ?
expect.expect(contains("[PROMPT]"))
expect.sendLine("pwd")


2) Would like to confirm the the command send via "expect.sendLine" actually executed. Is there a way we can capture the exit value of the command. For example

def exitValue = "mkdir foo".execute().exitValue()
if(!exitValue)
//do something


Your help in this matter is highly appreciated. Thank you

Alexey Gavrilov

unread,
Apr 19, 2016, 5:22:07 PM4/19/16
to rabbi....@gmail.com, Yet another Expect for Java
Hi!

See my answers below.

1) After Using jsch to create the session, and defining the ExpectBuilder; the script just hangs after the prompt.
If the script hangs then the output  does not probably match the prompt.

channel.connect()
//Should we be using regular expression to get the prompt, could you provide some examples ? 
It depends on the actual prompt string which is usually set by the user profile variable [1]. For example, expect.expect(contains(“#”)) or expect.expect(regexp(“bash-.+\$”)) could work for you.

And is this necessary before a command is sent ?
It is not always necessary but it guaranties that the remote server is ready to accept commands. 

expect.expect(contains("[PROMPT]"))
expect.sendLine("pwd”)


2) Would like to confirm the the command send via "expect.sendLine" actually executed. Is there a way we can capture the exit value of the command. For example 

def exitValue = "mkdir foo".execute().exitValue()
if(!exitValue)
//do something 

The only way to confirm that the command has been executed is to await for its output or for the command prompt. You can try something like:

expect.expect(contains(PROMPT));
expect.sendLine("mkdir foo; echo $?”);
int exitValue = Integer.parseInt(expect.expect(contains(PROMPT)).getBefore());

Alternatively, you can consider running JSch in the remote ‘exec’ mode [2] without ExpectIt and retrieve the exit code value from the channel exit status Channell#getExitStatus [3].

Regards,
Alexey

[1]
[2]
[3]


--
You received this message because you are subscribed to the Google Groups "Yet another Expect for Java" group.
To unsubscribe from this group and stop receiving emails from it, send an email to java-expecti...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

ervb.s...@gmail.com

unread,
Jul 11, 2016, 5:48:53 PM7/11/16
to Yet another Expect for Java, rabbi....@gmail.com, Alexey1....@gmail.com
what is this PROMPT, is this PROMPT is constant or method in Java??
Reply all
Reply to author
Forward
0 new messages