0

I am just starting off with paramiko, and I'm having some issue with load_system_host_keys().

When I try:

client = SSHClient()
client.load_system_host_keys(filename='/home/barashe/.ssh/known_hosts')
client.connect(hostname='lvs.cs.bgu.ac.il')
stdin, stdout, stderr = client.exec_command('ls -l')

I get

SHException: Server 'lvs.cs.bgu.ac.il' not found in known_hosts

And it seems like the hostkeys instance is empty

list(client.get_host_keys())
[]

If I use load_host_keys() instead of load_system_host_keys() I still get the same error, but the hostkeys instance is not empty now, and it includes the server I'm trying to connect to

list(client.get_host_keys())
['lvs.cs.bgu.ac.il',
'132.72.41.50']

Which seems rather odd... I know that by using

client.set_missing_host_key_policy(paramiko.AutoAddPolicy())

I can avoid this situation, but I prefer doing it the "right" way.

What I'm trying to understand is:

  1. Why am I getting the same error when using load_host_keys() even though the server appears in the hostkeys?
  2. What is the difference between load_host_keys() and load_system_host_keys() in this context?

Cheers!

Eran
  • 844
  • 6
  • 20

1 Answers1

0

If this is a private host key file in your home directory, you should not use load_system_host_keys but load_host_keys.

Just out of curiosity, where did you get your host key for that particular host if you did not use set_missing_host_key_policy? If you copied it from your .ssh directory, it is possible that the key file format is different. There are several.

You can test it by adding AutoAdd missing host key policy once and pointing to an empty private host key file. Your login should succeed now (assuming authentication succeeds). Whether it succeeds or fails, your private host key file should now contain the host key in the correct format. You can verify it works by removing the missing host key policy setting and running the script again. It should not moan about missing host keys anymore.

This works for me:

from paramiko import SSHClient
import paramiko

client = SSHClient()
client.load_host_keys(filename='/home/test/stest/kknown_hosts')
# client.set_missing_host_key_policy(paramiko.AutoAddPolicy())

client.connect(hostname='137.xx.x.x')
stdin, stdout, stderr = client.exec_command('ls -l')

Hope this helps, Hannu

Hannu
  • 11,685
  • 4
  • 35
  • 51
  • I did use my .ssh/known_hosts file, which I know understand wasn't a good call. I now created a new file, the way you suggested, and all is good! Many thanks! – Eran Mar 11 '17 at 16:51