From 9a3b430d90e1d5b176cf2542a8317d3beeb5ec5d Mon Sep 17 00:00:00 2001 From: Xavier Bachelot Date: Mon, 12 Aug 2019 16:37:43 +0200 Subject: [PATCH 1/3] Port to python3 Originaly submitted by @PHuhn (https://github.com/PHuhn): https://github.com/NagiosEnterprises/nrdp/issues/38#issuecomment-461011802 --- clients/send_nrdp.py | 57 ++++++++++++++++++++++---------------------- 1 file changed, 28 insertions(+), 29 deletions(-) diff --git a/clients/send_nrdp.py b/clients/send_nrdp.py index d2d9336..5c59cd2 100755 --- a/clients/send_nrdp.py +++ b/clients/send_nrdp.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 ############################################################################## # @@ -46,7 +46,8 @@ ############################################################################# -import argparse, sys, urllib, cgi +import argparse, sys, html +from urllib import request as UrlRequest, parse as UrlParse from xml.dom.minidom import parseString class send_nrdp: @@ -116,7 +117,7 @@ def run(self): try: self.setup(options) sys.exit() - except Exception, e: + except Exception as e: sys.exit(e) def getText(self, nodelist): @@ -130,21 +131,19 @@ 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: @@ -159,16 +158,16 @@ 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 += "" @@ -176,30 +175,30 @@ def setup(self, options): 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() From 94c66f5e848fa3fdadf2141345ac1977e748cb6c Mon Sep 17 00:00:00 2001 From: Xavier Bachelot Date: Wed, 14 Aug 2019 14:08:45 +0200 Subject: [PATCH 2/3] Compatibility with both python2 and python3 --- clients/send_nrdp.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/clients/send_nrdp.py b/clients/send_nrdp.py index 5c59cd2..3ec143f 100755 --- a/clients/send_nrdp.py +++ b/clients/send_nrdp.py @@ -45,9 +45,12 @@ # ############################################################################# - -import argparse, sys, html -from urllib import request as UrlRequest, parse as UrlParse +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: From 17d79ab6dbdbafbae6ba4d29872f12f7a1c48758 Mon Sep 17 00:00:00 2001 From: Xavier Bachelot Date: Wed, 14 Aug 2019 11:43:34 +0200 Subject: [PATCH 3/3] Whitespace fixes --- clients/send_nrdp.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/clients/send_nrdp.py b/clients/send_nrdp.py index 3ec143f..52fe16b 100755 --- a/clients/send_nrdp.py +++ b/clients/send_nrdp.py @@ -1,6 +1,6 @@ #!/usr/bin/env python3 -############################################################################## + ############################################################################# # # # send_nrdp.py - Send host/service checkresults to NRDP with XML @@ -56,7 +56,7 @@ 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.") @@ -110,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: @@ -122,7 +122,7 @@ def run(self): sys.exit() except Exception as e: sys.exit(e) - + def getText(self, nodelist): rc = [] for node in nodelist: @@ -153,7 +153,7 @@ def setup(self, options): 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"; @@ -173,7 +173,7 @@ def setup(self, options): xml += ""+html.escape(parts[2],True)+"" xml += "" xml += "" - + elif options.hostname and options.state: xml="\n\n"; if options.service: