diff --git a/clients/send_nrdp.py b/clients/send_nrdp.py index edae19c..8acc92b 100755 --- a/clients/send_nrdp.py +++ b/clients/send_nrdp.py @@ -1,6 +1,6 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 -############################################################################## + ############################################################################# # # # send_nrdp.py - Send host/service checkresults to NRDP with XML @@ -45,14 +45,18 @@ # ############################################################################# - -import argparse, sys, urllib, cgi +import argparse, sys +from html import escape +from future.standard_library import install_aliases +install_aliases() +from urllib import request as UrlRequest +from urllib import parse as UrlParse from xml.dom.minidom import parseString class send_nrdp: def run(self): parser = argparse.ArgumentParser(formatter_class=argparse.RawTextHelpFormatter) - + parser.add_argument('-u', '--url', action="store", dest="url", help="\ ** REQUIRED ** The URL used to access the remote NRDP agent.") @@ -106,9 +110,9 @@ def run(self): printf \"\\t\\t\\t\\n\"\n") parser.add_argument('-c', '--checktype', action="store", dest="checktype", help="1 for passive 0 for active") - + options = parser.parse_args() - + if not options.url: parser.error('You must specify a url.') if not options.token: @@ -116,9 +120,9 @@ def run(self): try: self.setup(options) sys.exit() - except Exception, e: + except Exception as e: sys.exit(e) - + def getText(self, nodelist): rc = [] for node in nodelist: @@ -130,28 +134,26 @@ def post_data(self, url, token, xml): # Make sure URL ends with a / if not url.endswith('/'): url += '/' - - params = urllib.urlencode({'token': token.strip(), 'cmd': 'submitcheck', 'xml': xml}); - opener = urllib.FancyURLopener() + params = UrlParse.urlencode({'token': token.strip(), 'cmd': 'submitcheck', 'XMLDATA': xml}); + url_with_data = "{0}?{1}".format(url, params) try: - f = opener.open(url, params) - result = parseString(f.read()) - except Exception, e: - print "Cannot connect to url." - # TODO add directory option - sys.exit(e) + with UrlRequest.urlopen(url_with_data) as f: + result = parseString(f.read().decode('utf-8')) + except Exception as e: + print("ERROR - Cannot connect to url: {0}".format(str(e))) + return -3 if self.getText(result.getElementsByTagName("status")[0].childNodes) == "0": - sys.exit() + return 1 else: - print "ERROR - NRDP Returned: "+self.getText(result.getElementsByTagName("message")[0].childNodes) - sys.exit(1) + print("ERROR - NRDP Returned: " + self.getText(result.getElementsByTagName("message")[0].childNodes)) + return -1 def setup(self, options): if not options.delim: options.delim = "\t" if not options.checktype: options.checktype = "1" - + # If only url and token have been provided then it is assumed that data is being piped if not options.hostname and not options.state and not options.file: xml="\n\n"; @@ -159,47 +161,47 @@ def setup(self, options): parts = line.split(options.delim) if len(parts) == 4: xml += "" - xml += ""+cgi.escape(parts[0],True)+"" - xml += ""+cgi.escape(parts[1],True)+"" + xml += ""+html.escape(parts[0],True)+"" + xml += ""+html.escape(parts[1],True)+"" xml += ""+parts[2]+"" - xml += ""+cgi.escape(parts[3],True)+"" + xml += ""+html.escape(parts[3],True)+"" xml += "" if len(parts) == 3: xml += "" - xml += ""+cgi.escape(parts[0],True)+"" + xml += ""+html.escape(parts[0],True)+"" xml += ""+parts[1]+"" - xml += ""+cgi.escape(parts[2],True)+"" + xml += ""+html.escape(parts[2],True)+"" xml += "" xml += "" - + elif options.hostname and options.state: xml="\n\n"; if options.service: xml += "" - xml += ""+cgi.escape(options.hostname,True)+"" - xml += ""+cgi.escape(options.service,True)+"" + xml += ""+html.escape(options.hostname,True)+"" + xml += ""+html.escape(options.service,True)+"" xml += ""+options.state+"" - xml += ""+cgi.escape(options.output,True)+"" + xml += ""+html.escape(options.output,True)+"" xml += "" else: xml += "" - xml += ""+cgi.escape(options.hostname,True)+"" + xml += ""+html.escape(options.hostname,True)+"" xml += ""+options.state+"" - xml += ""+cgi.escape(options.output,True)+"" + xml += ""+html.escape(options.output,True)+"" xml += "" xml += "" elif options.file: try: file_handle = open(options.file, 'r') - except Exception, e: - print "Error opening file '"+options.file+"'" - sys.exit(e) + except Exception as e: + print("ERROR - in opening file: " + options.file) + return -4 else: xml = file_handle.read() file_handle.close() - self.post_data(options.url, options.token, xml) + return self.post_data(options.url, options.token, xml) if __name__ == "__main__": send_nrdp().run()