commit 53ed3aef7c3104e06a05ddf2a0ba0690f7aec7a7
parent 838d453336427f2dff71dc3e0281a3131cc151d9
Author: Sheng <webmaster0115@gmail.com>
Date: Tue, 10 Apr 2018 08:26:32 +0800
Check is_missing_host_keys before adding them
Diffstat:
M | main.py | | | 31 | +++++++++++++++++++++---------- |
1 file changed, 21 insertions(+), 10 deletions(-)
diff --git a/main.py b/main.py
@@ -40,18 +40,29 @@ class AutoAddPolicy(paramiko.client.MissingHostKeyPolicy):
"""
lock = threading.Lock()
+ def is_missing_host_keys(self, client, hostname, key):
+ k = client._host_keys.lookup(hostname)
+ if k is None:
+ return True
+ host_key = k.get(key.get_name(), None)
+ if host_key is None:
+ return True
+ if host_key != key:
+ raise paramiko.BadHostKeyException(hostname, key, host_key)
+
def missing_host_key(self, client, hostname, key):
with self.lock:
- keytype = key.get_name()
- logging.info(
- 'Adding {} host key for {}'.format(keytype, hostname)
- )
- client._host_keys.add(hostname, keytype, key)
-
- with open(client._host_keys_filename, 'a') as f:
- f.write('{} {} {}\n'.format(
- hostname, keytype, key.get_base64()
- ))
+ if self.is_missing_host_keys(client, hostname, key):
+ keytype = key.get_name()
+ logging.info(
+ 'Adding {} host key for {}'.format(keytype, hostname)
+ )
+ client._host_keys.add(hostname, keytype, key)
+
+ with open(client._host_keys_filename, 'a') as f:
+ f.write('{} {} {}\n'.format(
+ hostname, keytype, key.get_base64()
+ ))
paramiko.client.AutoAddPolicy = AutoAddPolicy