paramiko.exec_command() 未执行并返回“在 CLI 中找到的额外参数"

问题描述

我正在尝试使用 Paramiko 对服务器进行 ssh 并执行命令.但是 paramiko.exec_command() 返回错误.为什么会这样?

I am trying to ssh a server using Paramiko and execute a command. But the paramiko.exec_command() returns with an error.Why is this happening?

这是我的 Python 脚本:

This is my Python script:

import paramiko

ssh = paramiko.SSHClient()
ssh.load_system_host_keys()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('10.126.141.132', username='usrm', password='passwd')

stdin, stdout, stderr = ssh.exec_command("show chassis")

print(stdout.readlines())

ssh.close()

执行时它会返回以下消息:

When executed it returns with this message:

['在 CLI 中发现额外参数,不支持,退出 CLI 会话: ']

['Extra params found in CLI, this is not supported, exiting the CLI session: ']

我在 Windows 上使用 Python 3.5.2 :: Anaconda 4.1.1(64 位)和 Paramiko.

I am using Python 3.5.2 :: Anaconda 4.1.1 (64-bit) with Paramiko on Windows.

我已经手动尝试了这些命令并且它正在工作.

I have tried the commands manually and it is working.


解决方案

根据您的最新评论:

我安装了 Cygwin 终端并使用命令对服务器进行 SSH...它出现了 Extra params 错误.我执行的命令:ssh usrm@10.126.141.132 "show chassis",输出:No entry for terminal type "dumb";使用哑终端设置.在 CLI 中发现额外参数,不支持,退出 CLI 会话:

I installed a Cygwin Terminal and SSH'd the server with the command...it came up with the Extra params error. Command I executed: ssh usrm@10.126.141.132 "show chassis", Output: No entry for terminal type "dumb"; using dumb terminal settings. Extra params found in CLI, this is not supported, exiting the CLI session:

好像usrm账号在SSH服务器上的login shell不允许以非交互方式运行命令.要解决问题,您必须像这样使用 invoke_shell():

it sounds like the usrm account's login shell on the SSH server is not allowed to run commands in the non-interactive way. To solve the problem you have to use invoke_shell() like this:

chan = ssh.invoke_shell()
chan.sendall('show chassis')
s = chan.recv(4096)
print s

相关文章