0

I've been digging around and have tried several solutions but can't get anything to work. I'm trying to run a python script to download a specific folder via SFTP. I've been trying to follow the instructions here: https://pysftp.readthedocs.io/en/release_0.2.9/ but having no luck when I run this:

import pysftp

with pysftp.Connection(host="sftp.url.com",username="username",password="password",port="##",private_key="C:\location\location"):
    with sftp.cd('public'):
        sftp.get('/todaysdate')

Want to be able to grab a single file and download it to a local folder.

Thanks in advance and apologies if this is a basic question, been doing lots of digging but haven't had any luck finding a solution.

Here is the error message I am receiving:

Warning (from warnings module):
  File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\site-packages\pysftp\__init__.py", line 61
    warnings.warn(wmsg, UserWarning)
UserWarning: Failed to load HostKeys from M:\\.ssh\known_hosts.  You will need to explicitly load HostKeys (cnopts.hostkeys.load(filename)) or disableHostKey checking (cnopts.hostkeys = None).
Traceback (most recent call last):
  File "C:\BBG API\keytest2.py", line 3, in <module>
    with pysftp.Connection(host="sftp.url.com",username="user",password="pass",port="##",private_key="C:/CSV/filename"):
  File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\site-packages\pysftp\__init__.py", line 132, in __init__
    self._tconnect['hostkey'] = self._cnopts.get_hostkey(host)
  File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\site-packages\pysftp\__init__.py", line 71, in get_hostkey
    raise SSHException("No hostkey for host %s found." % host)
paramiko.ssh_exception.SSHException: No hostkey for host sftp.trafix.com found.
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
thefinland
  • 65
  • 2
  • 6
  • How does this "no luck" manifest itself? With *exactly* which error message or exception? And are you sure the `/` on the filename is appropriate? Usually, that tells the server to ignore your `cd` and go back to the root filesystem. – Charles Duffy Jan 15 '19 at 23:19
  • It's helpful to show the specific "several solutions" you tried and the specific ways they didn't work, btw -- that way we have more context with which to diagnose. – Charles Duffy Jan 15 '19 at 23:21
  • Apologies, will make sure to make note of what I attempt in the future rather than overwrite. I added the error message I'm receiving into the body ^ – thefinland Jan 16 '19 at 00:14
  • Okay, so that warning tells you that the known_hosts list isn't in the expected format, and the error then tells you that it's failing because it couldn't find a known_hosts entry. Those two things seem related, no? :) – Charles Duffy Jan 16 '19 at 00:24
  • ...so, a place to start would be looking at the format of your `known_hosts` file. – Charles Duffy Jan 16 '19 at 00:26
  • Sorry if this is a silly question but I can't find the known_hosts file/wondering if I need to generate one? Is this something filezilla would have generated when I accessed the server that way? – thefinland Jan 16 '19 at 00:33
  • Ahh. So, different ssh/sftp servers use different file formats, so the issue here is pretty much certainly that filezilla is storing your known_hosts in a different way than this Python library expects. – Charles Duffy Jan 16 '19 at 00:47
  • ...I wouldn't be surprised if Filezilla is storing that data in the Windows registry rather than on-disk at all. – Charles Duffy Jan 16 '19 at 00:48
  • See my answer to [Verify host key with pysftp](https://stackoverflow.com/q/38939454/850848#43389508) - Do not use the most voted answer if you care about security. – Martin Prikryl Jan 16 '19 at 06:57

1 Answers1

0

A couple of things. I see you are on Windows so have you tried WinSCP to ensure you can definitely download the file as you expect outside of Python environment?

Also you are changing directory so I don't see why you need to put a forward slash prior to the file name.

Also, use forward slashes for your private key location (e.g. c:/location/location).

cherrysoft
  • 1,165
  • 8
  • 17
  • I'm using Filezilla rather than WinSCP but yep, checked that I can download the file outside of the Python environment. The 'filedate' is a placeholder for the folder, the file is within that path. I tried switching to forward slashes for the private key location but not luck – thefinland Jan 16 '19 at 00:04
  • 1
    check this https://stackoverflow.com/questions/38939454/verify-host-key-with-pysftp – cherrysoft Jan 16 '19 at 00:40
  • Indeed, I'd argue that this question is a duplicate of that one and should be closed as such. – Charles Duffy Jan 16 '19 at 00:52