I am currently having an issue implementing the jsch library to grab information from a ZyXel switch. The program itself will grab some information to confirm the type of switch and then upload the correct firmware and config.
My issue, to me, appears to be a buffer issue. I have no problems sending the command but when I send it, depending on when i run it or how often, I either get half the information I should be getting or all of it. I think it is because sometimes the buffer doesn't empty all the way into the ByteArrayInputStream but at this point I am at a lost. I was wondering if anyone can point me into the right direction on what i am getting wrong. I assume it is a basic InputStream or jsch documentation issue misunderstanding
Thanks!..my code is below.
package ssh;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import com.jcraft.jsch.*;
public class ssh {
private static String user = "admin";
private static String host = "192.168.1.1";
private static String password = "1234";
private static String command = "";
public static void startAutomation() {
JSch jsch = new JSch();
Session session = null;
OutputStream output = null;
Channel channel = null;
try {
session = jsch.getSession(user,host,22);
session.setPassword(password);
session.setConfig("StrictHostKeyChecking","no");
session.connect();
channel = session.openChannel("shell");
command = "show system-information\n";
output = runCommand(command, session, channel);
String test = "NOTHING";
if (output.toString().contains("ES-2024A")) {
test = "true";
command = "show run\n";
output = runCommand(command,session,channel);
} else {
test = "false";
}
System.out.println(test + " This is a 2024A");
} catch (JSchException | InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
channel.disconnect();
session.disconnect();
}
}
public static OutputStream runCommand(String c,Session session,Channel channel) throws InterruptedException, JSchException{
InputStream is = new ByteArrayInputStream(c.getBytes());
channel.setInputStream(is);
OutputStream outputInfo = new ByteArrayOutputStream();
channel.setOutputStream(outputInfo);
channel.connect(15*1000);
try {
is.close();
outputInfo.flush();
outputInfo.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return outputInfo;
}
}