10 args['known_hosts'] = os.environ['HOME'] + os.sep + ".ssh" + os.sep + "known_hosts"
11 args['XMLRPC_SERVER'] = 'https://www.planet-lab.org/PLCAPI/'
14 def __init__(self, args = args):
16 self.read_knownhosts()
18 self.auth['AuthMethod'] = "anonymous"
19 self.api = xmlrpclib.Server(args['XMLRPC_SERVER'], verbose=False, allow_none=True)
20 self.nodenetworks = {}
22 def _split_kh_entry(self, line):
25 (host,ip) = s[0].split(',')
30 key = ' '.join(s[1:3])
31 comment = ' '.join(s[3:])
32 return (host, ip, key, comment)
34 def _get_index(self, host, ip):
37 index = "%s,%s" % (host,ip)
42 def read_knownhosts(self):
43 kh_read = open(self.args["known_hosts"], 'r')
47 (host, ip, key, comment) = self._split_kh_entry(line[:-1])
48 rec = { self._get_index(host, ip) : "%s %s" % (key, comment) }
49 if 'PlanetLab' in comment:
50 self.pl_keys.update(rec)
52 self.other_keys.update(rec)
54 #for i in self.pl_keys:
56 # print self.pl_keys[i]
61 self.write_knownhosts()
63 def write_knownhosts(self):
64 f = open(self.args['known_hosts'], 'w')
65 for index in self.pl_keys:
66 print >>f, "%s %s" % (index, self.pl_keys[index])
67 for index in self.other_keys:
68 print >>f, "%s %s" % (index, self.other_keys[index])
72 l_nodes = self.getNodes()
76 name = node['hostname']
81 (host, ip, key, comment) = self._record_from_node(node, nokey_list)
82 rec = { "%s,%s" % (host,ip) : "%s %s" % (key, comment) }
83 self.pl_keys.update(rec)
87 def delete(self, host):
88 node = self.getNodes(host)
89 (host, ip, _, _) = self._record_from_node(node[0])
90 index = "%s,%s" % (host,ip)
91 if index in self.pl_keys:
92 del self.pl_keys[index]
93 if index in self.other_keys:
94 del self.other_keys[index]
96 def updateDirect(self, host):
97 cmd = os.popen("/usr/bin/ssh-keyscan -t rsa %s 2>/dev/null" % host)
99 (h, ip, key, comment) = self._split_kh_entry(line[:-1])
100 node = self.getNodes(host)
101 (host2, ip2, x, x) = self._record_from_node(node[0])
102 rec = { self._get_index(host2, ip2) : "%s %s" % (key, "DIRECT") }
105 self.other_keys.update(rec)
107 def update(self, host):
108 node = self.getNodes(host)
109 ret = self._record_from_node(node[0])
110 (host, ip, key, comment) = ret
112 self.updateDirect(host)
114 rec = { "%s,%s" % (host,ip) : "%s %s" % (key, comment) }
115 self.pl_keys.update(rec)
117 def getNodes(self, host=None):
118 if type(host) == type(""): host = [host]
120 # get the node(s) info
121 nodes = self.api.GetNodes(self.auth,host,["hostname","ssh_rsa_key","nodenetwork_ids"])
123 # for each node's node network, update the self.nodenetworks cache
126 for net in node["nodenetwork_ids"]:
127 nodenetworks.append(net)
129 plcnodenetworks = self.api.GetNodeNetworks(self.auth,nodenetworks,["nodenetwork_id","ip"])
130 for n in plcnodenetworks:
131 self.nodenetworks[n["nodenetwork_id"]]=n
134 def _record_from_node(self, node, nokey_list=None):
135 host = node['hostname']
136 key = node['ssh_rsa_key']
138 nodenetworks = node['nodenetwork_ids']
139 if len(nodenetworks)==0: return (host, None, None, None)
141 # the [0] subscript to node['nodenetwork_ids'] means
142 # that this function wont work with multihomed nodes
143 l_nw = self.nodenetworks.get(nodenetworks[0],None)
144 if l_nw is None: return (host, None, None, None)
148 if nokey_list is not None: nokey_list += [node]
149 return (host, ip, None, None)
152 # TODO: check for '==' at end of key.
154 print "Host with corrupt key! for %s %s" % (node['boot_state'], node['hostname'])
156 s_date = time.strftime("%Y/%m/%d_%H:%M:%S",time.gmtime(time.time()))
157 #rec = { "%s,%s" % (host,ip) : "%s %s" % (key, "PlanetLab_%s" % (s_date)) }
159 return (host, ip, key, "PlanetLab_%s" % s_date)
171 if __name__ == '__main__':