0

I have previously connected to a clients SFTP through vb.net script with WinSCP which has worked successfully however I don't want to use WinSCP. I am newish to Python but think for my overall goal for the project will be better suited using python.

What I believe I am missing is potentially a public key and/or my privatekey is in the wrong format. Would really appreciate some help on this as I have been stuck for days trying different things.

import pysftp

myHostname = 'sftp.name.com'
myUsername = 'username'
myPassword = 'password'
myKey = 'C:\\PrivateKey.ppk'

with pysftp.Connection(host=myHostname, username=myUsername, private_key=myKey, private_key_pass=myPassword) as sftp:

Error received
UserWarning: Failed to load HostKeys from C:\Users\name\.ssh\known_hosts.
Dales91
  • 21
  • 1
  • 6

1 Answers1

0

The library is trying to find the known_hosts file to do a host key check. You can disable it, but as is written in the docs, it is strongly discouraged.

import pysftp

myHostname = 'sftp.name.com'
myUsername = 'username'
myPassword = 'password'
myKey = 'C:\\PrivateKey.ppk'
cnopts = pysftp.CnOpts()
cnopts.hostkeys = None

with pysftp.Connection(host=myHostname,
                       username=myUsername,
                       private_key=myKey,
                       private_key_pass=myPassword,
                       cnopts=cnopts) as sftp:
    pass

A better way would be to set the known_hosts path using the CnOpts. I don't have a Windows machine, but based on this SA answer, maybe the following will work:

import os
import pysftp

myHostname = 'sftp.name.com'
myUsername = 'username'
myPassword = 'password'
myKey = 'C:\\PrivateKey.ppk'
knownhosts_path = os.path.join(os.environ['USERPROFILE'], '.ssh', 'known_hosts')
cnopts = pysftp.CnOpts(knownhosts=knownhosts_path)
cnopts.hostkeys = None

with pysftp.Connection(host=myHostname,
                       username=myUsername,
                       private_key=myKey,
                       private_key_pass=myPassword,
                       cnopts=cnopts) as sftp:
    pass
Milan Cermak
  • 7,476
  • 3
  • 44
  • 59
  • Thanks Milan, but what should be in known_hosts? Also I still get an error if I use cnopts = pysftp.CnOpts() cnopts.hostkeys = None – Dales91 May 26 '20 at 14:31
  • known_hosts is a file holding a list of fingerprints of remote servers. If a fingerprint would change since the last time you connected to a server, you would know something malicious is going on. As for the second part of the questions, did you also pass the cnopts to the `Connection` constructor? – Milan Cermak May 26 '20 at 14:34
  • I don't have known_hosts, I added in this pathway to see if I could get it to work. The only files required when connecting through Winscp was the ppk file. – Dales91 May 26 '20 at 14:37
  • 1
    When you connect with WinSCP, the first time it will ask you to [verify the hostkey](https://winscp.net/eng/docs/ssh_verifying_the_host_key). That's WinSCP equivalent of this. – Martin Prikryl May 26 '20 at 14:38
  • Right that makes sense! Does this need to be saved down in a specific file format? – Dales91 May 26 '20 at 14:43
  • 1
    See my answer to the duplicate question. – Martin Prikryl May 26 '20 at 15:01