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