ExpectJ Adventure
This is my script
echo "Name:"
read name
if [ "$name" == "abcd" ]; then
echo "correct username"
echo "Password:"
read password
if [ "$password" == "pwd" ]; then
echo "Hello"
else
echo "Wrong password"
fi
else
echo "wrong username"
fi
=================================================================================
This is my Java code
import java.io.IOException;
import java.util.*;
import expectj.*;
public class Trial {
public static void main(String[] args) {
ExpectJ exp = new ExpectJ();
String command = "sh /root/Desktop/hello.sh";
Spawn s = null;
try {
s = exp.spawn(command);
s.expect("Name:");
s.send("abcd\n");
System.out.println("Current status: "+s.getCurrentStandardOutContents());
s.expect("correct username");
s.expect("Password:");
s.send("pwd\n");
s.expect("Hello");
System.out.println("final output: " + s.getCurrentStandardOutContents());
System.out.println("Possible errors: " + s.getCurrentStandardErrContents());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("ioe\n");
} catch (TimeoutException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("toe\n");
} finally {
if (s != null)
开发者_StackOverflows.stop();
}
}
}
============================================================================
And this is my OUTPUT
Name:
Current status: Name:
correct username
Password:
============================================================================
Its not proceeding further.Its not terminating either.. I dunno why..
Does it work when you comment this line:
System.out.println("Current status: "+s.getCurrentStandardOutContents());
Maybe the application is "expecting" the value "correct username"
but sees "Current status: Name:
" instead (the output from your "debug" line). Not an expert on jExpert, but if the tool just redirects and monitors the System.out
, it will see the script output as well as everything you print to the console.
I get it..2 consecutive s.expect() statements can never work..So to get rid of it, a \n could be added..
In this case
s.expect("correct username");
s.expect("Password:");
is bound not to work.So, it should be replaced by-
s.expect("correct username\nPassword:");//This will work
精彩评论