2

I am trying to open an SFTP connection from an ipython session. However my code

import grequests
import pysftp

sftp = pysftp.Connection(
'ftp.server.com',
port=2255,
username='myname',
private_key='/Users/myname/.ssh/rsa') 

raises

Traceback (most recent call last):
  File "/Users/myname/reporting-python/lib/python2.7/site-packages/IPython/core/interactiveshell.py", line 3066, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-11-82bba0e75aee>", line 5, in <module>
    private_key='/Users/myname/.ssh/rsa') # TODO: adjust key path
  File "/Users/myname/reporting-python/lib/python2.7/site-packages/pysftp.py", line 187, in __init__
    self._transport.connect(username=username, pkey=prv_key)
  File "/Users/myname/reporting-python/lib/python2.7/site-packages/paramiko/transport.py", line 1072, in connect
    self.start_client()
  File "/Users/myname/reporting-python/lib/python2.7/site-packages/paramiko/transport.py", line 492, in start_client
    raise e
SSHException: Error reading SSH protocol bannerThis operation would block forever

Connecting to the server from OSX Terminal works without problems:

sftp -oPort=2255 -oUser=myname -oPubkeyAuthentication=yes -oIdentityFile=/Users/myname/.ssh/rsa ftp.server.com

I have already tried changing the banner timeout from 15 seconds to 60 secs in the transport.py, but it did not solve the problem.

paramiko.SSHException: Error reading SSH protocol banner suggests to inspect the banner, however I could not figure out how to use this information to solve the problem.

UPDATE:

It seems the problem is caused by importing the package grequests. If I do not import grequests, pysftp works as expected. The issue was raised before but has not been solved.

P. Magnusson
  • 82
  • 2
  • 9
malte
  • 559
  • 1
  • 8
  • 25

3 Answers3

3

The way I use paramiko to connect to SFTP server is as below.

from paramiko.client import SSHClient
from paramiko import AutoAddPolicy

client = SSHClient()
client.set_missing_host_key_policy(AutoAddPolicy())

client.connect(hostname=host,
               port=port,
               username=username,
               password=password)

sftp_handle = client.open_sftp()

I tried using pysftp earlier but paramiko proves to be more powerful and simple to use with extensive documentation. Let me know if it helps.

Abhinav
  • 992
  • 2
  • 11
  • 26
1

Add monkey patch to the main function, can solve the problem.

import grequests
import pysftp

###
from gevent import monkey
monkey.patch_all()
###

sftp = pysftp.Connection(
'ftp.server.com',
port=2255,
username='myname',
private_key='/Users/myname/.ssh/rsa') 
ezerear
  • 11
  • 1
0

as your answer:“I have already tried changing the banner timeout from 15 seconds to 60 secs in the transport.py, but it did not solve the problem.”

you can editor the transport.py,but not in the Installation directory, it is in the source code directory:

[root@localhost:/data/software/paramiko-1.9]# find . -name transport.py
./paramiko/transport.py
./build/lib/paramiko/transport.py

change banner_timeout like :

self.banner_timeout = 300

Then, reinstall paramiko ,can solve the problem.

jager
  • 1
  • 1