#! /local/web/.local/bin/python3 import urllib.request import urllib.parse import xml.etree.ElementTree as ET import cgi import html import cgitb import re def isAValidMac(macAddress): if re.match("[0-9a-f]{2}([-:])[0-9a-f]{2}(\\1[0-9a-f]{2}){4}$", macAddress.lower()): return True else: return False cgitb.enable() # Enable Traceback via HTML theurl = 'https://tipsapi/config/write/Endpoint' username = '' password = '' # Constants xml_string = ''' ''' passman = urllib.request.HTTPPasswordMgrWithDefaultRealm() # this creates a password manager passman.add_password(None, theurl, username, password) # Because with have put None at the start it will always # use this username/password combination for urls # for which 'theurl' is a super-urllib authhandler = urllib.request.HTTPBasicAuthHandler(passman) # create the authhandler opener = urllib.request.build_opener(authhandler) urllib.request.install_opener(opener) # All calls to urllib.urlopen will now use our handler # Make sure not to include the protocol in the URL, or # HTTPPasswordMgrWithDefaultRealm will be very confused. # You must (of course) use it when fetching the page though. form = cgi.FieldStorage() if "macAddress" not in form or "disabledBy" not in form or "disabledReason" not in form or "IRDBTicket" not in form : # Send HTTP Headers print("Content-Type: text/html") print() # Start HTML print("") print("") print("") print("") print("ITS : NEG : Disable Clearpass Client") print("") print("") print("

Disable Clearpass Client

") print("

") print("

") print("Client MAC Address:
") print("") print("
") print("Client Disabled By:
") print("") print("
") print("IRDB Ticket#:
") print("") print("
") print("Client Disabled Reason:
") print("") print("

") print("") print("
") print("") print("") else: # Send HTTP Headers print("Content-Type: text/html") print() # Start HTML print("") print("") print("") print("") print("ITS : NEG : Clearpass Disable Client") print("") print("") print("

Clearpass Disable Client

") print("

") macAddress = html.escape(form.getfirst("macAddress", "")) disabledBy = html.escape(form.getfirst("disabledBy","")) disabledReason = html.escape(form.getfirst("disabledReason","")) IRDBTicket = html.escape(form.getfirst("IRDBTicket","")) if isAValidMac(macAddress) == True: macAddress = macAddress.replace(":", "") macAddress = macAddress.replace("-", "") macAddress = macAddress.lower() xml_string = xml_string.replace("[[MACADDRESS]]", macAddress) xml_string = xml_string.replace("[[DISABLEDBY]]", disabledBy) disabledReason = 'DISABLED:{0}:{1}'.format(IRDBTicket,disabledReason) xml_string = xml_string.replace("[[DISABLEDREASON]]", disabledReason) data = xml_string.encode('utf-8') # Encode string as bytes req = urllib.request.Request(theurl, data) req.add_header("Content-Type","application/x-www-form-urlencoded;charset=utf-8") # Create request pagehandle = urllib.request.urlopen(req) # Submit request and return results # authentication automatically handled for username namespace ='{http://www.avendasys.com/tipsapiDefs/1.0}' root = ET.fromstring(pagehandle.read().decode('utf-8')) # Decode bytes to String and parse XML elem = root.find("{0}StatusCode".format(namespace)) if elem.text == "Success": print("Success!!!") print("Mac Address: ", macAddress, "
") print("Disabled By: ", disabledBy, "
") print("Disabled Reason: ", disabledReason, "
") else: print("Error:
") errorCode = root.find("{0}TipsApiError/{0}ErrorCode".format(namespace)) print("Code: ", errorCode.text) print("

") errorMessage = root.find("{0}TipsApiError/{0}Message".format(namespace)) print("Message: ", errorMessage.text) print("

") else: print("Invalid MAC Address entered") print("") print("")