What is the difference between the 'shell' channel and the 'exec' channel in JSch

前端 未结 4 1907
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-29 05:36

I want to be able to send many consecutive command represented as strings within a Java application to a SSH server for execution. Should I use:

Channel cha         


        
4条回答
  •  生来不讨喜
    2020-11-29 06:42

    It's not so much about JSch. It's about how the server implements the two channels.


    In common *nix OpenSSH server:

    • The shell channel executes a login shell (as if you login with SSH terminal client). The shell will then present a command prompt and wait for client/user to type the commands. The purpose of the shell channel is to implement an interactive shell session. That is something one will do very very rarely. If you do, you typically want to use terminal emulation.

      The shell channel is obviously used by SSH terminal clients (like OpenSSH ssh or PuTTY), under normal circumstances.

      The shell channel is a black box with an input and output. The input and output have no structure. If you for example execute a command by sending it to the input, you will never be able to tell when it ended. If you send two commands to input queue, you won't be able to distinguish what output comes from what command.

    • The exec command takes a command as an "argument" and executes it in an isolated environment – still via user's default shell, but not as a "login" shell, what may cause significant differences in the command execution.

      The purpose of the exec channel is automating a command execution. So typically you do not want to use a terminal emulation, to avoid the command to do fancy stuff like pagination, coloring and mainly interactive confirmations.

      exec channel is used by OpenSSH ssh or PuTTY plink, when you specify a command to execute on its command line:

      ssh user@host command
      

    With less common SSH servers, the difference can be even more significant. Some servers may even not support one of the channels. It is also quite common that they seemingly support both, but one of them (typically the exec) is completely broken.


    There's a similar question for Python/Paramiko:
    What is the difference between exec_command and send with invoke_shell() on Paramiko?

提交回复
热议问题