用paramiko执行命令后EOFError的含义是什么?

时间:2018-02-14 14:24:55

标签: python python-3.x paramiko

我正在尝试编写一个SSH客户端,用于与我的LAB中的设备进行通信。 我使用paramiko模块连接到设备,似乎我设法连接到设备。 但是,我似乎无法将某个命令发送到设备。由于这些是我在这种应用程序(SSH与paramiko)中编写的第一行,我不知道我在哪里失败。

我将代码与Debug一起附加以获得专业人员的帮助

>>> import paramiko
>>> Client_235 = paramiko.SSHClient()
>>> Client_235.set_missing_host_key_policy(paramiko.AutoAddPolicy())
>>> Client_235.load_system_host_keys()
>>> Client_235.connect('50.0.0.235', username='admin', password='admin')
DEBUG:paramiko.transport:starting thread (client mode): 0x4abddd8
DEBUG:paramiko.transport:Local version/idstring: SSH-2.0-paramiko_2.4.0
DEBUG:paramiko.transport:Remote version/idstring: SSH-1.99-IPSSH-6.8.0
INFO:paramiko.transport:Connected (version 1.99, client IPSSH-6.8.0)
DEBUG:paramiko.transport:kex algos:['diffie-hellman-group14-sha1', 'diffie-hellman-group1-sha1', 'diffie-hellman-group-exchange-sha1', 'diffie-hellman-group-exchange-sha256'] server key:['ssh-rsa', 'ssh-dss'] client encrypt:['aes128-cbc', 'aes192-cbc', 'aes256-cbc', 'blowfish-cbc', 'cast128-cbc', '3des-cbc', 'des-cbc', 'des-cbc', 'arcfour128', 'arcfour'] server encrypt:['aes128-cbc', 'aes192-cbc', 'aes256-cbc', 'blowfish-cbc', 'cast128-cbc', '3des-cbc', 'des-cbc', 'des-cbc','arcfour128', 'arcfour'] client mac:['hmac-sha1', 'hmac-sha1-96', 'hmac-md5', 'hmac-md5-96'] server mac:['hmac-sha1', 'hmac-sha1-96', 'hmac-md5', 'hmac-md5-96'] client compress:['none'] server compress:['none'] client lang:[''] server lang:[''] kex follows?False
DEBUG:paramiko.transport:Kex agreed: diffie-hellman-group-exchange-sha256
DEBUG:paramiko.transport:HostKey agreed: ssh-rsa
DEBUG:paramiko.transport:Cipher agreed: aes128-cbc
DEBUG:paramiko.transport:MAC agreed: hmac-sha1
DEBUG:paramiko.transport:Compression agreed: none
DEBUG:paramiko.transport:Got server p (2048 bits)
DEBUG:paramiko.transport:kex engine KexGexSHA256 specified hash_algo <built-in function openssl_sha256>
DEBUG:paramiko.transport:Switch to new keys ...
DEBUG:paramiko.transport:Adding ssh-rsa host key for 50.0.0.235: b'9acccee326cb2423aba3216cee6f6ba9'
DEBUG:paramiko.transport:userauth is OK
INFO:paramiko.transport:Authentication (password) successful!
>>> stdin, stdout, stderr = Client_235.exec_command('c i eth i')
DEBUG:paramiko.transport:[chan 0] Max packet in: 32768 bytes
DEBUG:paramiko.transport:[chan 0] Max packet out: 32768 bytes
DEBUG:paramiko.transport:Secsh channel 0 opened.
DEBUG:paramiko.transport:EOF in transport thread
Traceback (most recent call last):  File "<stdin>", line 1, in <module> File "C:\Program Files\Python35\lib\site-packages\paramiko\client.py", line 8
6, in exec_command chan.exec_command(command)
File "C:\Program Files\Python35\lib\site-packages\paramiko\channel.py", line 63, in _check return func(self, *args, **kwds)
File "C:\Program Files\Python35\lib\site-packages\paramiko\channel.py", line 241, in exec_command self._wait_for_event()
File "C:\Program Files\Python35\lib\site-packages\paramiko\channel.py", line 1198, in _wait_for_event raise e
File "C:\Program Files\Python35\lib\site-packages\paramiko\transport.py", line 1872, in run ptype, m = self.packetizer.read_message()
File "C:\Program Files\Python35\lib\site-packages\paramiko\packet.py", line 426, in read_message header = self.read_all(self.__block_size_in, check_rekey=True)
File "C:\Program Files\Python35\lib\site-packages\paramiko\packet.py", line 276, in read_all raise EOFError()
EOFError
>>>

我做错了什么?通过SSH连接发送命令的正确方法是什么?

1 个答案:

答案 0 :(得分:3)

您的SSH服务器可能不支持或不允许exec_command()。您可以通过手动运行以下ssh命令(例如从shell)来验证这一点:

ssh user@host c i eth i
  

使用手动ssh user@host c i eth i “我得到以下”远程主机已关闭50.0.0.235的连接。“

你需要在paramiko中启动一个interative shell:

ssh = paramiko.SSHClient()
ssh.connect(...)
chan = ssh.invoke_shell()

然后您可以使用Channel.send()Channel.recv()等API来发送命令和获取输出。有关详细信息,请参阅paramiko关于Channel的文档。

(这是一个简单的example。)