I have a Python script that uses pysftp to create an SFTP connection to a remote host as shown:
cnopts = pysftp.CnOpts(knownhosts='known_hosts') # REFERENCE A STATIC FILE
sftp = pysftp.Connection(host=parm0, username=parm1, password=parm2, cnopts=cnopts)
The file "known_hosts" lives in the same directory where the Python script lives. I would like to eliminate the static know_hosts file and pass a file like object to pysftp.CnOpts() like this:
remote_public_key = subprocess.getoutput('ssh-keyscan myRemoteSFTPServer.com')
key_buffer.write(remote_public_key)
key_buffer.seek(0)
cnopts = pysftp.CnOpts(knownhosts=key_buffer.read()) # REFERENCE A FILE LIKE OBJECT
sftp = pysftp.Connection(host=parm0, username=parm1, password=parm2, cnopts=cnopts)
However, this fails. Is it possible to pass a file like object as a parameter to pysftp.CnOpts, and if so, how do I do it?