1
0
Fork 1
mirror of https://github.com/thatmattlove/hyperglass.git synced 2026-01-17 08:48:05 +00:00

closes #262: fix mikrotik error

This commit is contained in:
thatmattlove 2024-06-16 16:54:14 -04:00
parent e19dd675e5
commit 30fda91bc8

View file

@ -37,45 +37,46 @@ class MikrotikGarbageOutput(OutputPlugin):
result = () result = ()
for each_output in output: for each_output in output:
if each_output.split()[-1] in ("DISTANCE", "STATUS"): if len(each_output) != 0:
# Mikrotik shows the columns with no rows if there is no data. if each_output.split()[-1] in ("DISTANCE", "STATUS"):
# Rather than send back an empty table, send back an empty # Mikrotik shows the columns with no rows if there is no data.
# response which is handled with a warning message. # Rather than send back an empty table, send back an empty
each_output = "" # response which is handled with a warning message.
else: each_output = ""
remove_lines = () else:
all_lines = each_output.splitlines() remove_lines = ()
# Starting index for rows (after the column row). all_lines = each_output.splitlines()
start = 1 # Starting index for rows (after the column row).
# Extract the column row. start = 1
column_line = " ".join(all_lines[0].split()) # Extract the column row.
column_line = " ".join(all_lines[0].split())
for i, line in enumerate(all_lines[1:]): for i, line in enumerate(all_lines[1:]):
# Remove all the newline characters (which differ line to # Remove all the newline characters (which differ line to
# line) for comparison purposes. # line) for comparison purposes.
normalized = " ".join(line.split()) normalized = " ".join(line.split())
# Remove ansii characters that aren't caught by Netmiko. # Remove ansii characters that aren't caught by Netmiko.
normalized = re.sub(r"\\x1b\[\S{2}\s", "", normalized) normalized = re.sub(r"\\x1b\[\S{2}\s", "", normalized)
if column_line in normalized: if column_line in normalized:
# Mikrotik often re-inserts the column row in the output, # Mikrotik often re-inserts the column row in the output,
# effectively 'starting over'. In that case, re-assign # effectively 'starting over'. In that case, re-assign
# the column row and starting index to that point. # the column row and starting index to that point.
column_line = re.sub(r"\[\S{2}\s", "", line) column_line = re.sub(r"\[\S{2}\s", "", line)
start = i + 2 start = i + 2
if "[Q quit|D dump|C-z pause]" in normalized: if "[Q quit|D dump|C-z pause]" in normalized:
# Remove Mikrotik's unhelpful helpers from the output. # Remove Mikrotik's unhelpful helpers from the output.
remove_lines += (i + 1,) remove_lines += (i + 1,)
# Combine the column row and the data rows from the starting # Combine the column row and the data rows from the starting
# index onward. # index onward.
lines = [column_line, *all_lines[start:]] lines = [column_line, *all_lines[start:]]
# Remove any lines marked for removal and re-join with a single # Remove any lines marked for removal and re-join with a single
# newline character. # newline character.
lines = [line for idx, line in enumerate(lines) if idx not in remove_lines] lines = [line for idx, line in enumerate(lines) if idx not in remove_lines]
result += ("\n".join(lines),) result += ("\n".join(lines),)
return result return result