Execute a list of commands from an ArrayList using JSch exec in Java

后端 未结 1 1448
深忆病人
深忆病人 2020-11-29 13:04

Using JSch \"exec\" channel, I connect to a remote server and execute a command. What I can\'t do is to execute a list of commands stored in ArrayList. I use fo

相关标签:
1条回答
  • 2020-11-29 13:28

    You cannot call the setCommand multiple times. The "exec" channel can run a single "command" only.

    But on most systems/shells, the "command" can actually include multiple commands.

    A syntax will depend on your system/shell. Semicolons work usually:

    ((ChannelExec)channel).setCommand("hostname ; df -l");
    

    Some servers/shells also allow newline.

    ((ChannelExec)channel).setCommand("hostname\ndf -l");
    

    On *nix servers, you can also use && to make the following commands be executed only when the previous commands succeeded:

    ((ChannelExec)channel).setCommand("hostname && df -l");
    

    See also Multiple commands through JSch shell.


    Another option is to open a new channel for each command. Note that you can have multiple channels over a single SSH session. You do not have to reconnect for each channel/command.

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