Created
November 21, 2019 09:33
-
-
Save dovideh/8d62664662447d81e2f0facca0467831 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import csv | |
import sys | |
import getopt | |
import regex | |
devices = [] | |
newdevices = {} | |
def loadDevices(file): | |
global devices | |
with open (file) as f: | |
devices.append ( f.read().splitlines() ) | |
return devices | |
def loadDictNew(): | |
file='Echipamente.txt' | |
global newdevices | |
with open(file) as f: | |
newdevices = dict(x.rstrip().split(';', 1) for x in f) | |
return newdevices | |
loadDictNew() | |
def unique(a): | |
i = 0 | |
while i < len(a): | |
j = i + 1 | |
while j < len(a): | |
if a[i] == a[j]: | |
del a[j] | |
else: | |
j += 1 | |
i += 1 | |
interfaces = ['et-','xe-','fe-','ge-','ae'] | |
remoteEquipmentCode = '<RE=' | |
remotePhysicalInterface = '<RI=' | |
ifaceType = '<TI=' | |
administrativeInternalId = '<MM=' | |
serviceKey = '<SRV' | |
ifaceComment = '<CMT=' | |
def usage(): | |
print(sys.argv[0] + ' -i CSV File (txt,csv) | -n nodeName | -e equipments ') | |
pattern = regex.compile( | |
""" | |
( (?:SRBB|SRBM|SROM|SBT)+\d{3}| | |
MSW\d\w{1}_?[\w|\d]{2,3}\d| | |
LB[G|S][I|N]\d{3}| | |
ICX\d{2}[\-]?\w{2}[\-]?\w{3}| | |
RR[I|V]\d{2}[\-]?\w{2}[\-]?\w{3}| | |
L[(S|E)]R\d{2}[\-|\_]?\w{2}[\-|\_]?\w{3}| | |
[A-Z]{2}\d{4}-?[A-Z]{3}\d?\d?| | |
SW?S?[(\d|I|N|OM)]+\d{3}\b| | |
FW[(G|S|O)(X|G|W|N|M)]+\d{3}| | |
CBR[0-9][0-9]-?(?!xe)\w?\w?-?(?!xe)\w?\w?\w?| | |
AGW\d{2}-\d-\w{2}-\w{3}| | |
ACC\d{2}-\d-\w{2}-\w{3}| | |
AGR\d{2}-?\d?-\w{2}-\w{3}| | |
DCL\d{2}-\w{2}-\w{3}| | |
DCS\d{2}-\w{2}-\w{3} ).*? | |
""",regex.M|regex.I|regex.X) | |
#( (?<![A-Za-z0-9]) [(i|a|x|f|g|T|e)][(e|t|rb)] (?!\_) [(-|\s)]? \/?\d?\/?\d?\/?\d? (?![a-z]) (?!\ ) (?!\_) (?!\-) .?\d+]? ) | |
ifacePattern = regex.compile ( | |
""" | |
( (?<![A-Za-z0-9]) (?:GigabitEthernet\d?\/?\d?\/?\d?) | [(i|a|x|f|g|T|e)][(e|t|rb)] (?!\_) [(-|\s)]? \/?\d?\/?\d?\/?\d? (?![a-z]) (?!\ ) (?!\_) (?!\-) .?\d+]? ) | |
""" | |
, regex.M|regex.X) | |
#, regex.M|regex.I|regex.X) | |
#(X16-|OSP|TdE|CNK|XTRA|TESA|ESE|XE_|TME|Admin_GE|BLK|XFE(R)?|ONO).?.* | |
llProvider = regex.compile ( | |
""" | |
(X16-|OSP|TdE|CNK|XTRA-?|TESA|ESE|XE_|TME|FOPT0|Admin_GE|BLK|XFER?|ONO).+?(?=(\;|\Z)) | |
""" | |
, regex.M|regex.I|regex.X) | |
#ifaceDescription = 'set interfaces ' + name + ' description' | |
def readme(file): | |
with open(file) as csv_file: | |
csv_reader = csv.reader(csv_file, delimiter=',') | |
line_count = 0 | |
devicename = "" | |
x = "" | |
interface = "" | |
for num,line in enumerate(csv_reader): | |
if not any (line[0] in devicename for word in devicename): | |
print("Configuration for "+ line[0] + ":\n") | |
devicename +=''.join(line[0]+'\n') | |
ifaceMatch = regex.findall(ifacePattern,line[2]) | |
deviceMatch = regex.findall(pattern,line[2]) | |
llmatch = regex.search(llProvider,line[2]) | |
def printNullRI(): | |
print(line[0]+","+line[1]+","+','.join(deviceMatch)+",NULL RI,") | |
def printNullRE(): | |
#print(line[0]+","+line[1]+",NULL RE,"+ifaceMatch[0]+",") | |
print("set interfaces "+line[1]+" description <RE=NULL" + "><RI=" + ifaceMatch[0] + "><MM_ADM="+ "NULL>" ) | |
def printLLNum(): | |
#print(line[0]+","+line[1]+","+','.join(deviceMatch)+","+','.join(ifaceMatch)+","+llmatch[0]) | |
print("set interfaces "+line[1]+" description <RE==" + deviceMatch[0] + "><RI=" + ifaceMatch[0] + "><MM_ADM="+ llmatch[0] + ">" ) | |
def printNULLll(): | |
#print(line[0]+","+line[1]+","+','.join(deviceMatch)+","+','.join(ifaceMatch)+","+"NULL LL") | |
print("set interfaces "+line[1]+" description <RE=" + deviceMatch[0] + "><RI=" + ifaceMatch[0] + "><MM_ADM="+ "NULL>" ) | |
print (list(v for k, v in newdevices.items() if line[2] in k )) | |
#print list(v for k, v in newdevices.iteritems() if line[0] in k.lower()) | |
#print (iter(newdevices.items())) | |
if ifaceMatch == None and deviceMatch != None: | |
printNullRI() | |
elif ifaceMatch != None and deviceMatch == None: | |
try: | |
printNullRE() | |
except IndexError: | |
print("IndexError") | |
elif ifaceMatch != None and deviceMatch != None: | |
try: | |
if deviceMatch[0] != line[0]: | |
try: | |
printLLNum() | |
except TypeError: | |
printNULLll() | |
else: | |
continue | |
except IndexError: | |
continue | |
else: | |
print(line[0]+","+line[1]+","+line[2]) | |
def pprint(inputFile,node): | |
loadDictNew() | |
print(inputFile,node) | |
readme(inputFile) | |
def main(): | |
inputFile = '' | |
node = '' | |
equip = "" | |
try: | |
opts, args = getopt.getopt(sys.argv[1:], "i:n:e:v", ["input", "node=","equipments","verbose"]) | |
except getopt.GetoptError as err: | |
# print help information and exit: | |
print(err) # will print something like "option -a not recognized" | |
usage() | |
sys.exit(2) | |
output = None | |
verbose = False | |
for o, a in opts: | |
if o == "-v": | |
verbose = True | |
elif o in ("-h", "--help"): | |
usage() | |
sys.exit() | |
elif o in ("-i", "--input"): | |
output = inputFile = a | |
elif o in ("-n", "--node"): | |
output = node = a | |
elif o in ("-e", "--equip"): | |
output = equip = a | |
loadDevices(equip) | |
else: | |
assert False, "unhandled option" | |
pprint(inputFile,node) | |
name = '' | |
if __name__ == "__main__": | |
main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment