Search code examples
javasshsshj

Sending Multiple Commands to device Through SSH


I am attempting to send multiple commands to a device using the SSHJ library. There is the option of sending multiple commands in this format:

Command command = sshSession.exec("show line; show ip interface brief;");

This works, but it is not always usable in my case. I have found other suggestions such as the second answer here.

When I attempt this suggestion the first command works fine and then it cycles between this error:

net.schmizz.sshj.connection.ConnectionException: Broken transport; encountered EOF
...
Caused by: net.schmizz.sshj.transport.TransportException: Broken transport; encountered EOF

or

Exception in thread "main" java.lang.IllegalStateException: Not connected
at net.schmizz.sshj.SSHClient.checkConnected(SSHClient.java:713)
at net.schmizz.sshj.SSHClient.startSession(SSHClient.java:651)

The code used is:

sshSession = sshClient.startSession();
Command command = sshSession.exec("sho ip int brie");
System.out.println(IOUtils.readFully(command.getInputStream()));//Just to see the reply while testing
command.join(5, TimeUnit.SECONDS);

sshSession = sshClient.startSession();
Command command2 = sshSession.exec("sho line");
System.out.println(IOUtils.readFully(command2.getInputStream()));//Just to see the reply while testing

A note if needed, the device I am connecting to, and majority of devices that it will connect to are Cisco networking equipment.

Thank you for any assistance. -Jarrod


Solution

  • Never found a resolution for the exact problem. But I worked around the issue by using the DefaultPTY and providing my own streams with all the data I wanted to send. Playing around with this example.