Python Pysftp Error
Asked Answered
J

4

7

My code:

import pysftp 
s = pysftp.Connection(host='test.rebex.net', username='demo', password='password') 
data = s.listdir() 
s.close() 
for i in data: 
    print i

I'm getting an error trying to connect to a SFTP server using pysftp.

This should be straight forward enough but I get the error below:

Traceback (most recent call last):
  File "/Users/gavinhinfey/Documents/Python Files/sftp_test.py", line 3, in <module>
    s = pysftp.Connection(host='test.rebex.net', username='demo', password='password')
  File "build/bdist.macosx-10.6-intel/egg/pysftp.py", line 55, in __init__
  File "build/bdist.macosx-10.5-intel/egg/paramiko/transport.py", line 303, in __init__
paramiko.SSHException: Unable to connect to test.rebex.net: [Errno 60] Operation timed out
Exception AttributeError: "'Connection' object has no attribute '_tranport_live'" in <bound     method Connection.__del__ of <pysftp.Connection object at 0x101a5a810>> ignored

I've tried using different versions of python (mostly 2.7), I have all dependencies installed and I tried numerous sftp connections. I'm using OS X 10.9.1.

Journeywork answered 13/1, 2014 at 13:48 Comment(7)
sorry the code input code is import pysftp s = pysftp.Connection(host='test.rebex.net', username='demo', password='password') data = s.listdir() s.close() for i in data: print iJourneywork
are you sure your host is correct and your ports aren't blocked? your error log is telling you that your connection to host timed out, after which the s object is not initiated and throws normal errors for an object that failed to initiate.Beltz
I can connect with File Zilla with the same details? Does this mean that my ports are not blocked?Journeywork
Strange. Maybe try log=True in your kwargs for Connection and post what you get there.Beltz
Sorry but when I add log=True where does it output the log? ThanksJourneywork
When you use File Zilla, which protocol are you using? FTP, FTPS, or SFTP?Rohrer
I just tried using pythons builtin ftplib and it works. It appears to be FTP not SFTP that I needed. My apologies.Journeywork
D
6

That initial error appears to be a problem connecting with the remote server (SSHException). The second (AttributeError), is from a bug in the code that occurs when the connection fails. It is fixed in the latest version of pysftp

https://pypi.python.org/pypi/pysftp

pip install -U pysftp

is your friend.

Deliberative answered 15/5, 2014 at 17:39 Comment(1)
Python 2.7's pip says: no such option: -u.Merola
S
15

updating the package didn't work for me, as it was already up-to-date (latest for python 2.7 at least)

Found a better aproach here.

1) You can manualy add the ssh key to the known_hosts file

ssh test.rebex.net

2) Or you can set a flag to ignore it

import pysftp
cnopts = pysftp.CnOpts()
cnopts.hostkeys = None    # disable host key checking.
with pysftp.Connection('host', username='me',private_key=private_key,
                           private_key_pass=private_key_password,
                           cnopts=cnopts) as sftp
    # do stuff here
Sogdiana answered 13/7, 2016 at 14:53 Comment(1)
Never use the option 2), unless you do not care about security!Loki
D
6

That initial error appears to be a problem connecting with the remote server (SSHException). The second (AttributeError), is from a bug in the code that occurs when the connection fails. It is fixed in the latest version of pysftp

https://pypi.python.org/pypi/pysftp

pip install -U pysftp

is your friend.

Deliberative answered 15/5, 2014 at 17:39 Comment(1)
Python 2.7's pip says: no such option: -u.Merola
A
0

@Martin.Prikryl: setting hostkeys = None is very useful in the initial stage of coding with pysftp. Debugging a program that keeps failing for a known exception hides other problems that need attention--like making an actual connection. I can deal with the 'man in the middle' problem later once I know my code is actually working correctly.

@All: The current pysftp.CnOpts() object appears to have a bug:

cnopts = pysftp.CnOpts()
cnopts.hostkeys = None

The above code does not prevent host key checking.

python getfile_v3.py --help Traceback (most recent call last): File "getfile_v3.py", line 9, in cnopts = pysftp.CnOpts() File "c:\Program Files\Python\Python38\lib\site-packages\pysftp_init_.py", line 64, in init raise HostKeysException('No Host Keys Found') pysftp.exceptions.HostKeysException: No Host Keys Found

The second line doesn't get executed because the first does the host key check by default. If I set the key with:

      cnopts = pysftp.CnOpts(hostkeys=None)

the same error results.

It appears that 'hostkeys' has been deprecated, and there is no way to disable the host key check.

Joe White

Autocatalysis answered 4/2, 2021 at 16:21 Comment(1)
This answer is not answering the original question. Maybe try to do a bug report or ask another question?Verisimilitude
H
0

Workaround for SSHException: No hostkey for host test.rebex.net found

just to add it manually via ssh

  1. ssh [email protected]
  2. it will ask for password, you need to enter "password"
  3. it will show you the message
The authenticity of host 'test.rebex.net (ip-adress)' can't be established.
ECDSA key fingerprint is SHA256:OzvpQxxxV9F/ECMXbQ7B7zbKxxxxUno65c.
Are you sure you want to continue connecting (yes/no/[fingerprint])?
  1. confirm action -> type "yes"
  2. done, you will get a message, since now your sftp connection to 'test.rebex.net' should work

Warning: Permanently added 'test.rebex.net,ip' (ECDSA) to the list of known hosts.

Himalayas answered 15/7, 2022 at 10:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.