Overview
This tutorial will demonstrate how to automate an SSH session using SSH Factory or Secure iNet Factory and issue commands in batch mode (non-interactive).
There are three ways to accomplish this. Write Java code that will make use of SshSession class, write Java code that will make use of SshScript class or write an SSCL command script. Both SshSession and SshScript class are present in SSH Factory and Secure iNet Factory components. SSCL is included only in SSH Factory.
Prerequisites
- An account on a machine running SSH server.
- SSH Factory or Secure iNet Factory
SSH Scripting using SshSession
SshSession is a convenience class that presents a very simple, straightforward API for automating SSH sessions.
There are two ways to define a class that uses SshSession. If you want to listen to events fired by the underlying SSH instance then 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 classes
import com.jscape.inet.ssh.SshAdapter;
//extend SshAdapter and implement its interface.
//we could then add ourself to SshSession as an ssh event listener
public class SshSessionTutorial extends SshAdapter {
…
}
Or if not interested on listening to SSH events then a plain Java class will do.
public class SshSessionTutorial {
…
}
Below is a code snippet that uses SshSession class to connect to an SSH server and issue commands once a session is established.
//import the necessary classes
import com.jscape.inet.ssh.SshException;
import com.jscape.inet.ssh.SshSession;
import com.jscape.inet.ssh.util.SshParameters;
…
public void executeSshScript(String hostname, String username, String password)
throws SshException
{
//initialize and create an SshSession object
SshParameters sshParams = new SshParameters(hostname, username, password);
SshSession session = new SshSession(sshParams);
session.setShellPrompt("$");
//attempt to connect and set timeout to 1000ms to disconnect if unable to.
session.connect(1000);
//issue commands and print command output
String dirListing = session.send("ls -al");
System.out.println(dirListing);
String pwd = session.send("pwd");
System.out.println(pwd);
//disconnect session
session.disconnect();
}
…
Download the complete Java code here: SshSessionTutorial.java
SSH Scripting Using SshScript
SshScript class provides a simple way of running a set of commands against a SSH server. It’s a bit more involved than SssSession but offers more control like the ability to have an SshScriptListener to capture events relating to every command submitted.
When defining your Java class that makes use of the SshScript class you may have it implement SshListener or extend SshAdapter if you want to listen to events fired by the Ssh instance bound to it. You may also want to have your class implement SshScriptListener to capture events that occur during the execution of each command.
//import the necessary class/interface
import com.jscape.inet.ssh.SshAdapter;
import com.jscape.inet.ssh.SshScriptListener;
public class SshScriptTutorial extends SshAdapter implements SshScriptListener {
…
}
If not interested in capturing any events a plain class definition will do.
public class SshScriptTutorial {
…
}
Below is a code snippet that uses SshScript class to connect to an SSH server and issue commands once a session is established.
When instantiating an SshScript object, an Ssh object is passed to its constructor and it gets bound to that Ssh object. When that Ssh object is connected all the tasks added to the SshScript instance are executed. A command is added by instantiating an SshTask object and adding it to an SshScript instance.
//import the necessary classes
import com.jscape.inet.ssh.Ssh;
import com.jscape.inet.ssh.SshException;
import com.jscape.inet.ssh.SshScript;
import com.jscape.inet.ssh.SshTask;
import com.jscape.inet.ssh.util.SshParameters;
…
public void executeSshScript(String hostname, String username, String password)
throws SshException
{
// assumes that SSH shell prompt is "$" .. this MUST match exactly
String shellPrompt = "$";
// initialize and create new Ssh instance
SshParameters sshParams = new SshParameters(hostname,username,password);
Ssh ssh = new Ssh(sshParams);
// register this class (if applicable) to receive Ssh events
ssh.addSshListener(this);
// create new script object and bind to the given ssh object
SshScript script = new SshScript(ssh);
// register this class (if applicable) to receive task events
script.addSshScriptListener(this);
// add tasks to script object
script.addTask(new SshTask(shellPrompt, "ls -al", shellPrompt));
script.addTask(new SshTask(shellPrompt, "pwd", shellPrompt));
// connect to SSH server and execute script
ssh.connect();
// wait until last task is complete
while(!script.isComplete()) {
try {
Thread.sleep(100);
} catch(Exception e) {}
}
// disconnect from server
ssh.disconnect();
}
…
Download the complete Java code here: SshScriptTutorial.java
SSH Scripting using SSCL
Above you have seen two ways to script an SSH session. It requires a bit of Java coding and while relatively easy is still a bit more involved.
Here we’ll show you how to do away with the Java programming altogether and use SSCL (Secure Shell Command Line) component to automate an SSH session. SSCL uses a simple scripting language. Below is a sequence of SSCL commands that accomplish the same task as the above Java codes.
prompt hostname "Enter SSH hostname: "
prompt username "Enter SSH username: "
prompt password "Enter SSH password: "
set secure true
connect
send "ls -al"
send "pwd"
sendnowait "exit"
wait 3
disconnect
The above commands can be written into a file and passed to SSCL component for direct execution like so:
java -jar /path/to/sshfactory.jar -f sscl_commands_file.txt
Please note that SSCL is only available with SSH Factory and is not present on Secure iNet Factory.
For SSCL command reference see link below.
Summary
The motivation for automating an SSH session are plentiful. Things like router configuration, user management and other administration tasks are good examples. Basically anything that you interactively do inside an SSH session that is repetitive. Scripting makes it easyly repeatable and less error prone.
References
SSH Factory for Java
http://www.jscape.com/sshfactory/index.html
Secure iNet Factory
http://www.jscape.com/secureinetfactory/index.html
SshSession
http://www.jscape.com/sshfactory/docs/html/overview21.html
SshScript
http://www.jscape.com/sshfactory/docs/html/overview20.html
SSCL
http://www.jscape.com/sshfactory/docs/html/overview.html