Multiple commands using JSch

半世苍凉 提交于 2019-11-27 15:35:00
Sumit Sharma

For executing multiple commands in sequence, you can create a command string like below:

String script ="pbrun su - user; cd /home/scripts;./sample_script.sh”

Execute it and pass this string to your method above.

The post may be old, but I found an other easy way that allows you to retreive the output of each command separately. Note that this code has to be executed once the session has been opened, as shown in the examples (http://www.jcraft.com/jsch/examples/Exec.java.html):

for (String command : commands) {
            ChannelExec channel = (ChannelExec) session.openChannel("exec");
            channel.setInputStream(null);
            channel.setErrStream(System.err);
            channel.setCommand(command);
            channel.connect();
            printOutput(channel);
            channel.disconnect();
        }

Where printOutput uses channel.getInpuStream() to read the result of the command.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!