Package com.sun.grid.util.expect
Class Expect
- java.lang.Object
-
- com.sun.grid.util.expect.Expect
-
public class Expect extends java.lang.ObjectSimple 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"); }
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidadd(ExpectHandler handler)Add a new expect handler.java.util.Listcommand()java.util.Listenv()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(java.lang.Object obj)print an object to stdinvoidprint(java.lang.String s)print 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)voidprintln(java.lang.Object x)print an object to stdin (with linefeed)voidprintln(java.lang.String x)print 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
-
-
-
Method Detail
-
command
public java.util.List command()
-
env
public java.util.List env()
-
add
public void add(ExpectHandler handler)
Add a new expect handler.- Parameters:
handler- the expect handler
-
exec
public int exec(long timeout) throws java.io.IOException, java.lang.InterruptedExceptionExecute the command and call the registered handlers.- Parameters:
timeout- timout for the command in milliseconds- Returns:
- the exit code of the command
- Throws:
java.io.IOException- if the execution of the command failedjava.lang.InterruptedException- if the current thread has been interrupted before the command has been finished.
-
flush
public void flush()
Flush stdin
-
println
public void println(java.lang.String x)
print a string to stdin (with line separator)- Parameters:
x- the string
-
print
public void print(java.lang.String s)
print a string to stdin- Parameters:
s- the string
-
println
public void println(java.lang.Object x)
print an object to stdin (with linefeed)- Parameters:
x- the object
-
print
public void print(java.lang.Object obj)
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.
-
-