Python SSH Paramiko EOFError

5
我有一个简单的脚本,可以通过SSH连接到诺基亚路由器并执行"show time"命令:
import paramiko
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('adres ip', port=22, username='username', password='password')
ssh_stdin, ssh_stdout, ssh_stderr = ssh.exec_command('show time')
output = stdout.readlines()
print '\n'.join(output)
ssh.close()

节点登录成功。我可以在路由器上看到自己,但是执行命令是不可行的。我会收到以下错误信息:

Traceback (most recent call last):   File "C:\Users\pkudalsk\Desktop\pyt.py", line 6, in <module>
    ssh_stdin, ssh_stdout, ssh_stderr = ssh.exec_command('show time')   File "C:\Users\pkudalsk\Desktop\paramiko\client.py", line 479, in exec_command

    chan.exec_command(command)   File "C:\Users\pkudalsk\Desktop\paramiko\channel.py", line 63, in _check
    return func(self, *args, **kwds)   File "C:\Users\pkudalsk\Desktop\paramiko\channel.py", line 241, in exec_comman d
    self._wait_for_event()   File "C:\Users\pkudalsk\Desktop\paramiko\channel.py", line 1198, in
_wait_for_ event
    raise e EOFError

有人知道是什么原因导致这个问题吗?我已经尝试使用Python 3.6和2.7,结果相同。

谢谢

1个回答

0

有2种解决方案:

1. 将此命令放置在代码中print('\n'.join(output)) 2. 如果问题仍然存在,则不要使用paramiko,请改用netmiko。我曾经在诺基亚路由器上遇到同样的问题,后来我换成了netmiko。这是一个例子:

from netmiko import ConnectHandler

device = {
    'device_type': 'nokia_sros',
    #'device_type': 'alcatel_sros',
    'ip': 'ip_add',
    'username': 'user',
    'password': 'password',
    'port': 22,
    'verbose': True
}

with ConnectHandler(**device) as net_connect:
    output = net_connect.send_command('show chassis detail')
    print(output)```
   

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接