paramiko中exec_command和invoke_shell方法都出现EOF错误

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了paramiko中exec_command和invoke_shell方法都出现EOF错误相关的知识,希望对你有一定的参考价值。

我正在尝试使用python paramiko从Windows机器在Windows计算机上的Linux服务器上执行命令,我同时使用了这两种方法

1.exec_command

2.invoke_shell

他们两个都给出EOF错误。

import paramiko
import time
def ConnectSSH1(host, port, username, password):
    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh.connect(host, int(port), username, password)
    chan = ssh.invoke_shell() # Throws EOF error here 
    #stdin, stdout, stderr = ssh.exec_command("ls")   #Throws EOF error here with exec_command
    chan.send("ls -a")
    time.sleep(5)
    output = chan.recv(9999)
    print (output)
    print (command)
    return (output)

exec_command中的错误

C:RobotFramework>python paramiko_solution1.py
Traceback (most recent call last):
  File "paramiko_solution1.py", line 15, in <module>
    chan.exec_command(command)
  File "C:Python37libsite-packagesparamikochannel.py", line 72, in _check
    return func(self, *args, **kwds)
  File "C:Python37libsite-packagesparamikochannel.py", line 257, in exec_command
    self._wait_for_event()
  File "C:Python37libsite-packagesparamikochannel.py", line 1226, in _wait_for_event
    raise e
  File "C:Python37libsite-packagesparamiko	ransport.py", line 2055, in run
    ptype, m = self.packetizer.read_message()
  File "C:Python37libsite-packagesparamikopacket.py", line 459, in read_message
    header = self.read_all(self.__block_size_in, check_rekey=True)
  File "C:Python37libsite-packagesparamikopacket.py", line 303, in read_all
    raise EOFError()
EOFError

invoke_shell命令中的错误

>>> channel.get_pty()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:Python37libsite-packagesparamikochannel.py", line 72, in _check
    return func(self, *args, **kwds)
  File "C:Python37libsite-packagesparamikochannel.py", line 203, in get_pty
    self._wait_for_event()
  File "C:Python37libsite-packagesparamikochannel.py", line 1226, in _wait_for_event
    raise e
  File "C:Python37libsite-packagesparamiko	ransport.py", line 2055, in run
    ptype, m = self.packetizer.read_message()
  File "C:Python37libsite-packagesparamikopacket.py", line 459, in read_message
    header = self.read_all(self.__block_size_in, check_rekey=True)
  File "C:Python37libsite-packagesparamikopacket.py", line 303, in read_all
    raise EOFError()
EOFError
>>>

[Parmakio日志

CRITICAL:root:None
DEBUG:paramiko.transport:starting thread (client mode): 0x8133c488
DEBUG:paramiko.transport:Local version/idstring: SSH-2.0-paramiko_2.7.1
DEBUG:paramiko.transport:Remote version/idstring: SSH-2.0-SSH
INFO:paramiko.transport:Connected (version 2.0, client SSH)
DEBUG:paramiko.transport:kex algos:['diffie-hellman-group-exchange-sha256', 'diffie-hellman-group1-sha1', 'diffie-hellman-group14-sha1'] server key:['ssh-rsa'] client encrypt:['aes128-ctr', 'aes192-ctr', 'aes256-ctr', 'aes128-cbc', 'blowfish-cbc', 'cast128-cbc', 'aes192-cbc', 'aes256-cbc', '3des-cbc', 'arcfour'] server encrypt:['aes128-ctr', 'aes192-ctr', 'aes256-ctr', 'aes128-cbc', 'blowfish-cbc', 'cast128-cbc', 'aes192-cbc', 'aes256-cbc', '3des-cbc', 'arcfour'] client mac:['hmac-md5', 'hmac-md5-96', 'hmac-sha1', 'hmac-sha1-96', 'hmac-sha2-256', 'hmac-sha2-512'] server mac:['hmac-md5', 'hmac-md5-96', 'hmac-sha1', 'hmac-sha1-96', 'hmac-sha2-256', 'hmac-sha2-512'] 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-ctr
DEBUG:paramiko.transport:MAC agreed: hmac-sha2-256
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 12.22.124.66: b'16d9d878229cssdcc837d33a64'
DEBUG:paramiko.transport:userauth is OK
INFO:paramiko.transport:Authentication (password) successful!
CRITICAL:root:None
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

请让我知道,我想念的是什么。

Paramiko版本是2.7.1

答案

在exec_command()中使用超时

SSHClient.exec_command(command,bufsize = -1,timeout = None,get_pty = False)在SSH服务器上执行命令。打开一个新的.Channel并执行所请求的命令。该命令的输入和输出流作为代表stdin,stdout和stderr的类似Python文件的对象返回。

参数:命令(str)–要执行的命令bufsize(int)–与Python中内置的file()函数的解释方式相同超时(int)–设置命令的通道超时。请参见Channel.settimeout.settimeout返回值:执行命令的标准输入,标准输出和标准错误,以3元组的形式显示]

引发SSHException:

如果服务器无法执行命令,则>

以上是关于paramiko中exec_command和invoke_shell方法都出现EOF错误的主要内容,如果未能解决你的问题,请参考以下文章

paramiko exec_command 返回 shell 脚本 exit 值

paramiko.SSHClient()的exec_command函数内部如何使用变量

paramiko.SSHClient()的exec_command函数内部如何使用变量

从 Paramiko SSH exec_command 连续获取输出

paramiko使用exec_command执行rm -rf删除目录的坑

使用Paramiko单独执行多个相关命令,并找出每个命令何时完成