Multiple commands using JSch

后端 未结 2 1643
Happy的楠姐
Happy的楠姐 2020-12-01 21:46

My requirement is as follow:
I have to login to Unix box using my credentials and once login, I have to do sudo to different user. Once sudo is successful, I have to inv

相关标签:
2条回答
  • 2020-12-01 22:20

    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.

    0 讨论(0)
  • 2020-12-01 22:24

    The post may be old, but I found another easy way that allows you to retrieve 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.getInputStream() to read the result of the command.

    0 讨论(0)
提交回复
热议问题