Creating an interactive SSH session |
To create an interactive SSH session you must first understand how to establish a connection, send data to the SSH server and receive data from the SSH server. Each of these is covered in the sections titled Establishing a connection , Receiving data and Sending data respectively.
This section will demonstrate how to create a very basic interactive SSH client that can be run from the console. The source code for this example may be found in the examples/ssh/ssh directory of your distribution.
1. Starting SSH client
To start the interactive SSH client example from the console the SshExample#main method is invoked.
/** * Main method for SshExample * @param args */ public static void main(String[] args) { SshExample test = new SshExample(); }
2. Establishing a connection and listening for events
Upon invoking the SshExample constructor a Ssh instance is created and the SshExample class is registered to handle any events fired by the Ssh instance. Next, a connection to the SSH server is established by invoking the Ssh#connect method and a java.io.OutputStream is retrieved by invoking the Ssh#getOutputStream method.
Next, a while loop is created that constantly reads data entered by the user at the console. This data is then sent to the SSH server using the SshOutputStream#println method.
Example
/** * Creates a new SshExample instance. * */ public SshExample() { String hostname = null; String username = null; String password = null; Ssh ssh = null;
try { BufferedReader bin = new BufferedReader(new InputStreamReader(System.in)); System.out.print("Enter SSH hostname: "); hostname = bin.readLine();
System.out.print("Enter SSH username: "); username = bin.readLine();
System.out.print("Enter SSH password: "); password = bin.readLine();
// create new Ssh instance ssh = new Ssh(hostname, username, password);
// register to capture events ssh.addSshListener(this);
System.out.println("Connecting please wait...");
// connect ssh.connect();
// get output stream for writing data to SSH server OutputStream out = ssh.getOutputStream();
// holds line entered at console String line = null;
// read data from console while (connected && (line = bin.readLine()) != null) { // send line with CRLF to SSH server line += "\r\n"; try { out.write(line.getBytes()); out.flush(); } catch(Exception ioe){ connected = false; } } } catch (Exception e) { e.printStackTrace(); } finally { try { if(connected) { ssh.disconnect(); } } catch(Exception e) {
} } }
3. Capturing events
Several events are fired during the SSH session. To capture and process these events the SshExample class implements the SshListener class and overloads the event handling methods it is interested in. For more information on capturing events see Capturing SSH related events
SshConnectedEvent
The first event to be fired is the SshConnectedEvent . This event is fired once a successful connection has been established. To capture the SshConnectedEvent overload the SshListener#connected method.
Example
/** * Captures SshConnectedEvent */ public void connected(SshConnectedEvent ev) { System.out.println("Connected: " + ev.getHost()); connected = true; }
SshDisconnectedEvent
To disconnect from the SSH server the user may enter the command "exit" (without quotes) at the shell prompt. This will in effect release the connection. Once the connection has been released a SshDisconnectedEvent will be fired. In the SshExample this sets the connected variable to false which in turn triggers the java.io.BufferedReader constructed in the SshExample#main method to stop reading data from the console.
Example
/** * Captures SshDisconnectedEvent */ public void disconnected(SshDisconnectedEvent ev) { System.out.println("Disconnected: " + ev.getHost() + ". Press Enter to exit"); connected = false; }
This concludes the section on how to create an interactive SSH session. For more information see the JavaDoc or look at the SshExample.java source code located in the examples/ssh/ssh directory of your distribution. |