Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

So what is the difference between SSHClient.exec_command() and send with SSHClient.invoke_shell on Paramiko?

I can send and execute command with exec_command to MikroTik router device but can't execute it with send (invoke_shell()).

On the other hand, I can send and execute command send (invoke_shell()) to Cisco device, but can't execute it with exec_command.

The command I mean is the configuration command like routing (ip route xxx xxx) or make vlan or add an ip address and etc..

like image 394
Adhy Avatar asked Apr 19 '19 12:04

Adhy


People also ask

What is Paramiko SSHClient ()?

SSHClient. A high-level representation of a session with an SSH server. This class wraps Transport , Channel , and SFTPClient to take care of most aspects of authenticating and opening channels. A typical use case is: client = SSHClient() client.

How do I get command output in Paramiko?

You can get the output the command by using stdout. read() (returns a string) or stdout. readlines() (returns a list of lines).

What was the objective of using Paramiko module in Python?

It provides the foundation for the high-level SSH library Fabric, which is what we recommend you use for common client use-cases such as running remote shell commands or transferring files. Direct use of Paramiko itself is only intended for users who need advanced/low-level primitives or want to run an in-Python sshd.

Does Paramiko use OpenSSH?

Paramiko does not itself leverage OpenSSH-style config file directives, but it does implement a parser for the format, which users can honor themselves (and is used by higher-level libraries, such as Fabric).


1 Answers

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. So basically it's legitimate use is an implementation of an SSH terminal client. 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).

    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.

    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.

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

See Executing command using Paramiko exec_command on device is not working.


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

like image 124
Martin Prikryl Avatar answered Sep 30 '22 04:09

Martin Prikryl