de.mud.telnet
Class TelnetWrapper

java.lang.Object
  |
  +--de.mud.telnet.TelnetProtocolHandler
        |
        +--de.mud.telnet.TelnetWrapper

public class TelnetWrapper
extends TelnetProtocolHandler

The telnet wrapper is a sample class for how to use the telnet protocol handler of the JTA source package. To write a program using the wrapper you may use the following piece of code as an example:

   TelnetWrapper telnet = new TelnetWrapper();
   try {
     telnet.connect(args[0], 23);
     telnet.login("user", "password");
     telnet.setPrompt("user@host");
     telnet.waitfor("Terminal type?");
     telnet.send("dumb");
     System.out.println(telnet.send("ls -l"));
   } catch(java.io.IOException e) {
     e.printStackTrace();
   }
 
Please keep in mind that the password is visible for anyone who can download the class file. So use this only for public accounts or if you are absolutely sure nobody can see the file.

Maintainer: Matthias L. Jugel

Version:
$Id: TelnetWrapper.java,v 1.8 2001/11/04 20:50:29 marcus Exp $
Author:
Matthias L. Jugel, Marcus Meißner

Field Summary
protected  java.lang.String host
           
protected  java.io.InputStream in
           
protected  java.io.OutputStream out
           
protected  int port
           
protected  java.util.Vector script
           
protected  ScriptHandler scriptHandler
           
protected  java.net.Socket socket
           
 
Fields inherited from class de.mud.telnet.TelnetProtocolHandler
ID
 
Constructor Summary
TelnetWrapper()
           
 
Method Summary
 void connect(java.lang.String host, int port)
          Connect the socket and open the connection.
 void disconnect()
          Disconnect the socket and close the connection.
 java.lang.String getTerminalType()
          Get the current terminal type for TTYPE telnet option.
 java.awt.Dimension getWindowSize()
          Get the current window size of the terminal for the NAWS telnet option.
 void login(java.lang.String user, java.lang.String pwd)
          Login into remote host.
 void notifyEndOfRecord()
          sent on IAC EOR (prompt terminator for remote access systems).
 int read(byte[] b)
          Read data from the socket and use telnet negotiation before returning the data read.
 java.lang.String send(java.lang.String cmd)
          Send a command to the remote host.
 void setLocalEcho(boolean echo)
          Set the local echo option of telnet.
 void setPrompt(java.lang.String prompt)
           
 java.lang.String waitfor(java.lang.String match)
           
 java.lang.String waitfor(java.lang.String[] searchElements)
          Wait for a string to come from the remote host and return all that characters that are received until that happens (including the string being waited for).
 void write(byte[] b)
          Write data to the socket.
 
Methods inherited from class de.mud.telnet.TelnetProtocolHandler
inputfeed, negotiate, reset, sendTelnetControl, setCR, setCRLF, startup, transpose
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

scriptHandler

protected ScriptHandler scriptHandler

in

protected java.io.InputStream in

out

protected java.io.OutputStream out

socket

protected java.net.Socket socket

host

protected java.lang.String host

port

protected int port

script

protected java.util.Vector script
Constructor Detail

TelnetWrapper

public TelnetWrapper()
Method Detail

connect

public void connect(java.lang.String host,
                    int port)
             throws java.io.IOException
Connect the socket and open the connection.

disconnect

public void disconnect()
                throws java.io.IOException
Disconnect the socket and close the connection.

notifyEndOfRecord

public void notifyEndOfRecord()
sent on IAC EOR (prompt terminator for remote access systems).
Overrides:
notifyEndOfRecord in class TelnetProtocolHandler

login

public void login(java.lang.String user,
                  java.lang.String pwd)
           throws java.io.IOException
Login into remote host. This is a convenience method and only works if the prompts are "login:" and "Password:".
Parameters:
user - the user name
pwd - the password

setPrompt

public void setPrompt(java.lang.String prompt)

send

public java.lang.String send(java.lang.String cmd)
                      throws java.io.IOException
Send a command to the remote host. A newline is appended and if a prompt is set it will return the resulting data until the prompt is encountered.
Parameters:
cmd - the command
Returns:
output of the command or null if no prompt is set

waitfor

public java.lang.String waitfor(java.lang.String[] searchElements)
                         throws java.io.IOException
Wait for a string to come from the remote host and return all that characters that are received until that happens (including the string being waited for).
Parameters:
match - the string to look for
Returns:
skipped characters

waitfor

public java.lang.String waitfor(java.lang.String match)
                         throws java.io.IOException

read

public int read(byte[] b)
         throws java.io.IOException
Read data from the socket and use telnet negotiation before returning the data read.
Parameters:
b - the input buffer to read in
Returns:
the amount of bytes read

write

public void write(byte[] b)
           throws java.io.IOException
Write data to the socket.
Overrides:
write in class TelnetProtocolHandler
Parameters:
b - the buffer to be written

getTerminalType

public java.lang.String getTerminalType()
Description copied from class: TelnetProtocolHandler
Get the current terminal type for TTYPE telnet option.
Overrides:
getTerminalType in class TelnetProtocolHandler
Following copied from class: de.mud.telnet.TelnetProtocolHandler
Returns:
the string id of the terminal

getWindowSize

public java.awt.Dimension getWindowSize()
Description copied from class: TelnetProtocolHandler
Get the current window size of the terminal for the NAWS telnet option.
Overrides:
getWindowSize in class TelnetProtocolHandler
Following copied from class: de.mud.telnet.TelnetProtocolHandler
Returns:
the size of the terminal as Dimension

setLocalEcho

public void setLocalEcho(boolean echo)
Description copied from class: TelnetProtocolHandler
Set the local echo option of telnet.
Overrides:
setLocalEcho in class TelnetProtocolHandler
Following copied from class: de.mud.telnet.TelnetProtocolHandler
Parameters:
echo - true for local echo, false for no local echo