【问题标题】:How to ssh using JSch to ZXA10 C320 device如何使用 JSch SSH 到 ZXA10 C320 设备
【发布时间】:2019-05-31 20:55:42
【问题描述】:

我正在开发一个 Java 应用程序以通过 ssh 连接到 zxa10 c320 设备。我使用 jsch 库来 ssh。但是当我运行应用程序时,它在第一个 while 循环中保持不变而不返回任何输出。当我通过指向 ssh 其他一些 Linux 服务器来运行此应用程序时,它可以正常工作。

如果我做错了什么,请告诉我。

我在 'exec' 和 'shell' 模式下都试过这个。

另外,我尝试使用 sshj 库来做到这一点。它也没有工作。

public class RunCommand {

    public String runCommand(String command) {
        String host = "XX.XX.X.XX";
        String user = "user";
        String password = "password";
        String result = "";

        try{
            Properties config = new Properties();
            config.put("StrictHostKeyChecking", "no");
            JSch jsch = new JSch();

            // Create a JSch session to connect to the server
            Session session = jsch.getSession(user, host, 22);
            session.setPassword(password);
            session.setConfig(config);

            // Establish the connection
            session.connect();

            if(session.isConnected()) {
                System.out.println("Connected...");
            }

            ChannelExec channel = (ChannelExec) session.openChannel("exec");

            channel.setCommand("pwd");

            int stat = channel.getExitStatus();

            StringBuilder outputBuffer = new StringBuilder();
            StringBuilder errorBuffer = new StringBuilder();

            InputStream in = channel.getInputStream();
            InputStream err = channel.getExtInputStream();

            channel.setPty(true);
            channel.connect();

            System.out.println("Channel Connected");
            System.out.println("channel.isConnected() "+channel.isConnected());
            System.out.println("channel.isClosed() "+channel.isClosed());


            byte[] tmp = new byte[1024];
            while (true) {
                while (in.available() > 0) {
                    int i = in.read(tmp, 0, 1024);
                    if (i < 0) break;
                    outputBuffer.append(new String(tmp, 0, i));
                }
                while (err.available() > 0) {
                    int i = err.read(tmp, 0, 1024);
                    if (i < 0) break;
                    errorBuffer.append(new String(tmp, 0, i));
                }
                if (channel.isClosed()) {
                    if ((in.available() > 0) || (err.available() > 0)) continue; 
                    System.out.println("exit-status: " + channel.getExitStatus());
                    break;
                }
                try { 
                    Thread.sleep(1000);
                } catch (Exception ee) {
                }
            }

            System.out.println("output: " + outputBuffer.toString());
            System.out.println("error: " + errorBuffer.toString());
            channel.disconnect();
            session.disconnect();
            System.out.println("Channel And Session Closed!!!");
        }catch(JSchException e){
            e.printStackTrace();
            result = "Auth fail";
        }catch(Exception e){
            e.printStackTrace();
        }
        return result;
    }
}

【问题讨论】:

  • 如果删除channel.setPty(true);会怎样?
  • 另外,你可以ssh user@host pwd吗? (或plink user@host pwd - 如果您使用的是 Windows)。
  • 它在哪里阻塞?
  • 嗨 @MartinPrikryl 我尝试删除 channel.setPty(true)。它也没有工作。
  • in.available() 被调用时被阻塞。似乎 in.available() 不会大于 0。

标签: java ssh router jsch


【解决方案1】:

我无法使用 peo java 做到这一点。但是我能够使用 python 的 paramiko 模块来做到这一点。我所做的是我写了两个应用程序,一个用java,一个用python,然后我从java应用程序调用python应用程序,然后通过python应用程序ssh到gpon。

【讨论】:

    猜你喜欢
    • 2011-12-01
    • 2011-10-09
    • 1970-01-01
    • 2017-01-07
    • 1970-01-01
    • 1970-01-01
    • 2015-09-17
    • 2014-02-10
    • 2011-01-25
    相关资源
    最近更新 更多