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