From c37ec659ed6aa79442e3843afa557fc23776ec2a Mon Sep 17 00:00:00 2001 From: Adrian Woodley Date: Mon, 22 Jul 2019 17:11:25 +0800 Subject: [PATCH] Configure In-band managment working. Adding command line arguments. --- Aviat-config.py | 201 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 201 insertions(+) create mode 100755 Aviat-config.py diff --git a/Aviat-config.py b/Aviat-config.py new file mode 100755 index 0000000..bd36783 --- /dev/null +++ b/Aviat-config.py @@ -0,0 +1,201 @@ +#!/usr/bin/python3 + +import os +import sys +import socket +import telnetlib +import csv +import threading +import argparse +try: + import queue +except ImportError: + import Queue as queue + +PORT=26000 +NEXT=1 +WORKERS = 5 + +NetMask = "255.255.0.0" +Gateway = "10.250.0.0.255.255.0.0.0.172.16.0.1" +OldSubnet = "192.168" +NewSubnet = "172.16" +VLAN = 4095 + +parser = argparse.ArgumentParser(description='Scriptable configuration of Aviat Eclipse INUes.') +parser.add_argument('csv') + + +def Config(tn, Context, Command, Position = 0, Value = None, Reverse = False): + tn.write(("contextid %s\n" % Context).encode('ascii')) + tn.read_until(b'Command Executed. Status unknown.\n') + if Reverse: + Temp = Position + Position = Value + Value = Temp + if Value: + tn.write(('set %s %s %s\n' % (Command, Position, Value)).encode('ascii')) + else: + tn.write(('get %s %s\n' % (Command, Position)).encode('ascii')) + response = tn.read_until(b'\n').split() + if response[0].decode() == Command: + return response[-1] + else: + return False + +def CommitSave(tn): + Config(tn, "Terminal_I", "configCommitSave", 0, 1) + Config(tn, "Terminal_I", "configCommitSwitch", 0, 1) + +class Worker(threading.Thread): + + def __init__(self, INUEs, Passed, Failed): + self.__INUEs = INUEs + self.__Passed = Passed + self.__Failed = Failed + threading.Thread.__init__(self) + + def run(self): + while 1: + INUE = self.__INUEs.get() + if INUE is None: + self.__Passed.put(None) + self.__Failed.put(None) + break + + NewIP = INUE.replace(OldSubnet, NewSubnet) + + RSTPDAC = 0 + + try: + with telnetlib.Telnet(INUE, PORT) as tn: + for Slot in range(1,9): + if Config(tn, "Slot%s_I" % Slot, "ceConfigBridgeStpMode"): + RSTPDAC = Slot + + if not RSTPDAC: + self.__Failed.put(INUE) + continue + + commands = [ + ["Slot%s_I" % Slot, "ceConfigNmsVid", 0, VLAN], + ["Terminal_I", "ipConfigAdEntAddress", Slot + 1, NewIP], + ["Terminal_I", "ipConfigAdEntNetMask", Slot + 1, NetMask], + ["Terminal_I", "ipConfigAutoroutingOspfEnable", Slot + 1, 2], + ["Terminal_I", "ipCidrRouteIfIndex", Slot + 48, Gateway, True], + ] + + fail = False + + for command in commands: + if not Config(tn, *command): + self.__Failed.put(INUE) + fail = True + continue + + if fail: + continue + + CommitSave(tn) + + except OSError: + FailedQueue.put(INUE) + continue + + try: + with telnetlib.Telnet(NewIP, PORT) as tn: + self.__Passed.put(NewIP) + except OSError: + FailedQueue.put(INUE) + continue + +def Input(): + with open(inFilePath, 'r', newline='') as inFile: + csvReader = csv.DictReader(inFile) + + for row in csvReader: + INUeQueue.put(row['IP Address']) + inFile.close() + + for i in range(WORKERS): + INUeQueue.put(None) + + +def FailedOutput(): + Count = WORKERS + + # with open('/tmp/sc200.csv', 'w', newline='') as outFile: + # csvWriter = csv.DictWriter(outFile, fieldnames=fieldNames, + # delimiter=',', quotechar='"', + # quoting=csv.QUOTE_MINIMAL) + + # csvWriter.writeheader() + + print('Failed:') + + while 1: + INUE = FailedQueue.get() + if INUE is None: + if Count > 1: + Count -= 1 + # print(Count) + else: + break + else: + print(INUE) + +def PassedOutput(): + Count = WORKERS + + fieldNames = ['IP Address'] + with open('/tmp/aviat.csv', 'w', newline='') as outFile: + csvWriter = csv.DictWriter(outFile, fieldnames=fieldNames, + delimiter=',', quotechar='"', + quoting=csv.QUOTE_MINIMAL) + + csvWriter.writeheader() + + while 1: + INUE = PassedQueue.get() + if INUE is None: + if Count > 1: + Count -= 1 + else: + break + else: + csvWriter.writerow({"IP Address": INUE}) + + outFile.close() + + +INUeQueue = queue.Queue(0) +PassedQueue = queue.Queue(0) +FailedQueue = queue.Queue(0) + +# for i in range(WORKERS): +# Worker(INUeQueue, PassedQueue, FailedQueue).start() + +# for INUE in sys.argv: +# if NEXT: +# NEXT=0 +# else: +# INUeQueue.put(INUE) + +# for i in range(WORKERS): +# INUeQueue.put(None) + +inFilePath = sys.argv[1] + +Input() + +for i in range(WORKERS): + Worker(INUeQueue, PassedQueue, FailedQueue).start() + +if threading.current_thread() is not threading.main_thread(): + sys.exit(0) + +PassedOutput() +FailedOutput() + +sys.exit(0) +