Overview
This article will demonstrate how using the SSH classes in SSH Factory or Secure iNet Factory components you can develop a console based interactive SSH shell.
To see what else these components have to offer please download a FREE 30 day evaluation here: SSH Factory Evaluation and Secure iNet Factory Evaluation.
Prerequisites
- An SSH server
- JDK 1.4 or above
- SSH Factory or Secure iNet Factory
Using Ssh Class to Connect to an SSH Server
The Ssh class provides an easy to use programming interface for communicating with SSH (Secure SHell) servers. This tutorial focuses on using the class by itself to communicate with an SSH server like a regular interactive SSH client. To see it used as an underlying object for SSH scripting see Scripting SSH sessions using Java and JSCAPE Components
Please note that the Ssh Class is present in both SSH Factory and Secure iNet Factory.
There are two ways to define a class that make use of the Ssh class. If you want to listen to events fired by the SSH instance you could have your class implement the SshListener interface or extend the SshAdapater class, which already implements SshListener and provide a default implementation.
//import the necessary interface/class
import com.jscape.inet.ssh.SshListener;
public class SshTutorial implements SshListener {
…
}
Or if not interested on listening to SSH events then a plain Java class will do.
public class SshTutorial {
…
}
Below is a code snippet that uses Ssh class to connect an SSH server. Once a connection with an SSH server is established the user will then be able to issue interactive commands within the session.
//import the necessary classes
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import com.jscape.inet.ssh.Ssh;
import com.jscape.inet.ssh.util.SshParameters;
…
// state of SSH connection
private boolean connected = false;
public void connectSsh(String hostname, String username, String password) {
Ssh ssh = null;
try {
// create/initialize new Ssh instance
SshParameters params = new SshParameters(hostname, username, password);
ssh = new Ssh(params);
// register to capture events if applicable (if your class extends
// SshAdapter or implements SshListener)
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 session commands from user via standard input
BufferedReader bin =
new BufferedReader(new InputStreamReader(System.in));
// read data from console
while (connected && (line = bin.readLine()) != null) {
// send line with LF to SSH server
line += "\n";
try {
out.write(line.getBytes());
out.flush();
} catch(Exception ioe) {
// set this flag to false to exit this loop when exception occurs
connected = false;
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if(connected) {
ssh.disconnect();
}
} catch(Exception e) {
}
}
}
…
Download the complete Java code for this tutorial here: SshTutorial.java
Summary
There are maybe few rare cases that an application will require an interactive SSH client. The power of the Ssh class lies in its ability to be used in a non-interactive (batch mode) and automate commands issued on an SSH server.
References
Comments