diff --git a/devices.py b/devices.py new file mode 100755 index 0000000..2bd93b0 --- /dev/null +++ b/devices.py @@ -0,0 +1,45 @@ +#!/usr/bin/python3 + +from netaddr import IPNetwork +import csv +import sys + +Section = "CDRN / TRN WAN" + +fields = [ + 'Section', + 'IP address', + 'Description', + 'Subnet', + 'Hostname' +] + + +inFilePath = sys.argv[1] + +if len(sys.argv) == 3: + outFile = open(sys.argv[2], 'w') +else: + outFile = sys.stdout + +csvWriter = csv.DictWriter(outFile, fieldnames=fields, + delimiter=',', quotechar='"', + quoting=csv.QUOTE_MINIMAL) + +csvWriter.writeheader() + +with open(inFilePath, 'r', newline='') as inFile: + csvReader = csv.DictReader(inFile) + outRow = {} + + for inRow in csvReader: + outRow['Section'] = Section + outRow['IP address'] = inRow['IP-Address'] + outRow['Description'] = inRow['Site-Name'] + outRow['Subnet'] = str(IPNetwork(inRow['IP-Address'] + '/' + inRow['Subnet-Mask']).cidr) + outRow['Hostname'] = inRow['Site-Name'].split()[0] + '-SC200' + + csvWriter.writerow(outRow) + inFile.close() + +outFile.close() diff --git a/sc200-pull.py b/sc200-pull.py index 4449256..1ecf1d6 100755 --- a/sc200-pull.py +++ b/sc200-pull.py @@ -1,6 +1,6 @@ #!/usr/bin/python3 from netaddr import IPNetwork -#from netaddr import IPAddress +from ipaddress import IPv4Address from xmlrpc.client import ServerProxy #import sqlite3 import socket @@ -59,6 +59,9 @@ Settings = [ 'Site-Address', 'System-Location', 'Site-Notes', + 'IP-Address', + 'Subnet-Mask', + 'Gateway-Address', 'Float-Voltage', 'Battery-Capacity', 'AC-Rectifier-Current-Limit', @@ -92,6 +95,11 @@ Settings = [ ['Alarm-Severity'] + \ ['Alarm-Severity:' + repr(x) for x in range(1, 59)] +NetworkSettings = [ + 'IP-Address', + 'Subnet-Mask', + 'Gateway-Address' +] def GetStatusCode(host, path="/"): try: @@ -134,7 +142,12 @@ class Worker(threading.Thread): else: break - if len(Values) > 0: + if len(Values) > 0: + for idx, setting in enumerate(Settings): + if setting in NetworkSettings: + print(Values[idx]) + Values[idx] = str(IPv4Address(Values[idx])) + Values.insert(0, str(IP)) self.__Results.put(Values) else: diff --git a/subnets.py b/subnets.py new file mode 100755 index 0000000..a017778 --- /dev/null +++ b/subnets.py @@ -0,0 +1,53 @@ +#!/usr/bin/python3 + +from netaddr import IPNetwork +import csv +import sys +import re + +Section = "CDRN / TRN WAN" + +fields = [ + 'Section', + 'Subnet', + 'Mask', + 'Description', +] + +subnets = [] + +inFilePath = sys.argv[1] + +if len(sys.argv) == 3: + outFile = open(sys.argv[2], 'w') +else: + outFile = sys.stdout + +csvWriter = csv.DictWriter(outFile, fieldnames=fields, + delimiter=',', quotechar='"', + quoting=csv.QUOTE_MINIMAL) + +csvWriter.writeheader() + +with open(inFilePath, 'r', newline='') as inFile: + csvReader = csv.DictReader(inFile) + outRow = {} + + for inRow in csvReader: + outRow['Section'] = Section + subnet = IPNetwork(inRow['IP-Address'] + '/' + inRow['Subnet-Mask']).cidr + if subnet in subnets: + continue + subnets.append(subnet) + outRow['Subnet'] = str(subnet) + outRow['Mask'] = inRow['Subnet-Mask'] + try: + outRow['Description'] = re.search(r"(([a-zA-Z]+S\d)|(C[a-zA-Z]+\d+)|(T[a-zA-Z]+)|([a-zA-Z]+(-Core)?))([\- ]+\d )?.*", inRow['Site-Name']).group(1) + '-SC200' + except AttributeError: + print(inRow['Site-Name']) + outRow['Description'] = inRow['Site-Name'] + + csvWriter.writerow(outRow) + inFile.close() + +outFile.close()