X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=system%2FPlcapiUrlScanner.py;h=020ea55a53e0ac587c4c485b5e48004edcdec055;hb=f265fa790c62a12de0a24fcbbd9eb32bb24eae0e;hp=1ef2d8085aefc872aae0c33fe1958e020997d115;hpb=52db22315ad721de978af1b1f2201fa2fe0a0d76;p=tests.git diff --git a/system/PlcapiUrlScanner.py b/system/PlcapiUrlScanner.py index 1ef2d80..020ea55 100755 --- a/system/PlcapiUrlScanner.py +++ b/system/PlcapiUrlScanner.py @@ -6,55 +6,67 @@ # using a hostname or an IP import socket -import xmlrpclib +import xmlrpc.client import traceback +import ssl class PlcapiUrlScanner: # turns out the config has an ip but no name.. def __init__ (self, auth, hostname=None, ip=None, verbose=False): - self.auth=auth + self.auth = auth if not hostname and not ip: - raise Exception,"PlcapiUrlScanner needs _some_ input" + raise Exception("PlcapiUrlScanner needs _some_ input") if hostname: if not ip: - try: ip=socket.gethostbyname(hostname) + try: + ip = socket.gethostbyname(hostname) except: - hostname="%s.pl.sophia.inria.fr"%hostname - ip=socket.gethostbyname(hostname) + hostname = "{}.pl.sophia.inria.fr".format(hostname) + ip = socket.gethostbyname(hostname) else: - if not hostname: hostname=socket.gethostbyaddr(ip)[0] - self.hostname=hostname - self.ip=ip - self.verbose=verbose + hostname=socket.gethostbyaddr(ip)[0] + self.hostname = hostname + self.ip = ip + self.verbose = verbose - def try_url (self,url): + def try_url (self, url): try: - xmlrpclib.ServerProxy (url, verbose=self.verbose, allow_none=True).GetNodes(self.auth) - print 'YES',url + proxy = xmlrpc.client.ServerProxy(url, verbose=self.verbose, allow_none=True, + context=ssl._create_unverified_context()) + nodes = proxy.GetNodes(self.auth) + print('YES', url) return True - except xmlrpclib.ProtocolError as e: - print '... (http error %s)'%e.errcode,url + except xmlrpc.client.ProtocolError as e: + print('... (http error {})'.format(e.errcode), url) return False except Exception as e: - print '---',type(e).__name__,url,e - if self.verbose: traceback.print_exc() + print('---', type(e).__name__, url, e) + if self.verbose: + traceback.print_exc() return False - def try_url_expected (self, url, expected): - return self.try_url(url)==expected + def try_url_required (self, url, required): + result = self.try_url(url) + if required and not result: + return False + else: + return True def scan(self): - overall=True + overall = True for protocol in ['http','https']: - expected= protocol=='https' for dest in [ self.hostname, self.ip ]: - for port in [ '',':80',':443']: - for path in ['PLCAPI','PLCAPI/']: - if protocol=='http' and port==':443': continue - if protocol=='https' and port==':80': continue - url="%s://%s%s/%s"%(protocol,dest,port,path) - if not self.try_url_expected (url,expected): overall=False + for port in [ '', ':80', ':443']: + for path in ['PLCAPI', 'PLCAPI/']: + if protocol=='http' and port==':443': + continue + if protocol=='https' and port==':80': + continue + required = (protocol=='https') and (path=='PLCAPI/') + url="{}://{}{}/{}".format(protocol, dest, port, path) + if not self.try_url_required (url,required): + overall=False return overall from optparse import OptionParser @@ -63,15 +75,15 @@ import sys auth={'AuthMethod':'password','Username':'root@test.onelab.eu','AuthString':'test++'} def main (): - usage="%prog hostname" - parser=OptionParser() - parser.add_option("-v","--verbose",dest='verbose',action='store_true',default=False) - (options,args)=parser.parse_args() - if len(args)!=1: + usage = "%prog hostname" + parser = OptionParser() + parser.add_option("-v", "--verbose", dest='verbose', action='store_true', default=False) + (options,args) = parser.parse_args() + if len(args) != 1: parser.print_help() sys.exit(1) - hostname=args[0] - success=PlcapiUrlScanner (auth=auth, hostname=hostname,verbose=options.verbose).scan() + hostname = args[0] + success = PlcapiUrlScanner (auth=auth, hostname=hostname,verbose=options.verbose).scan() sys.exit(0 if success else -1) if __name__ == '__main__':