Executing command using Paramiko exec_command on device is not working

I am trying to use Paramiko to SSH into a Brocade switch and carry out remote commands. The code is as given below:

def ssh_connector(ip, userName, passWord, command):
 ssh = paramiko.SSHClient()
 ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
 ssh.connect(ip, username=userName, password=passWord, port=22)
 stdin, stdout, stderr = ssh.exec_command(command)
 print stdout.readlines()

ssh_connector(ip, userName, passWord, 'show running-config')

While trying to run the code, I encounter a strange error which is as given below.

Protocol error, doesn’t start with scp!

I do not know the cause of the error or whether the SSH connection was successful. Could you please help me with this?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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

ssh <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2356504651634b4c5057">[email protected]</a> 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('lsn')
channel.send('exitn')

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


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x