在设备上使用 Paramiko exec_command 执行命令无效
Executing command using Paramiko exec_command on device is not working
我正在尝试使用 Paramiko 通过 SSH 连接到 Brocade 交换机并执行远程命令。代码如下:
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')
在尝试 运行 代码时,我遇到了一个奇怪的错误,如下所示。
Protocol error, doesn't start with scp!
不知道错误原因,也不知道SSH连接是否成功。你能帮我解决这个问题吗?
如果SSHClient.exec_command
不起作用,首先要测试的是尝试(在一个行):
ssh user@host command
这将使用与 SSHClient.exec_command
相同的 SSH API(“exec”通道)。如果您使用 Windows,则可以使用 plink
(来自 PuTTY 包)而不是 ssh
。如果 ssh
/plink
也失败,则表示您的设备不支持 SSH "exec" 通道。
在您的情况下,Brocade SSH 服务器上的“exec”通道似乎仅支持 scp
命令。
正如您声称能够通过“SSH”连接到交换机,“shell”通道似乎完全正常工作。
虽然通常不建议使用“shell”通道进行命令自动化,但对于您的服务器,您没有其他选择。使用 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')
另见
关于 C#/SSH.NET 的类似问题: .
强制性警告:请勿使用 AutoAddPolicy
– 您正在失去针对 MITM attacks by doing so. For a correct solution, see Paramiko "Unknown Server".
的保护
我正在尝试使用 Paramiko 通过 SSH 连接到 Brocade 交换机并执行远程命令。代码如下:
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')
在尝试 运行 代码时,我遇到了一个奇怪的错误,如下所示。
Protocol error, doesn't start with scp!
不知道错误原因,也不知道SSH连接是否成功。你能帮我解决这个问题吗?
如果SSHClient.exec_command
不起作用,首先要测试的是尝试(在一个行):
ssh user@host command
这将使用与 SSHClient.exec_command
相同的 SSH API(“exec”通道)。如果您使用 Windows,则可以使用 plink
(来自 PuTTY 包)而不是 ssh
。如果 ssh
/plink
也失败,则表示您的设备不支持 SSH "exec" 通道。
在您的情况下,Brocade SSH 服务器上的“exec”通道似乎仅支持 scp
命令。
正如您声称能够通过“SSH”连接到交换机,“shell”通道似乎完全正常工作。
虽然通常不建议使用“shell”通道进行命令自动化,但对于您的服务器,您没有其他选择。使用 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')
另见
关于 C#/SSH.NET 的类似问题:
强制性警告:请勿使用 AutoAddPolicy
– 您正在失去针对 MITM attacks by doing so. For a correct solution, see Paramiko "Unknown Server".