Package com.sun.grid.util.expect
Class Expect
- java.lang.Object
-
- com.sun.grid.util.expect.Expect
-
public class Expect extends java.lang.Object
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"); }
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
add(ExpectHandler handler)
Add a new expect handler.java.util.List
command()
java.util.List
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(java.lang.Object obj)
print an object to stdinvoid
print(java.lang.String s)
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
println(java.lang.Object x)
print an object to stdin (with linefeed)void
println(java.lang.String x)
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
-
-
-
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.InterruptedException
Execute 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 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.
-
-