From 481449e0598b6d2bbe59eb3ff665bd38558f805b Mon Sep 17 00:00:00 2001 From: Christian Theune Date: Fri, 31 May 2024 09:20:04 +0200 Subject: [PATCH] lldp-to-altname: fix quoting Some switches have weird characters in the LLDP info, we need to quote that. Fixes PL-132570 --- pkgs/fc/lldp-to-altname/fc-lldp-to-altname.py | 27 ++++++++++--------- 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/pkgs/fc/lldp-to-altname/fc-lldp-to-altname.py b/pkgs/fc/lldp-to-altname/fc-lldp-to-altname.py index 2637089dd..3f5c78acd 100755 --- a/pkgs/fc/lldp-to-altname/fc-lldp-to-altname.py +++ b/pkgs/fc/lldp-to-altname/fc-lldp-to-altname.py @@ -2,6 +2,7 @@ import argparse import json +import re import socket import subprocess import sys @@ -17,6 +18,10 @@ def has_known_prefix(name): return False +def quote_altname(name): + return re.sub(r"[^a-z0-9A-Z]+", "-", name) + + class Runner(object): def __init__(self, args): self.quiet = args.quiet @@ -72,15 +77,13 @@ def run(self): for iface in self.interfaces: oldnames[iface] = set() data = self.check_json_output("ip", "-j", "link", "show", iface) - for datum in data: - if "altnames" in datum: - names = [ - name - for name in datum["altnames"] - if has_known_prefix(name) - ] - oldnames[iface].update(names) + names = [ + name + for name in datum.get("altnames", []) + if has_known_prefix(name) + ] + oldnames[iface].update(names) lldpnames[iface] = set() data = self.check_json_output("lldpctl", "-f", "json", iface) @@ -90,11 +93,11 @@ def run(self): and iface in data["interface"] and "chassis" in data["interface"][iface] ): - switch_name = list(data["interface"][iface]["chassis"].keys())[ - 0 - ] + switch_name = list(data["interface"][iface]["chassis"])[0] port_name = data["interface"][iface]["port"]["id"]["value"] - lldpnames[iface].update([f"{switch_name}-port-{port_name}"]) + lldpnames[iface].update( + [quote_altname(f"{switch_name}-port-{port_name}")] + ) newnames = dict() for iface in self.interfaces: