java 如何读取JSch命令输出?

enxuqcxy  于 2023-02-14  发布在  Java
关注(0)|答案(1)|浏览(269)

下面的代码:

JSch jsch = new JSch();
jsch.setKnownHosts(dotSshDir + "/known_hosts");
jsch.addIdentity(dotSshDir + "/id_rsa");

Session session = jsch.getSession(userName, hostname, 22);
session.connect();

ChannelExec channel = (ChannelExec) session.openChannel("exec");
channel.setCommand(command);
channel.setInputStream(null);
channel.setErrStream(System.err);
Reader reader = new InputStreamReader(channel.getInputStream());

char[] buf = new char[1024];
int numRead;
while ((numRead = reader.read(buf)) != -1) {
    String readData = String.valueOf(buf, 0, numRead);
    result.append(readData);
    buf = new char[1024];
}

它挂在试图从阅读器读取。我该如何解决这个问题?我该如何去寻找发生了什么?

mrzz3bfm

mrzz3bfm1#

在等待命令完成的同时,必须连续读取输出,否则,如果命令产生的输出足以填满输出缓冲区,命令就会挂起,等待缓冲区被消耗掉,这是从来没有发生过的,所以你会得到一个死锁。
下面的示例在监视命令状态的同时连续读取stdout和stderr。它基于official JSch exec.java example(只是添加了一个stderr阅读)。

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

channel.setCommand(
    "for((i=1;i<=10000;i+=2)); do echo \"Long output - $i\"; done ; " +
    "echo error output >&2");

ByteArrayOutputStream outputBuffer = new ByteArrayOutputStream();
ByteArrayOutputStream errorBuffer = new ByteArrayOutputStream();

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

channel.connect();

byte[] tmp = new byte[1024];
while (true) {
    while (in.available() > 0) {
        int i = in.read(tmp, 0, 1024);
        if (i < 0) break;
        outputBuffer.write(tmp, 0, i);
    }
    while (err.available() > 0) {
        int i = err.read(tmp, 0, 1024);
        if (i < 0) break;
        errorBuffer.write(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("UTF-8"));
System.out.println("error: " + errorBuffer.toString("UTF-8"));

channel.disconnect();

如果在channel.connect();之后添加while (!channel.isClosed()) {},您将看到,如果shell for循环中的i足够大(在我的环境中,10000就足够了),则循环永远不会结束。

相关问题