3 # this checks various forms of URLS for reaching a PLCAPI
4 # i.e. with http:// or https:// (only the latter is expected to work)
5 # with or without a trailing slash
6 # using a hostname or an IP
12 class PlcapiUrlScanner:
14 # turns out the config has an ip but no name..
15 def __init__ (self, auth, hostname=None, ip=None, verbose=False):
17 if not hostname and not ip:
18 raise Exception,"PlcapiUrlScanner needs _some_ input"
21 try: ip=socket.gethostbyname(hostname)
23 hostname="%s.pl.sophia.inria.fr"%hostname
24 ip=socket.gethostbyname(hostname)
26 if not hostname: hostname=socket.gethostbyaddr(ip)[0]
27 self.hostname=hostname
31 def try_url (self,url):
33 xmlrpclib.ServerProxy (url, verbose=self.verbose, allow_none=True).GetNodes(self.auth)
36 except xmlrpclib.ProtocolError as e:
37 print '... (http error %s)'%e.errcode,url
39 except Exception as e:
40 print '---',type(e).__name__,url,e
41 if self.verbose: traceback.print_exc()
44 def try_url_required (self, url, required):
45 result=self.try_url(url)
46 if required and not result: return False
51 for protocol in ['http','https']:
52 for dest in [ self.hostname, self.ip ]:
53 for port in [ '',':80',':443']:
54 for path in ['PLCAPI','PLCAPI/']:
55 if protocol=='http' and port==':443': continue
56 if protocol=='https' and port==':80': continue
57 required = (protocol=='https') and (path=='PLCAPI/')
58 url="%s://%s%s/%s"%(protocol,dest,port,path)
59 if not self.try_url_required (url,required): overall=False
62 from optparse import OptionParser
65 auth={'AuthMethod':'password','Username':'root@test.onelab.eu','AuthString':'test++'}
68 usage="%prog hostname"
70 parser.add_option("-v","--verbose",dest='verbose',action='store_true',default=False)
71 (options,args)=parser.parse_args()
76 success=PlcapiUrlScanner (auth=auth, hostname=hostname,verbose=options.verbose).scan()
77 sys.exit(0 if success else -1)
79 if __name__ == '__main__':