    All Packages  Class Hierarchy  This Package  Previous  Next  Index

# Class socket.TelnetWrapper

    java.lang.Object
       |
       +----socket.TelnetWrapper

public class TelnetWrapper
extends Object

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();
    }

Version:
  0.2 5/15/97 - added comments, replaced String += with StringBuffer.append() in receiveUntil(), added port constructor
Author:
  George Ruban 3/4/97
See Also:
  TelnetIO

##

debug
  Set to true for System.out.println debugging.

##

TelnetWrapper(String)
  Connects to the default telnet port on the given host.
TelnetWrapper(String, int)
  Connects to a specific telnet port on the given host.

##

available()
  Returns bytes available to be read.
disconnect()
  Ends the telnet connection.
finalize()
  Ends the telnet connection.
login(String, String)
  Logs in as a particular user and password.
main(String[])
  Telnet test driver.
receive()
  Returns a String from the telnet connection.
receiveBytes()
  Returns a byte array.
receiveUntil(String)
  Returns all data received up until a certain token.
receiveUntil(String, long)
  Returns all data received up until a certain token.
send(byte[])
  Sends bytes over the telnet connection.
send(String)
  Sends a String to the remote host.
sendLine(String)
  Sends a line to the remote host, returns all data before the prompt.
setDefaultPrompt(String)
  Sets the default prompt used by all TelnetWrappers.
setLogin(String, String)
  Sets the default login used by TelnetWrappers.
setPrompt(String)
  Sets the expected prompt.
unsetLogin()
  Turns off the default login of TelnetWrappers.
wait(String)
  Skip any received data until the token appears.
wait(String, long)
  Wait for a String or a timeout.

##

debug

      public boolean debug

  Set to true for System.out.println debugging.

##

TelnetWrapper

      public TelnetWrapper(String host) throws IOException

  Connects to the default telnet port on the given host. If the defaultLogin and defaultPassword are non-null, attempts login.

TelnetWrapper

      public TelnetWrapper(String host,
                           int port) throws IOException

  Connects to a specific telnet port on the given host. If the defaultLogin and defaultPassword are non-null, attempts login.

##

wait

      public void wait(String token) throws IOException

  Skip any received data until the token appears. 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.
  Parameters:
    token - String to wait for
  Throws: IOException
    on problems with the socket connection
  See Also:
    receiveUntil

wait

      public void wait(String token,
                       long timeout) throws IOException, TimedOutException

  Wait for a String or a timeout. If time runs out, throws a TimedOutException. Sleeps in intervals of 100 milliseconds until either receiving the token or timeout.

  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.

  Parameters:
    token - String to wait for
    timeout - time in milliseconds to wait (negative means wait forever)
  Throws: IOException
    on problems with the socket connection
  Throws: TimedOutException
    if time runs out before token received
  See Also:
    receiveUntil

available

      public int available() throws IOException

  Returns bytes available to be read. Since they haven't been negotiated over, this could be misleading...

receive

      public String receive() throws IOException

  Returns a String from the telnet connection. Blocks until one is available. No guarantees that the string is in any way complete. NOTE: uses Java 1.0.2 style String-bytes conversion.

receiveBytes

      public byte[] receiveBytes() throws IOException

  Returns a byte array. Blocks until data is available.

receiveUntil

      public String receiveUntil(String token) throws IOException

  Returns all data received up until a certain token.
  Parameters:
    token - String to wait for
  Throws: IOException
    on problems with the socket connection
  See Also:
    wait

receiveUntil

      public String receiveUntil(String token,
                                 long timeout) throws IOException, TimedOutException

  Returns all data received up until a certain token.
  Parameters:
    token - String to wait for
    timeout - time in milliseconds to wait (negative means wait forever)
  Throws: IOException
    on problems with the socket connection
  Throws: TimedOutException
    if time runs out before token received
  See Also:
    wait

send

      public void send(String s) throws IOException

  Sends a String to the remote host. NOTE: uses Java 1.0.2 style String-bytes conversion.
  Throws: IOException
    on problems with the socket connection

sendLine

      public String sendLine(String command) throws IOException

  Sends a line to the remote host, returns all data before the prompt. Since telnet seems to rely on carriage returns ('\r'), one will be appended to the sent string, if necessary.
  Parameters:
    command - command line to send
  Returns:
    whatever data the command produced before the prompt.
  See Also:
    setPrompt

send

      public void send(byte buf[]) throws IOException

  Sends bytes over the telnet connection.

login

      public void login(String loginName,
                        String password) throws IOException

  Logs in as a particular user and password. Returns after receiving prompt.

setPrompt

      public void setPrompt(String prompt)

  Sets the expected prompt. If this function is not explicitly called, the default prompt is used.
  See Also:
    setDefaultPrompt

setDefaultPrompt

      public static void setDefaultPrompt(String prompt)

  Sets the default prompt used by all TelnetWrappers. This can be specifically overridden for a specific instance. The default prompt starts out as "$ " until this function is called.
  See Also:
    setPrompt

setLogin

      public static void setLogin(String login,
                                  String password)

  Sets the default login used by TelnetWrappers. If this method is called with non-null login and password, all TelnetWrappers will attempt to login when first created.
  Parameters:
    login - login name to use
    password - password to use
  See Also:
    login, unsetLogin

unsetLogin

      public static void unsetLogin()

  Turns off the default login of TelnetWrappers. After this method is called, TelnetWrappers will not login until that method is explicitly called.
  See Also:
    setLogin, login

disconnect

      public void disconnect() throws IOException

  Ends the telnet connection.

finalize

      public void finalize()

  Ends the telnet connection.
  Overrides:
    finalize in class Object

main

      public static void main(String args[]) throws IOException

  Telnet test driver. Modeled after the IOtest.java example in the Telnet Applet. Logs in to "host", creates a timestamped file in /tmp, lists the /tmp directory to System.out, disconnects. Shows off several TelnetWrapper methods.
  Parameters:
    args - host login password prompt

    All Packages  Class Hierarchy  This Package  Previous  Next  Index