I am working on an sftp connectivity set up:
import os
from Crypto.Cipher import AES
import paramiko
import pysftp as sftp
cnopts = sftp.CnOpts(knownhosts='C:/Users/mposc/.shh/known_hosts/')
cnopts.hostkeys.load('projectKey.pub')
#cnopts.hostkeys = None
def sftpconnect():
try:
cnnt = sftp.Connection(host="example.host.com", username="usersample", password="*****", cnopts=cnopts)
print("connection succesful")
cnnt.close()
except Exception as e:
print (str(e))
sftpconnect()
and i have the following known argument:
UserWarning: Failed to load HostKeys from C:/Users/mposc/.shh/known_hosts/. You will need to explicitly load HostKeys (cnopts.hostkeys.load(filename)) or disableHostKey checking (cnopts.hostkeys = None).
I have create the public key using Windows cmd (https://phoenixnap.com/kb/generate-ssh-key-windows-10) and the file is stored here: C:\Users\mposc.shh\known_hosts\projectKey.pub
I have searched around and find very useful notes form users, especially: Verify host key with pysftp
I am still struggling with this: if I set the cnopts.hostkeys.load('projectKey.pub') I cannot connect and with error: UserWarning: Failed to load HostKeys from C:/Users/mposc/.shh/known_hosts/. If I set cnopts.hostkeys = None I can connect..
I read that this is a pysftp bug, but I am concern that this is not safe. I have tried several different options from the pysftp library and as suggested in different site. How do I add the file name correctly?