3

I need some help I'm getting below message when i try to connect. The script Im executing is below. Any suggestions as to how I can fix this issue?

Also, once I get this issue fixed Im looking to add to this script the ability to listen until the result file is available and to download it from a different remote directory.

Thank you, in advance

No hostkey for host mysftpserver.com found.
Exception AttributeError: "'Connection' object has no attribute 
'_sftp_live'" in <bound method Connection.__del__ of <pysftp.Connection 
object at 0x101d8cd50>> ignored

Process finished with exit code 0

Script

import pysftp as sftp

def sftpExample():

    try:
        s = sftp.Connection(host='mysftpserver', port=1211, username='myusername', password='mypassword')

        remotepath='/Orders/incoming/myfile.csv'
        localpath='/Users/myusername/projects/order88.csv'
        s.put(localpath,remotepath)

        s.close()

    except Exception, e:
        print str(e)

sftpExample()
rookievmc
  • 173
  • 2
  • 2
  • 13

2 Answers2

11

The pysftp documentation and other answers on Stack Overflow also seem to suggest that the error can be resolved by setting cnopts.hostkeys = None.

So the full code would be something like:

import pysftp
def sftpExample():
    cnopts = pysftp.CnOpts()
    cnopts.hostkeys = None  
    with pysftp.Connection('hostname', username='me', password='secret', cnopts=cnopts) as sftp:
        sftp.put('/Users/myusername/projects/order88.csv','/Orders/incoming/myfile.csv')  # upload file to public/ on remote
        sftp.close()

sftpExample()

Hope it helps!

cosinepenguin
  • 1,545
  • 1
  • 12
  • 21
  • 3
    Please do **NOT** use this answer. This removes identification security for SSH. Check the official documentation and use the `ssh` command or manually place the server's host key in your `~./ssh/known_hosts` file. – djsumdog Jul 01 '17 at 03:37
  • As it says at the official documentation [https://pysftp.readthedocs.io/en/release_0.2.9/][https://pysftp.readthedocs.io/en/release_0.2.9/] just keep it simple and try this (adapt it to your use of case): ``` import pysftp with pysftp.Connection('hostname', username='me', password='secret') as sftp: with sftp.cd('public'): # temporarily chdir to public sftp.put('/my/local/filename') # upload file to public/ on remote sftp.get('remote_file') # get a remote file ``` It worked for me! – FLBKernel Sep 16 '20 at 11:41
0

Per the pysftp documentation you need to have the remote host key in your ~/.ssh/known_hosts. You can do this by connecting to the server via the ssh command (it will ask you if you want to add a new key to your hosts file; simply type yes if you trust the remote host's fingerprint).

The official documentation also points out how to disable host checking entirely, but this is NOT recommended as it would defeat critical security features for SSH identification.

djsumdog
  • 2,560
  • 1
  • 29
  • 55
  • I did try it out but I would still get an error. I tried import pysftp as sftp cnopts = pysftp.CnOpts() cnopts.hostkeys = None s = sftp.Connection(host=, port=, username=, password='', cnopts=cnopts) I get below error cnopts = pysftp.CnOpts() NameError: name 'pysftp' is not defined – rookievmc Jul 01 '17 at 02:11
  • When you say `import pysftp as sftp`, you are importing the module under a different name. So you would then say `cnopts = sftp.CnOpts()`, because you changed the name on import! – cosinepenguin Jul 01 '17 at 02:25
  • 1
    I also edited my answer above if that helps... All of the other answers I've seen use `with pysftp.Connection(...)` instead of `s=pysftp.Connection(...)`, so that might be a factor as well! – cosinepenguin Jul 01 '17 at 02:27