Package com.sun.grid.util.expect
Class Expect
java.lang.Object
com.sun.grid.util.expect.Expect
Simple helper class for handling output of a process. It works similar as
the tcl expect command.
Example
char [] password = null;
String username = null;
long timeout = 60 * 1000;
String [] cmd = new String[] { "su", "username", "-c" , "ls" };
Expect expect = new Expect(cmd);
expect.addHandler(new ExpectPasswordHandler("password: ", password));
expect.addHandler(new ExpectHandler() {
// print every line after the password question to System.out
public void handle(Expect expect, ExpectBuffer buffer) {
String line = buffer.consumeLine();
if(line != null) {
System.out.println(line);
}
}
});
if(expect.exec(timeout) == 0 ) {
System.out.println("ls as user " + username + " successfully executed");
}
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidadd(ExpectHandler handler) Add a new expect handler.command()env()intexec(long timeout) Execute the command and call the registered handlers.voidflush()Flush stdinvoidprint(boolean b) print a boolean to stdinvoidprint(char c) print a char to stdin.voidprint(char[] s) print a char arrary to stdin !!!Attention!!!: For security reasons this method should not be used to send passwords to the process, sice the content ofsis written to the logger.voidprint(double d) print a double to stdinvoidprint(float x) print a float to stdin.voidprint(int i) print an integer to stdinvoidprint(long l) print a long to stdinvoidprint an object to stdinvoidprint a string to stdinvoidprintln()print linefeed to stdin.voidprintln(boolean x) print a boolean to stdin (with linefeed)voidprintln(char x) print a char to stdin.voidprintln(char[] x) print a char arrary to stdin (with line feed) !!!Attention!!!: For security reasons this method should not be used to send passwords to the process, sice the content ofxis written to the logger.voidprintln(double x) print a double to stdin (with linefeed)voidprintln(float x) print a float to stdin (with linefeed).voidprintln(int x) print an integer to stdin (with linefeed)voidprintln(long x) print a long to stdin (with linefeed)voidprint an object to stdin (with linefeed)voidprint a string to stdin (with line separator)voidprintlnPassword(char[] s) print a password to stdin (with linefeed)voidprintPassword(char[] s) print a password to stdin
-
Constructor Details
-
Expect
public Expect() -
Expect
Creates a new instance of Expect- Parameters:
cmd- the command which will be executed- See Also:
-
-
Method Details
-
command
-
env
-
add
Add a new expect handler.- Parameters:
handler- the expect handler
-
exec
Execute the command and call the registered handlers.- Parameters:
timeout- timout for the command in milliseconds- Returns:
- the exit code of the command
- Throws:
IOException- if the execution of the command failedInterruptedException- if the current thread has been interrupted before the command has been finished.
-
flush
public void flush()Flush stdin -
println
print a string to stdin (with line separator)- Parameters:
x- the string
-
print
print a string to stdin- Parameters:
s- the string
-
println
print an object to stdin (with linefeed)- Parameters:
x- the object
-
print
print an object to stdin- Parameters:
obj- the object
-
println
public void println(int x) print an integer to stdin (with linefeed)- Parameters:
x- the integer
-
print
public void print(int i) print an integer to stdin- Parameters:
i- the integer
-
print
public void print(boolean b) print a boolean to stdin- Parameters:
b- the boolean
-
println
public void println(boolean x) print a boolean to stdin (with linefeed)- Parameters:
x- the boolean
-
println
public void println(long x) print a long to stdin (with linefeed)- Parameters:
x- the long
-
print
public void print(long l) print a long to stdin- Parameters:
l- the long
-
println
public void println(double x) print a double to stdin (with linefeed)- Parameters:
x- the double
-
println
public void println(char[] x) print a char arrary to stdin (with line feed) !!!Attention!!!: For security reasons this method should not be used to send passwords to the process, sice the content ofxis written to the logger.- Parameters:
x- the char array
-
print
public void print(char[] s) print a char arrary to stdin !!!Attention!!!: For security reasons this method should not be used to send passwords to the process, sice the content ofsis written to the logger.- Parameters:
s- the char array
-
printPassword
public void printPassword(char[] s) print a password to stdin- Parameters:
s- the password
-
printlnPassword
public void printlnPassword(char[] s) print a password to stdin (with linefeed)- Parameters:
s- the password
-
print
public void print(double d) print a double to stdin- Parameters:
d- the double
-
println
public void println(char x) print a char to stdin.- Parameters:
x- the char
-
print
public void print(char c) print a char to stdin.- Parameters:
c- the char
-
println
public void println(float x) print a float to stdin (with linefeed).- Parameters:
x- the float
-
print
public void print(float x) print a float to stdin.- Parameters:
x- the float
-
println
public void println()print linefeed to stdin.
-