Synchronous SSH Communication

 

Description

This sample application can be used to send CLI commands to the devices in a synchronous mode of communication using SSH protocol. You can create either dedicated or multiplexed (non-dedicated) CLI sessions. In the synchronous mode of communication, the message is sent to the device and waits till the response is received.

Format

 

Usage: java SshSyncApp [-s session(d/nd)] [-n RemotePort] [-cp CmdPrompt] [-l LoginName ] [-p Password ] [-d Debug] RemoteHost Command

 

Options

 

[-s]

 

-

Session, dedicated (d) or non-dedicated (nd). Default is dedicated.

[-n]

-

Remote port. The default is the telnet port (22).

[-cp]

-

Command prompt. This is the prompt displayed by the device for each command. Default is $.

[-d]

-

To set debug as true or false.

[-l]

-

Login Name. One of the user names/Login names present in the remote host.

[-p]

-

Password for the user.

Remote host

M

The host to which the session has to be established.

Command

M

The actual command that has to be sent to the device.

 

Note

 

1. M - Mandatory

2. To run the example using SSH2 protocol follow the procedure given in Installation and Setup section.

 

Initially, import the following packages:

 

com.adventnet.cli.*;

com.adventnet.cli.transport.*;

com.adventnet.cli.transport.ssh.SshProtocolOptionsImpl;

com.adventnet.cli.transport.ssh.SshTransportProviderImp;

java.io.*;

 

The main function contains the following declarations.

 

String usage = "java SshSyncApp [-s session(d/nd)] [ -n RemotePort]

[ -cp CmdPrompt] [-l LoginName] [ -p Password] [-d Debug] RemoteHost Command";

String options[] = {"-s", "-n", "-cp", "-l", "-p", "-d"};

String values[] = { null, null, null, null, null, "None" };

String cmdData = null;

boolean enablePooling = false;

boolean isDebug = false;

 

ParseOptions class is used to parse the arguments passed to the application to check the proper syntax and validity. In case of wrong syntax, the system throws exception.

 

ParseOptions opt = new ParseOptions(args,options,values,usage);

if(opt.remArgs.length<2){

        opt.usage_error();

Now, construct the ProtocolOptions for SSH connections. This file contains methods to set the remote host, login name, password, and other connection parameters.

SshProtocolOptionsImpl spoi = new SshProtocolOptionsImpl();

 

The remote host, login name, and the password are set to the transport implementation class so that it can be used by the transport provider class to send and receive messages.

 

spoi.setRemotePort(Integer.parseInt(values[1]));

spoi.setPrompt(values[2]);

spoi.setLoginName(values[3]);

spoi.setPassword(values[4]);

spoi.setRemoteHost(opt.remArgs[0]);

 

By setting isDebug as true,  the debug can be enabled and the debug messages can be received in <CLI Home>/log directory.

 

if(values[5].equals("Set")){

    isDebug = true ;

 

Create CLI command using CLIMessage. The CLIProtocolOptions can be set so that the message is sent to the appropriate device. You have to instantiate this class with the message to be sent and pass it to syncSend() in CLISession to send the message.

 

CLIMessage climsg = new CLIMessage(" ");

CLISession CLIsession = null;

Now, create and open CLI session with SSH as a protocol implementation and facility to create a dedicated or non-dedicated session. You can also set the debug level as given below.

clisession=new CLISession(spoi,enablePooling);

if( isDebug ) {

    clisession.setDebug(true);

    clisession.setDebugLevel(2);

} else {

    clisession.setDebug(false);

}

clisession.open();   

 

The message can be sent through synchronous communication using the following procedure:

 

climsg.setCLIPrompt(spoi.getPrompt());

System.out.println(clisession.syncSend(climsg).getData());

 

The complete source code is available in <CLI Home>/example/ssh directory.