1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 """\
21 Monkey Patch and feature map for Python Paramiko
22
23 """
24
25 import paramiko
26 import platform
27 from utils import compare_versions
28
29 PARAMIKO_VERSION = paramiko.__version__.split()[0]
30 PARAMIKO_FEATURE = {
31 'forward-ssh-agent': compare_versions(PARAMIKO_VERSION, ">=", '1.8.0') and (platform.system() != "Windows"),
32 'use-compression': compare_versions(PARAMIKO_VERSION, ">=", '1.7.7.1'),
33 'hash-host-entries': compare_versions(PARAMIKO_VERSION, ">=", '99'),
34 'host-entries-reloadable': compare_versions(PARAMIKO_VERSION, ">=", '99'),
35 'preserve-known-hosts': compare_versions(PARAMIKO_VERSION, ">=", '99'),
36 }
37
39 """\
40 FIXME!!! --- this method should become part of Paramiko
41
42 This method has been taken from SSHClient class in Paramiko and
43 has been improved and adapted to latest SSH implementations.
44
45 Save the host keys back to a file.
46 Only the host keys loaded with
47 L{load_host_keys} (plus any added directly) will be saved -- not any
48 host keys loaded with L{load_system_host_keys}.
49
50 @param filename: the filename to save to
51 @type filename: str
52
53 @raise IOError: if the file could not be written
54
55 """
56
57
58 if self.known_hosts is not None:
59 self.load_host_keys(self.known_hosts)
60
61 f = open(filename, 'w')
62
63 _host_keys = self.get_host_keys()
64 for hostname, keys in _host_keys.iteritems():
65
66 for keytype, key in keys.iteritems():
67 f.write('%s %s %s\n' % (hostname, keytype, key.get_base64()))
68
69 f.close()
70
71
73 """\
74 Read a file of known SSH host keys, in the format used by openssh.
75 This type of file unfortunately doesn't exist on Windows, but on
76 posix, it will usually be stored in
77 C{os.path.expanduser("~/.ssh/known_hosts")}.
78
79 If this method is called multiple times, the host keys are merged,
80 not cleared. So multiple calls to C{load} will just call L{add},
81 replacing any existing entries and adding new ones.
82
83 @param filename: name of the file to read host keys from
84 @type filename: str
85
86 @raise IOError: if there was an error reading the file
87
88 """
89 f = open(filename, 'r')
90 for line in f:
91 line = line.strip()
92 if (len(line) == 0) or (line[0] == '#'):
93 continue
94 e = paramiko.hostkeys.HostKeyEntry.from_line(line)
95 if e is not None:
96 _hostnames = e.hostnames
97 for h in _hostnames:
98 if self.check(h, e.key):
99 e.hostnames.remove(h)
100 if len(e.hostnames):
101 self._entries.append(e)
102 f.close()
103
104
105 -def _HostKeys_add(self, hostname, keytype, key, hash_hostname=True):
106 """\
107 Add a host key entry to the table. Any existing entry for a
108 C{(hostname, keytype)} pair will be replaced.
109
110 @param hostname: the hostname (or IP) to add
111 @type hostname: str
112 @param keytype: key type (C{"ssh-rsa"} or C{"ssh-dss"})
113 @type keytype: str
114 @param key: the key to add
115 @type key: L{PKey}
116
117 """
118 for e in self._entries:
119 if (hostname in e.hostnames) and (e.key.get_name() == keytype):
120 e.key = key
121 return
122 if not hostname.startswith('|1|') and hash_hostname:
123 hostname = self.hash_host(hostname)
124 self._entries.append(paramiko.hostkeys.HostKeyEntry([hostname], key))
125
126
134