0

I need to put file on SFTP server using hostkeys

import pysftp
import paramiko
from base64 import decodebytes


remotepath = r'/Location/TestFile.txt' 
localpath = r'\Location\SFTP\TestFile.txt'
keydata = b"""hUXEaCBx6d4lSGF157..."""
key = paramiko.RSAKey(data=decodebytes(keydata))
cnopts = pysftp.CnOpts()
cnopts.hostkeys.add('example.com', 'ssh-rsa', key)
with pysftp.Connection(host='example.com',username='usename',password='******',cnopts=cnopts) as sftp:
    sftp.put(localpath,remotepath)

But I got an error:

Traceback (most recent call last):
  File "\\locationPath\SFTP1.py", line 83, in <module>
    key = paramiko.RSAKey(data=decodebytes(keydata))
  File "C:\locationPath\Continuum\anaconda3\lib\site-packages\paramiko\rsakey.py", line 65, in __init__
    cert_type="ssh-rsa-cert-v01@openssh.com",
  File "C:\locationPath\Continuum\anaconda3\lib\site-packages\paramiko\pkey.py", line 397, in _check_type_and_load_cert
    type_ = msg.get_text()
  File "C:\locationPath\Local\Continuum\anaconda3\lib\site-packages\paramiko\message.py", line 178, in get_text
    return u(self.get_string())
  File "C:\locationPath\Local\Continuum\anaconda3\lib\site-packages\paramiko\py3compat.py", line 147, in u
    return s.decode(encoding)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe9 in position 2: invalid continuation byte

UPDATE:

Thanks @Martin Prikryl. I am able to use hostkey, however I still get warning:

Failed to load HostKeys from C:\Users\user\.ssh\known_hosts`.

So I generated public hostkey using command keygen. Now in a folder C:\Users\username\.ssh I have two files:

Do I need to modify one of those with the right hostkey, create folder known_hosts and manually insert move the file?

Or maybe there is a way to direct python to go to particular folder for hostkey?

Thank you

enter image description here

Serdia
  • 4,242
  • 22
  • 86
  • 159

0 Answers0