0

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;
}

}
kmalik
  • 109
  • 1
  • 2
  • 11

1 Answers1

0

I'm work on similar project, now I created global output stream which doesn't need to wait anything. I re-written OutputStream to use JTextArea as output

private java.util.List<Character> buffer = new ArrayList<Character>();
public class AreaOutputStream extends OutputStream {
private JTextArea textArea;

public AreaOutputStream(JTextArea textArea) {
    this.textArea = textArea;
}

@Override
public synchronized void write(int b) throws IOException {
    // collect all character into the buffer
    buffer.add((char) 
    // on '\n' character append to output window
    if (String.valueOf((char) b).equals("\n")) {
            StringBuilder sb = new StringBuilder(buffer.size());
            for (Character c : buffer) sb.append(c);
            String line = sb.toString();
            // filter output from special characters
            //and replace invitation '[username@server~]$ ' with current time
            line = line.replaceAll(" \r", "");
            line = line.replaceAll("\r", "");
            line = line.replaceAll(String.valueOf(Pattern.compile("\\[[0-9][^m ]*m")), "");
            line = line.replaceAll(String.valueOf(Pattern.compile("\\[.*@[^$]*\\$ ")), DateFormat.getTimeInstance().format(java.lang.System.currentTimeMillis()) + " ");
            line = line.replaceAll(String.valueOf(Pattern.compile("\u001B")), "");
            if (!line.matches("(.*)Last login(.*)from(.*)\n")) {
                textArea.append(line);
                textArea.setCaretPosition(textArea.getDocument().getLength());
            }
        buffer.clear();
    }

}
}

So, to use this output stream in section where you create your channel enough set following:

channel.setOutputStream(new AreaOutputStream(TEXT_AREA_NAME))

So, you can create another channels to execute commands and set as output this class - all these will appear in JTextArea.

BTW now I try to create InputStream to send commands in one channel (for contact please replace 'domain.name' with 'mail.ru')

Hope this helps and sorry for my english.

reconnect
  • 1
  • 1