public class TelnetSsh extends Telnet
Ssh class to interact with a remote shell without port forwarding thru
the Telnet service. Use of this class requires that the Telnet service be enabled on the remote host.
Example Usage:
public class TelnetExample extends TelnetAdapter {
private TelnetSsh telnet = null;
private OutputStream output = null;
private static BufferedReader reader = null;
private boolean connected = false;
private String hostname = "10.0.0.1";
private String sshHostname = "10.0.0.1";
private String sshUsername = "jsmith";
private String sshPassword = "secret";
private SshParameters sshParams = null;
public TelnetExample() throws IOException, TelnetException {
// create SSH connection parameters
sshParams = new SshParameters(sshHostname,sshUsername,sshPassword);
// create new TelnetSsh instance
telnet = new TelnetSsh(sshParams,hostname);
// register this class as TelnetListener
telnet.addTelnetListener(this);
// establish Telnet connection
telnet.connect();
connected = true;
// get output stream
output = telnet.getOutputStream();
// sends all data entered at console to Telnet server
String input = null;
while ((input = reader.readLine()) != null) {
if (connected) {
((TelnetOutputStream) output).println(input);
} else {
break;
}
}
}
// Invoked when Telnet socked is connected.
public void connected(TelnetConnectedEvent event) {
System.out.println("Connected");
}
// Invoked when Telnet socket is disconnected. Disconnect can
public void disconnected(TelnetDisconnectedEvent event) {
connected = false;
System.out.print("Disconnected. Press enter key to quit.");
}
// Invoked when Telnet server requests that the Telnet client begin performing specified TelnetOption.
public void doOption(DoOptionEvent event) {
// refuse any options requested by Telnet server
telnet.sendWontOption(event.getOption());
}
// Invoked when Telnet server offers to begin performing specified TelnetOption.
public void willOption(WillOptionEvent event) {
// refuse any options offered by Telnet server
telnet.sendDontOption(event.getOption());
}
// Invoked when data is received from Telnet server.
public void dataReceived(TelnetDataReceivedEvent event) {
// print data recevied from Telnet server to console
System.out.print(event.getData());
}
// starts console program
public static void main(String[] args) {
try {
// create BufferedReader to read data from console
reader = new BufferedReader(new InputStreamReader(System.in));
// create new TelnetExample instance
TelnetExample example = new TelnetExample();
} catch (Exception e) {
e.printStackTrace(System.out);
}
}
}
Ssh,
Serialized FormTelnet.ByteDecoder, Telnet.DefaultDecoder, Telnet.EUCKoreanDecoder, Telnet.UTF8Decoder| Constructor and Description |
|---|
TelnetSsh(SshParameters sshParameters,
java.lang.String hostname)
Creates a new TelnetSsh instance.
|
| Modifier and Type | Method and Description |
|---|---|
void |
connect()
Establishes connection with SSH server and creates secure tunnel to Telnet server.
|
void |
disconnect()
Closes tunnel to Telnet server and closes connection with SSH server.
|
SshParameters |
getSshParameters()
Gets SSH parameters used in establishing connection with SSH server.
|
void |
setSshParameters(SshParameters parameters)
Sets SSH parameters used in establishing connection with SSH server.
|
addTelnetListener, clearProxySettings, getCharacterSet, getCommandName, getDebug, getDebugStream, getHostname, getInputStream, getOutputStream, getPort, getReader, getReadingTimeout, getTimeout, getWriter, removeTelnetListener, sendCommand, sendCommand, sendDontOption, sendDoOption, sendOptionSubnegotiation, sendWillOption, sendWontOption, setCharacterSet, setDebug, setDebugStream, setHostname, setPort, setProxyAuthentication, setProxyHost, setProxyType, setReadingTimeout, setTimeoutpublic TelnetSsh(SshParameters sshParameters, java.lang.String hostname)
hostname - sshParameters - public void connect()
throws TelnetException
connect in class TelnetTelnetException - if an I/O or Telnet related error occurspublic void disconnect()
disconnect in class Telnetpublic SshParameters getSshParameters()
SshParameterspublic void setSshParameters(SshParameters parameters)
parameters - the SSH parametersSshParametersCopyright © JSCAPE LLC. 1999-2021. All Rights Reserved