Basic script to collect ssh_rsa_keys for all nodes and dump into a known_hosts
[monitor.git] / getsshkeys.py
1 #!/usr/bin/python
2
3 import os
4 import sys
5 import string
6 import time
7 import soltesz
8 import plc
9
10 def main():
11
12         l_nodes = plc.getNodes() 
13         d_nodes = {}
14         nokey_list = []
15         for host in l_nodes:
16                 name = host['hostname']
17                 d_nodes[name] = host
18
19         f = open("known_hosts", 'w')
20         for host in d_nodes:
21                 node = d_nodes[host]
22                 key = node['ssh_rsa_key']
23                 if key == None:
24                         nokey_list += [node]
25                 else:
26                         l_nw = plc.getNodeNetworks({'nodenetwork_id':node['nodenetwork_ids']})
27                         if len(l_nw) > 0:
28                                 ip = l_nw[0]['ip']
29                                 key = key.strip()
30                                 # TODO: check for '==' at end of key.
31                                 if key[-1] != '=':
32                                         print "Host with corrupt key! for %s %s" % (node['boot_state'], node['hostname'])
33                                 s_date = time.strftime("%Y/%m/%d_%H:%M:%S",time.gmtime(time.time()))
34                                 print >>f, "%s,%s %s %s" % (host,ip, key, "PlanetLab_%s" % (s_date))
35         f.close()
36
37         for node in nokey_list:
38                 print "%5s %s" % (node['boot_state'], node['hostname'])
39         
40 if __name__ == '__main__':
41         import os
42         main()