|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--socket.TelnetWrapper
Wrapper for a Java Telnet call. To use, make a new TelnetWrapper() with the name or IP address of a host. Then, for most uses, the easiest way is to call setPrompt() with the expected prompt, then call login(), and a sequence of sendLine()'s until you get what you want done.
If you don't know the prompt ahead of time, you have to do a sequence of send() and wait() or receiveUntil() calls. send() sends a string across the telnet connection. Add a '\r' to the end if you want to complete a command. wait() waits for an exact string from the other side of the telnet connection, and returns nothing, receiveUntil() also waits for a string, but returns all the data that it received while waiting, including the string itself. Use this if you want the output from a command. Please note that the telnet connection will usually echo the sent command.
sendLine() is generally better, since it adds the '\r' automatically, waits for the prompt before returning, and returns all data received before the prompt, with the prompt itself cut off the end, and the sent command cut off the beginning. login() and sendLine() are implemented using send(), wait() and receiveUntil(). They can be freely mixed and matched.
Here is a simple example of the use of TelnetWrapper:
// creates a new file in /tmp, lists the directory to prove it done { TelnetWrapper telnet = new TelnetWrapper("123.45.78.90"); // setting the correct prompt ahead of time is very important // if you want to use login and sendLine telnet.setPrompt("$ "); telnet.login("loginname", "password"); // this is how you have to do it otherwise telnet.send("touch /tmp/TELNET_WRAPPER" + "\r"); telnet.wait("$ "); // sendLine 1: adds the \r automatically, 2: waits for the prompt // before returning 3: returns what was printed from the command String ls = telnet.sendLine("ls /tmp"); System.out.println(ls); // clean up telnet.disconnect(); }
TelnetIO
Field Summary | |
boolean |
debug
Set to true for System.out.println debugging. |
Constructor Summary | |
TelnetWrapper(java.lang.String host)
Connects to the default telnet port on the given host. |
|
TelnetWrapper(java.lang.String host,
int port)
Connects to a specific telnet port on the given host. |
Method Summary | |
int |
available()
Returns bytes available to be read. |
void |
disconnect()
Ends the telnet connection. |
void |
finalize()
Ends the telnet connection. |
void |
login(java.lang.String loginName,
java.lang.String password)
Logs in as a particular user and password. |
static void |
main(java.lang.String[] args)
Telnet test driver. |
java.lang.String |
receive()
Returns a String from the telnet connection. |
byte[] |
receiveBytes()
Returns a byte array. |
java.lang.String |
receiveUntil(java.lang.String token)
Returns all data received up until a certain token. |
java.lang.String |
receiveUntil(java.lang.String token,
long timeout)
Returns all data received up until a certain token. |
void |
send(byte[] buf)
Sends bytes over the telnet connection. |
void |
send(java.lang.String s)
Sends a String to the remote host. |
java.lang.String |
sendLine(java.lang.String command)
Sends a line to the remote host, returns all data before the prompt. |
static void |
setDefaultPrompt(java.lang.String prompt)
Sets the default prompt used by all TelnetWrappers. |
static void |
setLogin(java.lang.String login,
java.lang.String password)
Sets the default login used by TelnetWrappers. |
void |
setPrompt(java.lang.String prompt)
Sets the expected prompt. |
static void |
unsetLogin()
Turns off the default login of TelnetWrappers. |
void |
wait(java.lang.String token)
Skip any received data until the token appears. |
void |
wait(java.lang.String token,
long timeout)
Wait for a String or a timeout. |
Methods inherited from class java.lang.Object |
clone,
equals,
getClass,
hashCode,
notify,
notifyAll,
toString,
wait,
wait,
wait |
Field Detail |
public boolean debug
Constructor Detail |
public TelnetWrapper(java.lang.String host) throws java.io.IOException
public TelnetWrapper(java.lang.String host, int port) throws java.io.IOException
Method Detail |
public void wait(java.lang.String token) throws java.io.IOException
token
- String to wait forreceiveUntil(java.lang.String)
public void wait(java.lang.String token, long timeout) throws java.io.IOException, TimedOutException
More efficient than receiveUntil, but liable to fail on large tokens that can be spread over several "send"s. In that case, consider using receiveUntil and ignoring the return value.
token
- String to wait fortimeout
- time in milliseconds to wait (negative means wait forever)receiveUntil(String, long)
public int available() throws java.io.IOException
public java.lang.String receive() throws java.io.IOException
public byte[] receiveBytes() throws java.io.IOException
public java.lang.String receiveUntil(java.lang.String token) throws java.io.IOException
token
- String to wait forwait(java.lang.String)
public java.lang.String receiveUntil(java.lang.String token, long timeout) throws java.io.IOException, TimedOutException
token
- String to wait fortimeout
- time in milliseconds to wait (negative means wait forever)wait(String, long)
public void send(java.lang.String s) throws java.io.IOException
public java.lang.String sendLine(java.lang.String command) throws java.io.IOException
command
- command line to sendsetPrompt(java.lang.String)
public void send(byte[] buf) throws java.io.IOException
public void login(java.lang.String loginName, java.lang.String password) throws java.io.IOException
public void setPrompt(java.lang.String prompt)
setDefaultPrompt(java.lang.String)
public static void setDefaultPrompt(java.lang.String prompt)
setPrompt(java.lang.String)
public static void setLogin(java.lang.String login, java.lang.String password)
login
- login name to usepassword
- password to uselogin(java.lang.String, java.lang.String)
,
unsetLogin()
public static void unsetLogin()
setLogin(java.lang.String, java.lang.String)
,
login(java.lang.String, java.lang.String)
public void disconnect() throws java.io.IOException
public void finalize()
public static void main(java.lang.String[] args) throws java.io.IOException
args
- host login password prompt
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |