public class Expect
extends java.lang.Object
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 and Description |
|---|
Expect() |
Expect(java.lang.String[] cmd)
Creates a new instance of Expect
|
| Modifier and Type | Method and 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 stdin
|
void |
print(boolean b)
print a boolean to stdin
|
void |
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 of
s
is written to the logger. |
void |
print(double d)
print a double to stdin
|
void |
print(float x)
print a float to stdin.
|
void |
print(int i)
print an integer to stdin
|
void |
print(long l)
print a long to stdin
|
void |
print(java.lang.Object obj)
print an object to stdin
|
void |
print(java.lang.String s)
print a string to stdin
|
void |
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 of
x
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
|
public Expect()
public Expect(java.lang.String[] cmd)
cmd - the command which will be executedRuntime.exec(String[])public java.util.List command()
public java.util.List env()
public void add(ExpectHandler handler)
handler - the expect handlerpublic int exec(long timeout)
throws java.io.IOException,
java.lang.InterruptedException
timeout - timout for the command in millisecondsjava.io.IOException - if the execution of the command failedjava.lang.InterruptedException - if the current thread has been interrupted
before the command has been finished.public void flush()
public void println(java.lang.String x)
x - the stringpublic void print(java.lang.String s)
s - the stringpublic void println(java.lang.Object x)
x - the objectpublic void print(java.lang.Object obj)
obj - the objectpublic void println(int x)
x - the integerpublic void print(int i)
i - the integerpublic void print(boolean b)
b - the booleanpublic void println(boolean x)
x - the booleanpublic void println(long x)
x - the longpublic void print(long l)
l - the longpublic void println(double x)
x - the doublepublic void println(char[] x)
x
is written to the logger.x - the char arraypublic void print(char[] s)
s
is written to the logger.s - the char arraypublic void printPassword(char[] s)
s - the passwordpublic void printlnPassword(char[] s)
s - the passwordpublic void print(double d)
d - the doublepublic void println(char x)
x - the charpublic void print(char c)
c - the charpublic void println(float x)
x - the floatpublic void print(float x)
x - the floatpublic void println()