使用 paramiko 执行命令后出现 EOFError 是什么意思?

What is the meaning of the EOFError after I execute a command with paramiko?

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

为了得到专业人士的帮助,我附上了调试代码

>>> 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 连接发送命令的正确方法是什么?

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

ssh user@host c i eth i

With manual ssh user@host c i eth i "I get the following "Connection to 50.0.0.235 closed by remote host."

您需要在 paramiko 中开始互动 shell:

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

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

的文档

(这里是一个简单的。)