简体   繁体   中英

SFTP connection failed socket.timeout?

I am attempting to connect to one of our SFTP servers in order to automate a report. We pull a file from this server once a week and I am not able to get this connections to work. I am not finding any useful references to the below traceback error. Anyone have an idea of what could be causing this problem?

I get a socket.timeout error after 2 or 3 seconds of executing the code.

import paramiko


cli = paramiko.SSHClient()

cli.set_missing_host_key_policy(paramiko.AutoAddPolicy())
cli.connect(hostname='AHOST', port=21, username="USER", password="PASS")

stdin_, stdout_, stderr_ = cli.exec_command("ls -l ~")
print(stdout_.readlines())

cli.close()

Traceback:

Exception: Error reading SSH protocol banner
Traceback (most recent call last):
  File "C:\Users\user_name\Desktop\Python 3.6.2\lib\site-packages\paramiko\transport.py", line 2138, in _check_banner
    buf = self.packetizer.readline(timeout)
  File "C:\Users\user_name\Desktop\Python 3.6.2\lib\site-packages\paramiko\packet.py", line 367, in readline
    buf += self._read_timeout(timeout)
  File "C:\Users\user_name\Desktop\Python 3.6.2\lib\site-packages\paramiko\packet.py", line 576, in _read_timeout
    raise socket.timeout()
socket.timeout

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\user_name\Desktop\Python 3.6.2\lib\site-packages\paramiko\transport.py", line 1966, in run
    self._check_banner()
  File "C:\Users\user_name\Desktop\Python 3.6.2\lib\site-packages\paramiko\transport.py", line 2143, in _check_banner
    "Error reading SSH protocol banner" + str(e)
paramiko.ssh_exception.SSHException: Error reading SSH protocol banner

Traceback (most recent call last):
  File "C:\Users\user_name\Desktop\Python 3.6.2\lib\site-packages\paramiko\transport.py", line 2138, in _check_banner
    buf = self.packetizer.readline(timeout)
  File "C:\Users\user_name\Desktop\Python 3.6.2\lib\site-packages\paramiko\packet.py", line 367, in readline
    buf += self._read_timeout(timeout)
  File "C:\Users\user_name\Desktop\Python 3.6.2\lib\site-packages\paramiko\packet.py", line 576, in _read_timeout
    raise socket.timeout()
socket.timeout

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:/Users/user_name/PycharmProjects/PDD_Report/MAIN/MAIN.py", line 12, in <module>
    cli.connect(hostname='A40T', port=21, username="user_name", password="gate001")
  File "C:\Users\user_name\Desktop\Python 3.6.2\lib\site-packages\paramiko\client.py", line 397, in connect
    t.start_client(timeout=timeout)
  File "C:\Users\user_name\Desktop\Python 3.6.2\lib\site-packages\paramiko\transport.py", line 587, in start_client
    raise e
  File "C:\Users\user_name\Desktop\Python 3.6.2\lib\site-packages\paramiko\transport.py", line 1966, in run
    self._check_banner()
  File "C:\Users\user_name\Desktop\Python 3.6.2\lib\site-packages\paramiko\transport.py", line 2143, in _check_banner
    "Error reading SSH protocol banner" + str(e)
paramiko.ssh_exception.SSHException: Error reading SSH protocol banner

You are connecting to FTP port 21.

SSH/SFTP uses port 22 (what is the default value of the port parameter, which you override).


Side note 1: Your question is about SFTP. Yet, instead of using SFTPClient , you use SSHClient and execute shell commands – That has nothing to do with SFTP.


Side note 2: Do not use AutoAddPolicy like this. You lose security by doing so.
See Paramiko "Unknown Server" .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM