Executing Command Using Paramiko Exec_Command on Device Is Not Working

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

You seem to have this problem:

Executing command using Paramiko exec_command on device is not working

But as you need to use low-level Transport API because your specific authentication needs, you have to use a more low-level code (basically what SSHClient.invoke_shell does internally):

channel = transport.open_session()
channel.get_pty() # Might not be necessary
channel.invoke_shell()

channel.send('ls\n')
channel.send('exit\n')

Executing command using Paramiko exec_command on device is not working

If the SSHClient.exec_command does not work, the first thing to test is to try (on one line):

ssh user@host command

That will use the same SSH API (the "exec" channel) as SSHClient.exec_command. If you are on Windows, you can use plink (from PuTTY packages) instead of ssh. If ssh/plink fails too, it indicates that your device does not support the SSH "exec" channel.


In your case, it seems that the "exec" channel on Brocade SSH server is implemented to support the scp command only.

As you claim to be able to "SSH" to the switch, it seems that the "shell" channel is fully working.

While it is generally not recommended to use the "shell" channel for command automation, with your server you won't have other option. Use the SSHClient.invoke_shell and write the commands to the channel (= to the shell) using the Channel.send.

channel = ssh.invoke_shell()
channel.send('ls\n')
channel.send('exit\n')

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

A similar question on C#/SSH.NET: SSH.NET is not executing command on device.


Obligatory warning: Do not use AutoAddPolicy – You are losing a protection against MITM attacks by doing so. For a correct solution, see Paramiko "Unknown Server".

Command executed with Paramiko does not produce any output

If you get no output on stdout, it is usually because the command fails to start.

Read stderr to check for any errors.

print(stderr.readlines())

Quite often the error is "<command> not found". For that see

Some Unix commands fail with "<command> not found", when executed using Python Paramiko exec_command


If you are connecting to a device, see also Executing command using Paramiko exec_command on device is not working.

paramiko not executing basic command on machine

The server might not be allowing exec_command()

Try using the interactive shell

ssh_client = paramiko.SSHClient()
shh_client.connect(#creds)
shell = ssh_client.invoke_shell()

You can now use shell.send() and shell.recv() to execute commands and get back their outputs

http://docs.paramiko.org/en/2.7/api/channel.html#paramiko.channel.Channel.send
http://docs.paramiko.org/en/2.7/api/channel.html#paramiko.channel.Channel.recv

Example: https://www.semicolonworld.com/question/56794/implement-an-interactive-shell-over-ssh-in-python-using-paramiko

Unable to send AT commands trough Paramiko SSH

Thanks Martin Prikryl !

So I looked at the link you sent me and it worked:
Executing command using Paramiko exec_channel on device is not working.

So I changed my code to use a shell and send my commands through it.
Here is my code

shell = client.invoke_shell()  
shell.send('AT*GNSSSTATUS? \r')

Thank you very much and have a nice day

A command does not finish when executed using Python Paramiko exec_command

You read only one line of the output.

logging.debug(f'ssh_stdout: {ssh_stdout.readline()}')

If the remote program produces lot of output, as soon as its output buffer fills in, the program hangs on the next attempt to write some output.


If you want the program to finish, you have to keep reading the output.

The simplest way is to use readlines or read:

print(stdout.read())

But that's inefficient for large outputs like yours.

Instead you can read the output line by line:

for line in stdout:
print(line.strip())

It gets more complicated, when the commands produces also an error output, as then you have to read both output streams.
See Paramiko ssh die/hang with big output.

And you should check the error output in any case, for good error handling. See also:

Command executed with Paramiko does not produce any output



Related Topics



Leave a reply



Submit