1

I'm connecting to a SSH server using Paramiko, and I want to add the "host_keys" temporarily.

How do I do it?

import paramiko

client = paramiko.SSHClient()
client.load_system_host_keys()
#client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(hostname=str(host),username =str(user),password=str(pswd))
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
Ramon Medeiros
  • 2,272
  • 2
  • 24
  • 41

1 Answers1

0
from paramiko import RSAKey
from paramiko.py3compat import decodebytes

client = SSHClient()

# known host key
know_host_key = "<KEY>"
keyObj = RSAKey(data=decodebytes(know_host_key.encode()))

# add to host keys
client.get_host_keys().add(hostname=HOST, keytype="ssh-rsa", key=keyObj)

# login to ssh hostname
client.connect(hostname=HOST, port=PORT, username=USER)...

source: https://github.com/paramiko/paramiko/blob/2.6.0/tests/test_hostkeys.py#L75-L84

Ramon Medeiros
  • 2,272
  • 2
  • 24
  • 41
  • This and more is already covered in [Paramiko SSH failing with “Server '…' not found in known_hosts” when run on web server](https://stackoverflow.com/q/53635843/850848). – Martin Prikryl Jan 20 '20 at 17:39