java jsch 返回值_如何读取JSch命令输出?

小编典典

在等待命令完成的同时,必须连续读取输出。否则,如果命令产生足够的输出以填充输出缓冲区,则该命令将挂起,等待缓冲区被消耗,这将永远不会发生。这样您就陷入僵局。

以下示例在监视命令状态的同时连续读取stdout和stderr。它基于官方的JSch

exec.java示例(仅添加了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");

InputStream commandOutput = channel.getExtInputStream();

StringBuilder outputBuffer = new StringBuilder();

StringBuilder errorBuffer = new StringBuilder();

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.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();

如果在while (!channel.isClosed()) {}之后添加channel.connect();,您会看到ishell

for循环中有足够大的空间(在我的环境中,10000就足够了),循环永远不会结束。

2020-09-09

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用Java JSch库逐行读取远程文件的步骤如下: 1. 创建一个JSch对象,用于连接到远程服务器。 2. 使用JSch对象创建一个Session对象,指定连接的用户名、密码和远程服务器的IP地址。 3. 打开Session连接。 4. 使用Session对象创建一个Channel对象,指定通道类型为“exec”。 5. 设置Channel对象的输入流和输出流,用于执行命令读取命令输出结果。 6. 使用Channel对象执行要读取的文件的命令。 7. 循环读取输出流中的每一行数据,直到读取完整个文件。 8. 关闭Channel对象和Session对象。 下面是示例代码: ```java import com.jcraft.jsch.*; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; public class ReadRemoteFile { public static void main(String[] args) { String host = "remote-server-ip-address"; String user = "username"; String password = "password"; String remoteFile = "/path/to/remote/file"; JSch jsch = new JSch(); Session session = null; Channel channel = null; BufferedReader reader = null; try { session = jsch.getSession(user, host, 22); session.setConfig("StrictHostKeyChecking", "no"); session.setPassword(password); session.connect(); channel = session.openChannel("exec"); ((ChannelExec) channel).setCommand("cat " + remoteFile); channel.setInputStream(null); ((ChannelExec) channel).setErrStream(System.err); InputStream in = channel.getInputStream(); channel.connect(); reader = new BufferedReader(new InputStreamReader(in)); String line; while ((line = reader.readLine()) != null) { System.out.println(line); } } catch (JSchException | IOException e) { e.printStackTrace(); } finally { if (reader != null) { try { reader.close(); } catch (IOException e) { e.printStackTrace(); } } if (channel != null) { channel.disconnect(); } if (session != null) { session.disconnect(); } } } } ``` 在这个示例中,我们使用JSch库连接到远程服务器,然后执行了一个命令读取指定的远程文件。最终,我们循环读取输出流中的每一行数据,并打印到控制台上。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值