python - 使用 paramiko 执行命令后,EOFError 的含义是什么?

标签 python python-3.x paramiko

我正在尝试编写一个 SSH 客户端,用于与我实验室中的设备进行通信。 我使用 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 的文档.

(这是一个简单的 example 。)

关于python - 使用 paramiko 执行命令后,EOFError 的含义是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48789499/

相关文章:

python - 我是否需要 Google App Engine 来制作和运行 Facebook 应用程序?

python - 如何为 n 个列表创建叉积元组?

python - celery + SQS - pycurl错误

python - 在本地调用 open_sftp() 和通过单独的函数调用有什么区别?

python - 使用 Python 从 windows 到 unix 的 sftp

python - 将Python项目集成到Xcode中

python - 如何在 Seaborn distplot 中绘制 Pandas 日期时间序列?

python - 如何让 XKCD 字体在 matplotlib 中工作

python - Sklearn 管道抛出 ValueError : too many values to unpack (expected 2)

python - 为 paramiko session 设置 session 变量