11 args['known_hosts'] = "/home/soltesz/.ssh/known_hosts"
14 def __init__(self, args = args):
16 self.read_knownhosts()
18 def _split_kh_entry(self, line):
21 (host,ip) = s[0].split(',')
26 key = ' '.join(s[1:3])
27 comment = ' '.join(s[3:])
28 return (host, ip, key, comment)
30 def _get_index(self, host, ip):
33 index = "%s,%s" % (host,ip)
38 def read_knownhosts(self):
39 kh_read = open(self.args["known_hosts"], 'r')
43 (host, ip, key, comment) = self._split_kh_entry(line[:-1])
44 rec = { self._get_index(host, ip) : "%s %s" % (key, comment) }
45 if 'PlanetLab' in comment:
46 self.pl_keys.update(rec)
48 self.other_keys.update(rec)
50 #for i in self.pl_keys:
52 # print self.pl_keys[i]
57 self.write_knownhosts()
59 def write_knownhosts(self):
60 f = open(self.args['known_hosts'], 'w')
61 for index in self.pl_keys:
62 print >>f, "%s %s" % (index, self.pl_keys[index])
63 for index in self.other_keys:
64 print >>f, "%s %s" % (index, self.other_keys[index])
68 l_nodes = plc.getNodes()
72 name = node['hostname']
77 (host, ip, key, comment) = self._record_from_node(node, nokey_list)
78 rec = { "%s,%s" % (host,ip) : "%s %s" % (key, comment) }
79 self.pl_keys.update(rec)
83 def delete(self, host):
84 node = plc.getNodes(host)
85 (host, ip, _, _) = self._record_from_node(node[0])
86 index = "%s,%s" % (host,ip)
87 if index in self.pl_keys:
88 del self.pl_keys[index]
89 if index in self.other_keys:
90 del self.other_keys[index]
92 def updateDirect(self, host):
93 cmd = os.popen("/usr/bin/ssh-keyscan -t rsa %s 2>/dev/null" % host)
95 (h, ip, key, comment) = self._split_kh_entry(line[:-1])
96 node = plc.getNodes(host)
97 (host2, ip2, x, x) = self._record_from_node(node[0])
98 rec = { self._get_index(host2, ip2) : "%s %s" % (key, "DIRECT") }
101 self.other_keys.update(rec)
103 def update(self, host):
104 node = plc.getNodes(host)
105 ret = self._record_from_node(node[0])
106 (host, ip, key, comment) = ret
108 self.updateDirect(host)
110 rec = { "%s,%s" % (host,ip) : "%s %s" % (key, comment) }
111 self.pl_keys.update(rec)
113 def _record_from_node(self, node, nokey_list=None):
114 host = node['hostname']
115 key = node['ssh_rsa_key']
117 l_nw = plc.getNodeNetworks({'nodenetwork_id':node['nodenetwork_ids']})
119 # No network for this node. So, skip it.
120 return (host, None, None, None)
125 if nokey_list is not None: nokey_list += [node]
126 return (host, ip, None, None)
129 # TODO: check for '==' at end of key.
131 print "Host with corrupt key! for %s %s" % (node['boot_state'], node['hostname'])
133 s_date = time.strftime("%Y/%m/%d_%H:%M:%S",time.gmtime(time.time()))
134 #rec = { "%s,%s" % (host,ip) : "%s %s" % (key, "PlanetLab_%s" % (s_date)) }
136 return (host, ip, key, "PlanetLab_%s" % s_date)
141 nokey_list = k.updateAll()
143 for node in nokey_list:
144 print "%5s %s" % (node['boot_state'], node['hostname'])
146 if __name__ == '__main__':
150 #k.update('planetlab-4.cs.princeton.edu')
151 #print "updateDirect"
152 k.update(sys.argv[1])
153 #k.updateDirect(sys.argv[1])