I'm using JSch to pull data from a router, however the way the router's prompt works is preventing me from successfully reading the data.
The flow should go something like this:
router>drop_into_privileged_mode
Password: password_entered_here
router#give_me_data
...Data goes here...
router#
The problem I'm running into is that since the router drops back to a shell prompt after give_me_data, the InputStream
never dies. The natural choice would be to run a command like give_me_data; exit;
or give_me_data && exit
, but unfortunately the router's operating system doesn't allow me to chain commands like that. Equally frustratingly, the command give_me_data takes so long to run that putting the exit
command into the OutputBuffer does nothing (as it gets sent while the other command is still running and thus not interpreted).
Is there a way for me to preserve the connects of the InputStream, but kill the connection? If that's possible, then the buffer will have a natural end (as the connection is dead), and I can just copy that to a variable. Alternatively, if there's a way to transfer all information currently present in the stream out, then kill the connection, that would work too.
I've tried the solutions proposed in similar StackOverflow questions, such as this one and this one to no avail.
Additionally, here is the code that I'm currently using (It's a solution I devised that suffers from the same blocking problem):
InputStream in = channel.getInputStream(); //This gets the output from the router
byte[] buffer = new byte[1024];
StringBuffer stringBuffer = new StringBuffer();
while (true) {
while (in.available() > 0) {
int i = in.read(buffer, 0, 1024);
if (i < 0) {
break;
}
stringBuffer.append(new String(buffer, "UTF-8"));
}
System.out.println("done");
channel.disconnect(); // this closes the jsch channel
if (channel.isClosed()) {
if (in.available() > 0) {
continue;
}
System.out.println("exit-status: " + channel.getExitStatus());
break;
}
try{
Thread.sleep(1000);
}catch(InterruptedException e){
e.printStackTrace();
}
}