I am trying to copy files from SFTP server too local server (window) using python pysftp libraries. I am authenticating sftp server with usrname & password and no SSH-Host keys.
My code is running and copying files to local directory but still getting warning message about HostKeys.
import pysftp
import sys
import csv
import json, os
from pysftp import known_hosts
import warnings
warnings.simplefilter(action='ignore',category=UserWarning)
myHostname = "exmaple.com"
myUsername = "user"
myPassword = "foo"
data = []
cnopts = pysftp.CnOpts()
cnopts.hostkeys = None
try:
with pysftp.Connection(host=myHostname, username=myUsername, password=myPassword, port=22, cnopts=cnopts ) as sftp:
print ("Connection succesfully stablished ... ")
#cnopts=cnopts
# Define the file that you want to download from the remote directory
remoteFilePath = '/rcv'
os.chdir("gpgencrypted")
file = sftp.get_d(remoteFilePath,'',preserve_mtime=True)
print("File copied to mid-server successfully")
except ValueError:
print("File Transfer was unsuccessful")
Here is warning error in output. I am setting None hostkey in code but still warning message is appearing
Warning (from warnings module):
File "C:\Program Files\Python39\lib\site-packages\pysftp\__init__.py", line 61
warnings.warn(wmsg, UserWarning)
UserWarning: Failed to load HostKeys from C:\Users\kiran.patil\.ssh\known_hosts. You will need to explicitly load HostKeys (cnopts.hostkeys.load(filename)) or disableHostKey checking (cnopts.hostkeys = None).
Connection succesfully stablished ...
None
Edit 1: Added warning filter to skip UserWarning. Ideally host-key should be used but right now I dont have sftp host key but I definitely use host-key before we commit to production.