Multiple commands through JSch shell

前端 未结 4 1499
不知归路
不知归路 2020-11-30 01:05

I was trying to execute multiple commands through SSH protocol using the JSch library. But I seem to have stuck and cannot find any solution. The setCommand() m

4条回答
  •  借酒劲吻你
    2020-11-30 01:19

    If you do not have to distinguish the inputs or outputs of the individual commands, the answer from Aaron (giving all the commands in a row, separated by \n or ;) is fine.

    If you have to handle them separately, or don't know the later commands before the earlier ones are finished: You can open multiple exec-Channels on the same Session (i.e. connection), one after the other (i.e. after the one before was closed). Each one has its own command. (But they don't share environment, so a cd command in the first one has no effect on later ones.)

    You simply have to take care to have the Session object around, and not create a new one for each command.

    Another option would be a shell channel, and then passing the individual commands to the remote shell as input (i.e. via a stream). But then you have to take care to not mix the the input to one command with the next command (i.e. this works only if you know what the commands are doing, or if you have an interactive user who can supply both input to the command and the next command, and knows which one is to be used when.)

提交回复
热议问题