make HostKeys use odict to ensure order is preserved, and add HostKeys.save()
This commit is contained in:
parent
4fa4fdee4b
commit
c22df44900
|
@ -26,6 +26,7 @@ import UserDict
|
|||
|
||||
from paramiko.common import *
|
||||
from paramiko.dsskey import DSSKey
|
||||
from paramiko.odict import odict
|
||||
from paramiko.rsakey import RSAKey
|
||||
|
||||
|
||||
|
@ -50,7 +51,7 @@ class HostKeys (UserDict.DictMixin):
|
|||
@type filename: str
|
||||
"""
|
||||
# hostname -> keytype -> PKey
|
||||
self._keys = {}
|
||||
self._keys = odict()
|
||||
self.contains_hashes = False
|
||||
if filename is not None:
|
||||
self.load(filename)
|
||||
|
@ -68,7 +69,7 @@ class HostKeys (UserDict.DictMixin):
|
|||
@type key: L{PKey}
|
||||
"""
|
||||
if not hostname in self._keys:
|
||||
self._keys[hostname] = {}
|
||||
self._keys[hostname] = odict()
|
||||
if hostname.startswith('|1|'):
|
||||
self.contains_hashes = True
|
||||
self._keys[hostname][keytype] = key
|
||||
|
@ -89,7 +90,7 @@ class HostKeys (UserDict.DictMixin):
|
|||
|
||||
@raise IOError: if there was an error reading the file
|
||||
"""
|
||||
f = file(filename, 'r')
|
||||
f = open(filename, 'r')
|
||||
for line in f:
|
||||
line = line.strip()
|
||||
if (len(line) == 0) or (line[0] == '#'):
|
||||
|
@ -106,6 +107,26 @@ class HostKeys (UserDict.DictMixin):
|
|||
self.add(host, keytype, DSSKey(data=base64.decodestring(key)))
|
||||
f.close()
|
||||
|
||||
def save(self, filename):
|
||||
"""
|
||||
Save host keys into a file, in the format used by openssh. The order of
|
||||
keys in the file will be preserved when possible (if these keys were
|
||||
loaded from a file originally). The single exception is that combined
|
||||
lines will be split into individual key lines, which is arguably a bug.
|
||||
|
||||
@param filename: name of the file to write
|
||||
@type filename: str
|
||||
|
||||
@raise IOError: if there was an error writing the file
|
||||
|
||||
@since: 1.6.1
|
||||
"""
|
||||
f = open(filename, 'w')
|
||||
for hostname, d in self._keys.iteritems():
|
||||
for keytype, key in d.iteritems():
|
||||
f.write('%s %s %s\n' % (hostname, keytype, key.get_base64()))
|
||||
f.close()
|
||||
|
||||
def lookup(self, hostname):
|
||||
"""
|
||||
Find a hostkey entry for a given hostname or IP. If no entry is found,
|
||||
|
|
|
@ -0,0 +1,63 @@
|
|||
#
|
||||
# This file and source code are in the public domain.
|
||||
#
|
||||
|
||||
class odict (dict):
|
||||
"""
|
||||
A dictionary with ordered keys. Based on the cookbook recipe at:
|
||||
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/107747
|
||||
"""
|
||||
|
||||
def __init__(self, *larg, **kwarg):
|
||||
self._keys = []
|
||||
dict.__init__(self, *larg, **kwarg)
|
||||
|
||||
def __delitem__(self, key):
|
||||
dict.__delitem__(self, key)
|
||||
self._keys.remove(key)
|
||||
|
||||
def __setitem__(self, key, item):
|
||||
dict.__setitem__(self, key, item)
|
||||
if key not in self._keys:
|
||||
self._keys.append(key)
|
||||
|
||||
def clear(self):
|
||||
dict.clear(self)
|
||||
self._keys = []
|
||||
|
||||
def copy(self):
|
||||
od = odict(self)
|
||||
return od
|
||||
|
||||
def items(self):
|
||||
return zip(self._keys, self.values())
|
||||
|
||||
def iteritems(self):
|
||||
for k in self._keys:
|
||||
yield k, self[k]
|
||||
|
||||
def keys(self):
|
||||
return self._keys[:]
|
||||
|
||||
def popitem(self):
|
||||
try:
|
||||
key = self._keys[-1]
|
||||
except IndexError:
|
||||
raise KeyError('dictionary is empty')
|
||||
|
||||
val = self[key]
|
||||
del self[key]
|
||||
|
||||
return (key, val)
|
||||
|
||||
def setdefault(self, key, failobj=None):
|
||||
if key not in self._keys:
|
||||
self._keys.append(key)
|
||||
dict.setdefault(self, key, failobj)
|
||||
|
||||
def update(self, d):
|
||||
for key, item in d.items():
|
||||
self.__setitem__(key, item)
|
||||
|
||||
def values(self):
|
||||
return map(self.get, self._keys)
|
Loading…
Reference in New Issue