Good to go!

This commit is contained in:
2019-07-23 16:24:45 +08:00
parent e1a0bfa34f
commit bb597ad323

View File

@@ -3,9 +3,11 @@
# import os
import sys
import logging
from dataclasses import dataclass
# import socket
import telnetlib
import re
import csv
import threading
import time
@@ -27,16 +29,26 @@ VLAN = 4095
ARG_PARSER = argparse.ArgumentParser(
description='Scriptable configuration of Aviat Eclipse INUes.')
ARG_PARSER.add_argument('--in', action="store", dest="in_file", required=True)
ARG_PARSER.add_argument('--out', action="store", dest="out_file", default="passed.csv")
ARG_PARSER.add_argument('-i', action="store", dest="in_file", required=True)
ARG_PARSER.add_argument('-o', action="store", dest="out_file", default="passed.csv")
ARG_PARSER.add_argument('--area', action="store", dest="ospf_area", default=None)
ARG_PARSER.add_argument('--cleanup', action="store_true")
ARG_PARSER.add_argument('--test', action="store_true")
ARG_PARSER.add_argument('--debug', action="store_true")
arguments = ARG_PARSER.parse_args()
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
INFILEPATH = arguments.in_file
OUTFILEPATH = arguments.out_file
OSPF_AREA = arguments.ospf_area
CLEANUP = arguments.cleanup
DEBUG = arguments.debug
TEST = arguments.test
def debug(message):
if DEBUG:
logging.debug(message)
@dataclass
class INUeCommand():
@@ -53,16 +65,18 @@ class INUeCommand():
def get_context(self):
"""Get the context of the command, ready to send."""
debug ("contextid %s\n" % self.context)
return ("contextid %s\n" % self.context).encode('ascii')
def get_command(self):
"""Get the command, as either a get or set, based on the value."""
if self.value:
print ('set %s %s %s\n' % (self.command, self.position, self.value))
return ('set %s %s %s\n' % (self.command, self.position, self.value)).encode('ascii')
else:
print ('get %s %s\n' % (self.command, self.position))
if self.value is None:
debug ('get %s %s\n' % (self.command, self.position))
return ('get %s %s\n' % (self.command, self.position)).encode('ascii')
else:
debug ('set %s %s %s\n' % (self.command, self.position, self.value))
return ('set %s %s %s\n' % (self.command, self.position, self.value)).encode('ascii')
def send_config(telnet, inue_command):
"""Send a config command to an INUe via a supplied telnet connection."""
@@ -70,15 +84,11 @@ def send_config(telnet, inue_command):
telnet.write(inue_command.get_context())
telnet.read_until(b'Command Executed. Status unknown.\n')
# if inue_command.value:
# telnet.write(('set %s %s %s\n' %
# (command, position, value)).encode('ascii'))
# else:
# telnet.write(('get %s %s\n' % (command, position)).encode('ascii'))
telnet.write(inue_command.get_command())
response = telnet.read_until(b'\n').split()
if response[0].decode() == inue_command.command:
return response[-1]
response = telnet.read_until(b'\n').decode()
if inue_command.command in response:
debug("Response: %s" % response.split(" = ",1)[-1])
return response.split(" = ",1)[-1]
else:
return False
@@ -91,6 +101,18 @@ def commit_save(telnet):
# send_config(telnet, "Terminal_I", "configCommitSwitch", 0, 1)
def get_district(site_name):
"""Determine the segment district from the site name."""
district = False
debug("Site Name: %s" % site_name)
if site_name:
district=re.split(r'[\d]+', site_name)[0]
if district.endswith('S') and "-" not in site_name:
district = district[:-1]
return district
class Worker(threading.Thread):
"""Class for worker threads which will process INUEs."""
@@ -114,12 +136,20 @@ class Worker(threading.Thread):
try:
with telnetlib.Telnet(inue, PORT) as telnet:
district = get_district(send_config(telnet,
INUeCommand("Terminal_I", "sysDetailsInfoSiteName")))
debug("District: %s" % district)
for slot in range(1, 10):
if send_config(telnet,
INUeCommand("Slot%s_I" % slot, "ceConfigBridgeStpMode")) == b'1':
if (send_config(telnet, INUeCommand(
"Slot%s_I" % slot, "ceConfigBridgeStpMode")) or
send_config(telnet, INUeCommand(
"Slot%s_I" % slot, "ceConfigPortMacLearning", 1)) == 2):
dac_name = send_config(telnet, INUeCommand(
"Terminal_I", "slotConfigName", slot))
debug("DAC: %s" % dac_name)
if district in dac_name:
rstp_dac = slot
break
print(rstp_dac)
if not rstp_dac:
self.__failed.put(inue)
@@ -131,15 +161,20 @@ class Worker(threading.Thread):
]
elif CLEANUP:
print('cleanup')
debug('cleanup')
commands = [
INUeCommand("Slot%s_I" % rstp_dac, "ceConfigNmsVid", 0, 0),
INUeCommand("Terminal_I", "ipConfigAdEntAddress", rstp_dac + 1, '0.0.0.0'),
INUeCommand("Terminal_I", "ipConfigAdEntNetMask", rstp_dac + 1, '255.255.255.255'),
INUeCommand("Slot%s_I" % rstp_dac, "ceConfigNmsVid", 0, 0),
# INUeCommand("Terminal_I", "ipConfigAutoroutingOspfEnable", rstp_dac + 1, 2),
# INUeCommand("Terminal_I", "ipCidrRouteIfIndex", rstp_dac + 48, GATEWAY, True),
]
elif TEST:
debug('test')
self.__passed.put({'IP Address': inue, 'RSTP DAC': rstp_dac})
continue
else:
commands = [
INUeCommand("Slot%s_I" % rstp_dac, "ceConfigNmsVid", 0, VLAN),
@@ -224,6 +259,8 @@ def passed_output():
count = WORKERS
field_names = ['IP Address']
if TEST:
field_names = ['IP Address', 'RSTP DAC']
with open(OUTFILEPATH, 'w', newline='') as out_file:
csv_writer = csv.DictWriter(out_file, fieldnames=field_names,
delimiter=',', quotechar='"',
@@ -238,6 +275,9 @@ def passed_output():
count -= 1
else:
break
else:
if TEST:
csv_writer.writerow(inue)
else:
csv_writer.writerow({"IP Address": inue})