0

I am aware that Verify host key with pysftp has reply for this post that every other post seem to refer but I am still unable to connect using hostkey after trying considerable amount of time before asking.

What am I doing? I have this Server host key fingerprints SHA-256 = ssh-rsa 2048 TaaZ43VZIRKls7oDx............ MD5 = ssh-rsa 2048 ed:06:b3:16:9f:..................

my code looks like this:

fp_key =b"""TaaZ43VZIRKls7oDx........."""
key = paramiko.RSAKey(data=decodebytes(fp_key))
cnopts = pysftp.CnOpts()
cnopts.hostkeys.add(hostname="host",keytype='ssh-rsa', key=key)    
sftpConnect = pysftp.Connection(host="host", username="username", password="password", cnopts=cnopts)

Error I am getting:

File "C:\Program Files\Python38\lib\site-packages\paramiko\py3compat.py", line 147, in u
    return s.decode(encoding)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xa5 in position 4: invalid start byte

Ofcourse my key is not encoded, so it won't be able to decode. So I tried this :

    fp_key =b"""TaaZ43VZIRKls7oDx........."""
    fp_key = encodebytes(fp_key)
    key = paramiko.RSAKey(data=decodebytes(fp_key))    
    cnopts = pysftp.CnOpts()
    cnopts.hostkeys.add(hostname="host",keytype='ssh-rsa', key=key)    
    sftpConnect = pysftp.Connection(host="host", username="username", password="password", cnopts=cnopts)

Now I am getting this error :

Traceback (most recent call last):
  File "sftp_ssh_test.py", line 20, in <module>
    key = paramiko.RSAKey(data=decodebytes(fp_key))
  File "C:\Program Files\Python38\lib\site-packages\paramiko\rsakey.py", line 62, in __init__
    self._check_type_and_load_cert(
  File "C:\Program Files\Python38\lib\site-packages\paramiko\pkey.py", line 416, in _check_type_and_load_cert
    raise SSHException(err.format(self.__class__.__name__, type_))
paramiko.ssh_exception.SSHException: Invalid key (class: RSAKey, data type: TaaZ43VZIRKls7oDx.........
Prabhash Jha
  • 330
  • 2
  • 12
  • 1
    What goes to `RSAKey` is full public key, not only its fingerprint. [My answer to the linked question](https://stackoverflow.com/q/38939454/850848#43389508) shows how to get the full public key. If you need to use a fingerprint only, my answer also links to a question [Verify host key using its fingerprint](https://stackoverflow.com/q/46814823/850848). – Martin Prikryl Dec 09 '19 at 07:53
  • Thank you Martin. I was able to get it work with an implementation of your article described in https://stackoverflow.com/questions/47586224/connecting-to-an-sftp-server-using-pysftp-and-python-3-with-just-the-server-fing – Prabhash Jha Dec 09 '19 at 21:17

0 Answers0