X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=getsshkeys.py;h=137ea68742e3aa1877905f8423e1d787a92b4ca9;hb=3f501b69f366b8b6c62d35d6aea4ebf5fa0f1192;hp=d0084db0f09a304330160925fd7230a6b16f17c4;hpb=0a8be8151bb0c598591a0c35efcdeb91faac155a;p=monitor.git diff --git a/getsshkeys.py b/getsshkeys.py index d0084db..137ea68 100755 --- a/getsshkeys.py +++ b/getsshkeys.py @@ -5,17 +5,25 @@ import sys import string import time import xml, xmlrpclib +try: + from monitor import config + auth = {'Username' : config.API_AUTH_USER, + 'AuthMethod' : "password", + 'AuthString' : config.API_AUTH_PASSWORD} +except: + import traceback + print traceback.print_exc() + auth = {'AuthMethod' : "anonymous"} args = {} args['known_hosts'] = os.environ['HOME'] + os.sep + ".ssh" + os.sep + "known_hosts" -args['XMLRPC_SERVER'] = 'https://www.planet-lab.org/PLCAPI/' +args['XMLRPC_SERVER'] = 'https://boot.planet-lab.org/PLCAPI/' class SSHKnownHosts: def __init__(self, args = args): self.args = args self.read_knownhosts() - self.auth = {} - self.auth['AuthMethod'] = "anonymous" + self.auth = auth self.api = xmlrpclib.Server(args['XMLRPC_SERVER'], verbose=False, allow_none=True) self.nodenetworks = {} @@ -86,12 +94,14 @@ class SSHKnownHosts: def delete(self, host): node = self.getNodes(host) - (host, ip, _, _) = self._record_from_node(node[0]) - index = "%s,%s" % (host,ip) - if index in self.pl_keys: - del self.pl_keys[index] - if index in self.other_keys: - del self.other_keys[index] + if len(node) > 0: + (host, ip, _, _) = self._record_from_node(node[0]) + index = "%s,%s" % (host,ip) + if index in self.pl_keys: + del self.pl_keys[index] + if index in self.other_keys: + del self.other_keys[index] + return node def updateDirect(self, host): cmd = os.popen("/usr/bin/ssh-keyscan -t rsa %s 2>/dev/null" % host) @@ -105,14 +115,16 @@ class SSHKnownHosts: self.other_keys.update(rec) def update(self, host): - node = self.getNodes(host) - ret = self._record_from_node(node[0]) - (host, ip, key, comment) = ret - if ip == None: - self.updateDirect(host) - else: - rec = { "%s,%s" % (host,ip) : "%s %s" % (key, comment) } - self.pl_keys.update(rec) + node = self.delete(host) + #node = self.getNodes(host) + if node is not []: + ret = self._record_from_node(node[0]) + (host, ip, key, comment) = ret + if ip == None: + self.updateDirect(host) + else: + rec = { "%s,%s" % (host,ip) : "%s %s" % (key, comment) } + self.pl_keys.update(rec) def getNodes(self, host=None): if type(host) == type(""): host = [host] @@ -150,7 +162,7 @@ class SSHKnownHosts: key = key.strip() # TODO: check for '==' at end of key. - if key[-1] != '=': + if len(key) > 0 and key[-1] != '=': print "Host with corrupt key! for %s %s" % (node['boot_state'], node['hostname']) s_date = time.strftime("%Y/%m/%d_%H:%M:%S",time.gmtime(time.time())) @@ -163,7 +175,7 @@ def main(hosts): k = SSHKnownHosts() if len (hosts) > 0: for host in hosts: - k.update(host) + k.updateDirect(host) else: k.updateAll() k.write()