Rick.Brown

Flexible SSH test step

Discussion created by Rick.Brown Employee on Feb 20, 2014
Latest reply on Mar 5, 2014 by MaryGreening

I sometimes have the need to run multiple commands in a session to a *nix server. We have a single-command SSH step that can be provided, but this does not have te flexibility I often require.

My preferred go-to in these situations is to download a JAR into my hotDeploy folder and create a scripting step to use that JAR.

I downloaded the Ganymed-SSH2 for Java from http://www.cleondris.ch/en/opensource-ssh2.php According to that website, this component is apparently used in various projects, such as the Eclipse SVN plug-in & Cyberduck. I copied the JAR to my LISA_HOME/hotDeploy (YMMV if yours is a "shared" install). Nb: check licensing for whatever component you're going to download & use in a LISA step. You don't want to inadvertently use a component that is restrictive. Ganymed-SSH2 for Java uses the BSD licence, so should be ok.

There are various examples of usage on the Internet, so I selected one of those and re-wrote bits of it until it was compatible with Bean Shell (the LISA "Java Scripting" step) and I added some LISA facilities for data manipulation.

This script takes data values for {{hostname}}, {{username}} and {{password}}. You can generate these however you like - I would generally add a data set to the scripting step with three columns of those names.

This is a sample script to perform multiple actions. All comments welcomed.

-----snip-----

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
 
import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.Session;
import ch.ethz.ssh2.StreamGobbler;
 
 
 
public connect(hostname, portNumber, username, password) {
    /* Create a connection instance */
    Connection conn = new Connection(hostname, portNumber);
 
    /* Now connect */
    conn.connect();
 
    /* Authenticate.
     * If you get an IOException saying something like
     * "Authentication method password not supported by the server at this stage."
     * then please check the FAQ.
     */
    boolean isAuthenticated = conn.authenticateWithPassword(username, password);
 
    if (isAuthenticated == false)
        throw new IOException("Authentication failed.");
    return(conn);
}
 
public type(conn, myCommand) {
    /* Create a session */
    Session sess = conn.openSession();
 
    sess.execCommand(myCommand);
    /* 
     * This basic example does not handle stderr, which is sometimes dangerous
     * (please read the FAQ).
     */
    InputStream stdout = new StreamGobbler(sess.getStdout());
 
    BufferedReader br = new BufferedReader(new InputStreamReader(stdout));
 
    String response = "";
    while (true)
    {
        String line = br.readLine();
        if (line == null)
            break;
        
        //Adding the \n is for human readability.
        //you might want to not bother with formatting the response
        response = response + line + "\n";
    }
 
    /* Show exit status, if available (otherwise "null") */
 
    sessExitStatus = sess.getExitStatus();
//    testExec.setStateValue("Exit status", sessExitStatus);
    /* Close this session */
    sess.close();
 
    //I am adding the exit status to the response
    //You might not want to do this
    return response + sessExitStatus;
}
 
public disconnect(myConnection){
    /* Close the connection */
    myConnection.close();
    return 0;
}
 
 
 
 
//String hostname = "192.168.1.9";
//String username = "rbrown";
//String password = "";
String hostname = testExec.getStateValue("hostname");
String username = testExec.getStateValue("username");
String password = testExec.getStateValue("password");
int portNumber = 22;
myConnection = connect(hostname, portNumber, username, password);
 
rsp1 = type(myConnection, "uname -a && date && uptime && who");
rsp2 = type(myConnection, "/bin/ls -l");
rsp3 = type(myConnection, "whoami");
rsp4 = type(myConnection, "pwd");
 
//Determine what output values to store as LISA properties
testExec.setStateValue("Response1", rsp1);
testExec.setStateValue("Response2", rsp2);
testExec.setStateValue("Response3", rsp3);
testExec.setStateValue("Response4", rsp4);
 
disconnectValue = disconnect(myConnection);
 
//return sessExitStatus + "" + sess2ExitStatus;
 

-----snip-----

Rick

Outcomes