Jsch - 一个会话多个频道 [英] Jsch - One Session Multiple Channels

查看:1334
本文介绍了Jsch - 一个会话多个频道的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设法通过ssh用Jsch执行单个命令但是当我尝试执行第二个命令时它失败

I managed to execute a single command through ssh with Jsch but when i try to execute a second command it fails

对于调试我把这个问题带到了这一行:

For debugging i brought this problem down to this lines:

import java.io.IOException;
import java.io.InputStream;

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;

public class Exec {

    public static void test(Session session) throws Exception {
        Channel channel = session.openChannel("exec");
        ((ChannelExec) channel).setCommand("pwd");

        channel.setInputStream(null);

        ((ChannelExec) channel).setErrStream(System.err);

        InputStream in = channel.getInputStream();

        channel.connect();

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

    public static void main(String[] arg) {
        try {
            JSch jsch = new JSch();         
            Session session = jsch.getSession("nck", "127.0.0.1", 22);          
            session.setPassword("asd");         
            session.setConfig("StrictHostKeyChecking", "no");           
            session.connect();

            test(session); // This one succeeds with exit-status: 0         
            test(session); // This one fails with exit-status: 255

            session.disconnect();
        } catch (Exception e) {
            //
        }
    }
}

这主要是官方的Exec示例,但这给了我这个输出:

This is mostly the official Exec example but this gives me this output:

/home/nck
exit-status: 0
exit-status: 255

第一个命令成功执行第二个不执行。

The first command is executed succesfully the second doesnt.

任何想法?

推荐答案

嘿,我和jsch和Ubuntu有同样的问题。 (怎么样)你解决了吗?为每次执行创建一个新会话会占用太多时间吗?目前我捕获了jsch异常并搜索session is not down然后我重新连接会话并再次执行命令。这可行,但不是一个好的解决方案。

Hey I have exactly the same problem with jsch and Ubuntu. (How) did you solve it? Making a new session for every execute eats too much time? At the moment I catch the jsch exception and search for "session isn't down" then I reconnect the session and execute command again. That works but is not a good solution.

编辑:我错了,我现在通过使用

I was on the wrong way I solved my problem for now by using

channel = session.openChannel(shell);

而不是exec。

这篇关于Jsch - 一个会话多个频道的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆