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 TypeMethodDescriptionvoid
add
(ExpectHandler handler) Add a new expect handler.command()
env()
int
exec
(long timeout) Execute the command and call the registered handlers.void
flush()
Flush stdinvoid
print
(boolean b) print a boolean to stdinvoid
print
(char c) print a char to stdin.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 ofs
is written to the logger.void
print
(double d) print a double to stdinvoid
print
(float x) print a float to stdin.void
print
(int i) print an integer to stdinvoid
print
(long l) print a long to stdinvoid
print an object to stdinvoid
print a string to stdinvoid
println()
print linefeed to stdin.void
println
(boolean x) print a boolean to stdin (with linefeed)void
println
(char x) print a char to stdin.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 ofx
is written to the logger.void
println
(double x) print a double to stdin (with linefeed)void
println
(float x) print a float to stdin (with linefeed).void
println
(int x) print an integer to stdin (with linefeed)void
println
(long x) print a long to stdin (with linefeed)void
print an object to stdin (with linefeed)void
print a string to stdin (with line separator)void
printlnPassword
(char[] s) print a password to stdin (with linefeed)void
printPassword
(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 ofx
is 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 ofs
is 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.
-