2

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();
        }
    }   
Community
  • 1
  • 1
ahjohnston25
  • 1,915
  • 15
  • 36
  • A side note. Have you considered to use a third-party Expect-like library for interacting with a remote shell? There is a list of java alternatives here http://en.wikipedia.org/wiki/Expect#Alternatives. Also I'm happen to be an author of such a library, it is called ExpectIt. – Alexey Gavrilov Aug 08 '14 at 09:42
  • I was considering it; I'll check out expectit. Thanks! – ahjohnston25 Aug 08 '14 at 11:17
  • @AlexeyGavrilov is there a way to reach out to you directly? I have a question regarding ExpectIt. – ahjohnston25 Aug 08 '14 at 15:29
  • please post you question to the google group: https://groups.google.com/forum/#!forum/java-expectit – Alexey Gavrilov Aug 08 '14 at 19:36

0 Answers0