简体   繁体   English

如何使用JSch连接在单个会话中运行多个命令并读取其输出?

[英]How to run multiple commands in a single session with JSch connection and read their output?

I have series of commands that needs to be executed one after another on a remote vm. 我有一系列需要在远程虚拟机上一个接一个地执行的命令。 I also want to get the output of each command executed. 我还想获得每个命令的输出。 This will be done with single JSch session. 这将通过单个JSch会话完成。 How can I achieve this? 我怎样才能做到这一点?

To execute multiple commands, just use an appropriate syntax of your server. 要执行多个命令,只需使用适当的服务器语法即可。 Most *nix server use semicolon or double-ampersand (with different semantics). 大多数* nix服务器使用分号或双符号(具有不同的语义)。

See Multiple commands using JSch . 请参阅使用JSch的多个命令


Though to if your want to read commands output, you will have problem distinguishing, where output of one commands ends and output of the following commands starts. 虽然如果您想要读取命令输出,您将有问题区别,其中一个命令的输出结束并且以下命令的输出开始。

Then it's better to execute each commands in its own "exec" channel. 然后最好在自己的“exec”通道中执行每个命令。 Single SSH session can have multiple channels opened (in sequence or even in parallel). 单个SSH会话可以打开多个通道(按顺序或甚至并行)。

Channel channel1 = session.openChannel("exec");
((ChannelExec)channel1).setCommand(command1);
// ...

Channel channel2 = session.openChannel("exec");
((ChannelExec)channel2).setCommand(command2);
// ...

See also How to perform multiple operations with JSch . 另请参见如何使用JSch执行多个操作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM