-->

Executing command using Paramiko on Brocade switch

2020-02-06 11:20发布

问题:

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?

回答1:

The "exec" channel on Brocade SSH server seems to be implemented to support the scp command only. So you cannot use your code with the SSHClient.exec_command.

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?