Multiple commands through JSch shell

前端 未结 4 1496
不知归路
不知归路 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:17

    The command is a String and can be anything the remote shell accepts. Try

    cmd1 ; cmd2 ; cmd3
    

    to run several commands in sequence. Or

    cmd1 && cmd2 && cmd3
    

    to run commands until one fails.

    Even this might work:

    cmd1
    cmd2
    cmd3
    

    or in Java:

    channel.setCommand("cmd1\ncmd2\ncmd3");
    

    Sidenote: Don't put passwords and user names into the code. Put them into a property file and use a system property to specify the name of the property file. That way, you can keep the file even outside the project and make sure passwords/user names don't leak.

提交回复
热议问题