What is the difference between exec_command and send with invoke_shell() on Paramiko?

。_饼干妹妹 提交于 2019-12-01 10:55:10

The difference is that invoke_shell uses SSH shell channel, while exec_command uses SSH exec channel.

What that really means to you as a user/developer really depends on the SSH server, not on Paramiko itself.


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 (Paramiko invoke_shell does that unconditionally, but actually it's possible to open the shell channel without the terminal emulation). SSH terminal clients (like OpenSSH ssh or PuTTY) do obviously use shell channel, under normal circumstances.

  • 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.

    For a typical example of such difference, see Some Unix commands fail with "<command> not found", when executed using Python Paramiko exec_command.

    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. That's why the default value of get_pty is False.

    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 seeming support both, but one of them (typically the exec) is completely broken.


There's a similar question for Java/JSch:
What is the difference between the 'shell' channel and the 'exec' channel in JSch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!