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
13 from gethostbyaddr import workaround_gethostbyaddr
16 class PlcapiUrlScanner:
18 # turns out the config has an ip but no name..
19 def __init__ (self, auth, hostname=None, ip=None, verbose=False):
21 if not hostname and not ip:
22 raise Exception("PlcapiUrlScanner needs _some_ input")
24 hostname = workaround_gethostbyaddr(ip)
25 elif hostname and not ip:
27 ip = socket.gethostbyname(hostname)
29 hostname = "{}.pl.sophia.inria.fr".format(hostname)
30 ip = socket.gethostbyname(hostname)
31 self.hostname = hostname
33 self.verbose = verbose
35 def try_url (self, url):
37 proxy = xmlrpc.client.ServerProxy(url, verbose=self.verbose, allow_none=True,
38 context=ssl._create_unverified_context())
39 nodes = proxy.GetNodes(self.auth)
42 except xmlrpc.client.ProtocolError as e:
43 print('... (http error {})'.format(e.errcode), url)
45 except Exception as e:
46 print('---', type(e).__name__, url, e)
51 def try_url_required (self, url, required):
52 result = self.try_url(url)
53 if required and not result:
60 for protocol in ['http','https']:
61 for dest in [ self.hostname, self.ip ]:
62 for port in [ '', ':80', ':443']:
63 for path in ['PLCAPI', 'PLCAPI/']:
64 if protocol=='http' and port==':443':
66 if protocol=='https' and port==':80':
68 required = (protocol=='https') and (path=='PLCAPI/')
69 url="{}://{}{}/{}".format(protocol, dest, port, path)
70 if not self.try_url_required (url,required):
74 from optparse import OptionParser
77 auth={'AuthMethod':'password','Username':'root@test.onelab.eu','AuthString':'test++'}
80 usage = "%prog hostname"
81 parser = OptionParser()
82 parser.add_option("-v", "--verbose", dest='verbose', action='store_true', default=False)
83 (options,args) = parser.parse_args()
88 success = PlcapiUrlScanner (auth=auth, hostname=hostname,verbose=options.verbose).scan()
89 sys.exit(0 if success else -1)
91 if __name__ == '__main__':