使用pysftp的EOFError

问题描述

我正在使用以下代码

host='xxx.xxxxxxxx.xxx'
username='xxxx'
password='xxxx'
port=5022
cnopts = pysftp.Cnopts()
cnopts.hostkeys = None
with pysftp.Connection(host=host,username=username,password=password,port=port,cnopts=cnopts) as sftp:
    print("hello")
    s=sftp.listdir()
    print(s)
    sftp.close()

它打印 hello 并且连接成功。 但是 sftp.listdir() 给出了 EOFError。 直接使用 Paramiko 也会出现同样的错误 如果使用不带 () 的 s=sftp.listdir 给出下面的打印 s:

我可以连接 Filezila 并获取列表。以下是日志:

DEB [20210712-12:24:44.671] thr=1   paramiko.transport: starting thread (client mode): 0x75b11c88  
DEB [20210712-12:24:44.681] thr=1   paramiko.transport: Local version/idstring: SSH-2.0-paramiko_2.7.2  
DEB [20210712-12:24:44.861] thr=1   paramiko.transport: Banner: Session Time-out  
DEB [20210712-12:24:45.065] thr=1   paramiko.transport: Remote version/idstring: SSH-2.0-SSH  
INF [20210712-12:24:45.065] thr=1   paramiko.transport: Connected (version 2.0,client SSH)    
DEB [20210712-12:24:45.066] thr=1   paramiko.transport: kex algos:['diffie-hellman-group-exchange-sha256','diffie-hellman-group-exchange-sha1','diffie-hellman-group14-sha1','diffie-hellman-group1-sha1'] server key:['rsa-sha2-256','ssh-rsa'] client encrypt:['aes128-ctr','aes192-ctr','aes256-ctr','aes128-cbc','aes192-cbc','aes256-cbc','3des-cbc','blowfish-cbc','cast128-cbc','arcfour'] server encrypt:['aes128-ctr','arcfour'] client mac:['hmac-sha2-256','hmac-sha2-512','hmac-sha1','hmac-md5'] server mac:['hmac-sha2-256','hmac-md5'] client compress:['none'] server compress:['none'] client lang:[''] server lang:[''] kex follows?False
DEB [20210712-12:24:45.076] thr=1   paramiko.transport: Kex agreed: diffie-hellman-group-exchange-sha256
DEB [20210712-12:24:45.076] thr=1   paramiko.transport: HostKey agreed: ssh-rsa
DEB [20210712-12:24:45.076] thr=1   paramiko.transport: Cipher agreed: aes128-ctr
DEB [20210712-12:24:45.076] thr=1   paramiko.transport: MAC agreed: hmac-sha2-256
DEB [20210712-12:24:45.076] thr=1   paramiko.transport: Compression agreed: none
DEB [20210712-12:24:45.380] thr=1   paramiko.transport: Got server p (2048 bits)
DEB [20210712-12:24:45.563] thr=1   paramiko.transport: kex engine KexGexSHA256 specified hash_algo <built-in function openssl_sha256>
DEB [20210712-12:24:45.685] thr=1   paramiko.transport: Switch to new keys ...
DEB [20210712-12:24:45.685] thr=2   paramiko.transport: Attempting password auth...
DEB [20210712-12:24:45.902] thr=1   paramiko.transport: userauth is OK
INF [20210712-12:24:46.186] thr=1   paramiko.transport: Authentication (password) successful!
DEB [20210712-12:24:46.186] thr=2   paramiko.transport: [chan 0] Max packet in: 32768 bytes
DEB [20210712-12:24:46.317] thr=1   paramiko.transport: [chan 0] Max packet out: 32768 bytes
DEB [20210712-12:24:46.317] thr=1   paramiko.transport: Secsh channel 0 opened.
DEB [20210712-12:24:46.387] thr=1   paramiko.transport: EOF in transport thread
DEB [20210712-12:25:01.353] thr=3   paramiko.transport: starting thread (client mode): 0x75c569c8
DEB [20210712-12:25:01.353] thr=3   paramiko.transport: Local version/idstring: SSH-2.0-paramiko_2.7.2
DEB [20210712-12:25:01.386] thr=3   paramiko.transport: Banner: Session Time-out
DEB [20210712-12:25:01.493] thr=3   paramiko.transport: Remote version/idstring: SSH-2.0-SSH
INF [20210712-12:25:01.493] thr=3   paramiko.transport: Connected (version 2.0,client SSH)
DEB [20210712-12:25:01.494] thr=3   paramiko.transport: kex algos:['diffie-hellman-group-exchange-sha256','hmac-md5'] client compress:['none'] server compress:['none'] client lang:[''] server lang:[''] kex follows?False
DEB [20210712-12:25:01.494] thr=3   paramiko.transport: Kex agreed: diffie-hellman-group-exchange-sha256
DEB [20210712-12:25:01.494] thr=3   paramiko.transport: HostKey agreed: ssh-rsa
DEB [20210712-12:25:01.494] thr=3   paramiko.transport: Cipher agreed: aes128-ctr
DEB [20210712-12:25:01.494] thr=3   paramiko.transport: MAC agreed: hmac-sha2-256
DEB [20210712-12:25:01.494] thr=3   paramiko.transport: Compression agreed: none
DEB [20210712-12:25:01.780] thr=3   paramiko.transport: Got server p (2048 bits)
DEB [20210712-12:25:01.899] thr=3   paramiko.transport: kex engine KexGexSHA256 specified hash_algo <built-in function openssl_sha256>
DEB [20210712-12:25:01.979] thr=3   paramiko.transport: Switch to new keys ...
DEB [20210712-12:25:01.979] thr=2   paramiko.transport: Attempting password auth...
DEB [20210712-12:25:02.058] thr=3   paramiko.transport: userauth is OK
INF [20210712-12:25:02.312] thr=3   paramiko.transport: Authentication (password) successful!
DEB [20210712-12:25:02.312] thr=3   paramiko.transport: EOF in transport thread

和 FileZilla 日志:

2021-07-12 11:26:30 15320 1 Status: Connecting to exxxx.xxxx.xx:5022...  
2021-07-12 11:26:30 15320 1 Response: fzSftp started,protocol_version=11  
2021-07-12 11:26:30 15320 1 Command: open "m******e@exxxx.xxxx.xx" 5022  
2021-07-12 11:26:31 15320 1 Status: Using username "m******e".   
2021-07-12 11:26:32 15320 1 Command: Pass: **********  
2021-07-12 11:26:33 15320 1 Status: Connected to exxxx.xxxx.xx  
2021-07-12 11:26:33 15320 1 Status: Retrieving directory listing...  
2021-07-12 11:26:33 15320 1 Command: pwd  
2021-07-12 11:26:33 15320 1 Response: Current directory is: "/home/shared/m******e"  
2021-07-12 11:26:33 15320 1 Command: ls  
2021-07-12 11:26:33 15320 1 Status: Listing directory /home/shared/m******e  
2021-07-12 11:26:33 15320 1 Status: Directory listing of "/home/shared/m******e" successful  
2021-07-12 11:27:02 15320 1 Status: disconnected from server  
2021-07-12 12:19:04 15320 1 Trace: CControlSocket::SendNextCommand()  
2021-07-12 12:19:04 15320 1 Trace: CSftpConnectOpData::Send() in state 0  
2021-07-12 12:19:04 15320 1 Status: Connecting to exxxx.xxxx.xx:5022...  
2021-07-12 12:19:04 15320 1 Trace: Going to execute C:\Program Files\FileZilla FTP Client\fzsftp.exe  
2021-07-12 12:19:04 15320 1 Response: fzSftp started,protocol_version=11  
2021-07-12 12:19:04 15320 1 Trace: CSftpConnectOpData::ParseResponse() in state 0  
2021-07-12 12:19:04 15320 1 Trace: CControlSocket::SendNextCommand()  
2021-07-12 12:19:04 15320 1 Trace: CSftpConnectOpData::Send() in state 3  
2021-07-12 12:19:04 15320 1 Command: open "m******e@exxxx.xxxx.xx" 5022  
2021-07-12 12:19:04 15320 1 Trace: Looking up host "exxxx.xxxx.xx" for SSH connection  
2021-07-12 12:19:04 15320 1 Trace: Connecting to 10.196.xxx.xxx port 5022  
2021-07-12 12:19:04 15320 1 Trace: We claim version: SSH-2.0-FileZilla_3.55.0  
2021-07-12 12:19:04 15320 1 Trace: Connected to 10.196.xxx.xxx  
2021-07-12 12:19:04 15320 1 Trace: Remote version: SSH-2.0-SSH  
2021-07-12 12:19:04 15320 1 Trace: Using SSH protocol version 2  
2021-07-12 12:19:04 15320 1 Trace: Doing Diffie-Hellman group exchange  
2021-07-12 12:19:04 15320 1 Trace: Doing Diffie-Hellman key exchange using 4096-bit modulus and hash SHA-256 (unaccelerated) with a server-supplied group  
2021-07-12 12:19:04 15320 1 Trace: Host key fingerprint is:  
2021-07-12 12:19:04 15320 1 Trace: ssh-rsa 1024 SHA256:K/CB5jPtDSHYhuVOZlmt+yJLrUMZVp1g1LQ7asnuCEU  
2021-07-12 12:19:04 15320 1 Trace: Initialised AES-256 SDCTR (AES-NI accelerated) outbound encryption  
2021-07-12 12:19:04 15320 1 Trace: Initialised HMAC-SHA-256 (unaccelerated) outbound MAC algorithm  
2021-07-12 12:19:05 15320 1 Trace: Initialised AES-256 SDCTR (AES-NI accelerated) inbound encryption  
2021-07-12 12:19:05 15320 1 Trace: Initialised HMAC-SHA-256 (unaccelerated) inbound MAC algorithm  
2021-07-12 12:19:05 15320 1 Status: Using username "m******e".   
2021-07-12 12:19:05 15320 1 Command: Pass: **********  
2021-07-12 12:19:05 15320 1 Trace: Sent password  
2021-07-12 12:19:05 15320 1 Trace: Access granted  
2021-07-12 12:19:05 15320 1 Trace: opening main session channel  
2021-07-12 12:19:05 15320 1 Trace: Opened main channel  
2021-07-12 12:19:05 15320 1 Status: Connected to exxxx.xxxx.xx  
2021-07-12 12:19:05 15320 1 Trace: Started a shell/command  
2021-07-12 12:19:05 15320 1 Trace: Remote working directory is /home/shared/m******e  
2021-07-12 12:19:05 15320 1 Trace: CSftpConnectOpData::ParseResponse() in state 3  
2021-07-12 12:19:05 15320 1 Trace: CControlSocket::Resetoperation(0)  
2021-07-12 12:19:05 15320 1 Trace: CSftpConnectOpData::Reset(0) in state 3  
2021-07-12 12:19:05 15320 1 Trace: CFileZillaEnginePrivate::Resetoperation(0)  
2021-07-12 12:19:05 15320 1 Trace: CControlSocket::SendNextCommand()  
2021-07-12 12:19:05 15320 1 Trace: CSftpListOpData::Send() in state 0  
2021-07-12 12:19:05 15320 1 Status: Retrieving directory listing...  
2021-07-12 12:19:05 15320 1 Trace: CSftpChangeDirOpData::Send() in state 0  
2021-07-12 12:19:05 15320 1 Trace: CSftpChangeDirOpData::Send() in state 1  
2021-07-12 12:19:05 15320 1 Command: pwd  
2021-07-12 12:19:05 15320 1 Response: Current directory is: "/home/shared/m******e"  
2021-07-12 12:19:05 15320 1 Trace: CSftpChangeDirOpData::ParseResponse() in state 1  
2021-07-12 12:19:05 15320 1 Trace: CControlSocket::Resetoperation(0)  
2021-07-12 12:19:05 15320 1 Trace: CSftpChangeDirOpData::Reset(0) in state 1  
2021-07-12 12:19:05 15320 1 Trace: CSftpListOpData::SubcommandResult(0) in state 1  
2021-07-12 12:19:05 15320 1 Trace: CControlSocket::SendNextCommand()  
2021-07-12 12:19:05 15320 1 Trace: CSftpListOpData::Send() in state 2  
2021-07-12 12:19:05 15320 1 Trace: CSftpListOpData::Send() in state 3  
2021-07-12 12:19:05 15320 1 Command: ls  
2021-07-12 12:19:05 15320 1 Status: Listing directory /home/shared/m******e  
2021-07-12 12:19:06 15320 1 Trace: CSftpListOpData::ParseResponse() in state 3  
2021-07-12 12:19:06 15320 1 Trace: CControlSocket::Resetoperation(0)  
2021-07-12 12:19:06 15320 1 Trace: CSftpListOpData::Reset(0) in state 3  
2021-07-12 12:19:06 15320 1 Status: Directory listing of "/home/shared/m******e" successful  
2021-07-12 12:19:06 15320 1 Trace: CFileZillaEnginePrivate::Resetoperation(0)  
2021-07-12 12:19:57 15320 1 Trace: CControlSocket::DoClose(66)  
2021-07-12 12:19:57 15320 1 Trace: CControlSocket::Resetoperation(66)  
2021-07-12 12:19:57 15320 1 Trace: CFileZillaEnginePrivate::Resetoperation(66)  
2021-07-12 12:19:57 15320 1 Trace: CControlSocket::DoClose(66)  
2021-07-12 12:19:57 15320 1 Trace: CControlSocket::Resetoperation(66)  
2021-07-12 12:19:57 15320 1 Trace: CFileZillaEnginePrivate::Resetoperation(66)  
Traceback (most recent call last):  
  File "<ipython-input-11-f9249d66c05f>",line 4,in <module>
    s=sftp.listdir()
  File "C:\ProgramData\Anaconda3\lib\site-packages\pysftp\__init__.py",line 591,in listdir
    self._sftp_connect()
  File "C:\ProgramData\Anaconda3\lib\site-packages\pysftp\__init__.py",line 205,in _sftp_connect
    self._sftp = paramiko.SFTPClient.from_transport(self._transport)
  File "C:\ProgramData\Anaconda3\lib\site-packages\paramiko\sftp_client.py",line 169,in from_transport
    chan.invoke_subsystem("sftp")
  File "C:\ProgramData\Anaconda3\lib\site-packages\paramiko\channel.py",line 72,in _check
    return func(self,*args,**kwds)
  File "C:\ProgramData\Anaconda3\lib\site-packages\paramiko\channel.py",line 283,in invoke_subsystem
    self._wait_for_event()
  File "C:\ProgramData\Anaconda3\lib\site-packages\paramiko\channel.py",line 1226,in _wait_for_event
    raise e
  File "C:\ProgramData\Anaconda3\lib\site-packages\paramiko\transport.py",line 2055,in run
    ptype,m = self.packetizer.read_message()
  File "C:\ProgramData\Anaconda3\lib\site-packages\paramiko\packet.py",line 459,in read_message
    header = self.read_all(self.__block_size_in,check_rekey=True)
  File "C:\ProgramData\Anaconda3\lib\site-packages\paramiko\packet.py",line 303,in read_all
    raise EOFError()
EOFError

并返回错误

EOFError                                  Traceback (most recent call last)
<ipython-input-14-cf0db09999d7> in <module>
      2 
      3         print("hello")
----> 4         s=sftp.listdir()
      5         print(s)
      6         sftp.close()

C:\ProgramData\Anaconda3\lib\site-packages\pysftp\__init__.py in listdir(self,remotepath)
    589 
    590         """
--> 591         self._sftp_connect()
    592         return sorted(self._sftp.listdir(remotepath))
    593 

C:\ProgramData\Anaconda3\lib\site-packages\pysftp\__init__.py in _sftp_connect(self)
    203         """Establish the SFTP connection."""
    204         if not self._sftp_live:
--> 205             self._sftp = paramiko.SFTPClient.from_transport(self._transport)
    206             if self._default_path is not None:
    207                 # print("_default_path: [%s]" % self._default_path)

C:\ProgramData\Anaconda3\lib\site-packages\paramiko\sftp_client.py in from_transport(cls,t,window_size,max_packet_size)
    167         if chan is None:
    168             return None
--> 169         chan.invoke_subsystem("sftp")
    170         return cls(chan)
    171 

C:\ProgramData\Anaconda3\lib\site-packages\paramiko\channel.py in _check(self,**kwds)
     70         ):
     71             raise SSHException("Channel is not open")
---> 72         return func(self,**kwds)
     73 
     74     return _check

C:\ProgramData\Anaconda3\lib\site-packages\paramiko\channel.py in invoke_subsystem(self,subsystem)
    281         self._event_pending()
    282         self.transport._send_user_message(m)
--> 283         self._wait_for_event()
    284 
    285     @open_only

C:\ProgramData\Anaconda3\lib\site-packages\paramiko\channel.py in _wait_for_event(self)
   1224         if e is None:
   1225             e = SSHException("Channel closed.")
-> 1226         raise e
   1227 
   1228     def _set_closed(self):

C:\ProgramData\Anaconda3\lib\site-packages\paramiko\transport.py in run(self)
   2053                         self._send_kex_init()
   2054                     try:
-> 2055                         ptype,m = self.packetizer.read_message()
   2056                     except NeedRekeyException:
   2057                         continue

C:\ProgramData\Anaconda3\lib\site-packages\paramiko\packet.py in read_message(self)
    457         :raises: `.NeedRekeyException` -- if the transport should rekey
    458         """
--> 459         header = self.read_all(self.__block_size_in,check_rekey=True)
    460         if self.__etm_in:
    461             packet_size = struct.unpack(">I",header[:4])[0]

C:\ProgramData\Anaconda3\lib\site-packages\paramiko\packet.py in read_all(self,n,check_rekey)
    301                 x = self.__socket.recv(n)
    302                 if len(x) == 0:
--> 303                     raise EOFError()
    304                 out += x
    305                 n -= len(x)

EOFError: 

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)