0

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.

snowcoder
  • 481
  • 1
  • 9
  • 23

1 Answers1

-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

import pysftp
import sys
import csv
import json, os
from pysftp import known_hosts
import warnings
warnings.simplefilter(action='ignore',category=UserWarning)

myHostname = "example.coom"
myUsername = "useer"
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")
snowcoder
  • 481
  • 1
  • 9
  • 23
  • *"I dont have sftp host key"* – what do you mean by that? Of course you do. – Martin Prikryl Nov 05 '21 at 07:06
  • I found the key using putty but any idea how can I use file in code. I am running script on window server. Many other documentation reefers to linux based systems. – snowcoder Nov 05 '21 at 18:23
  • There's a link in my answer to the duplicate question: [Verify host key with pysftp](https://stackoverflow.com/q/38939454/850848). – Martin Prikryl Nov 05 '21 at 18:24