From d4265fa406ceb01ce0e281c4d24a3b5163d2af0b Mon Sep 17 00:00:00 2001 From: Jarrod Johnson Date: Thu, 28 Sep 2017 16:28:33 -0400 Subject: [PATCH] Normalize likely HEX strings Chassis Id and Port Id are highly likely to present as hex strings, detect and present this case more sanely. --- confluent_server/confluent/networking/lldp.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/confluent_server/confluent/networking/lldp.py b/confluent_server/confluent/networking/lldp.py index 0147c331..0b7429b0 100644 --- a/confluent_server/confluent/networking/lldp.py +++ b/confluent_server/confluent/networking/lldp.py @@ -116,6 +116,15 @@ def _extract_extended_desc(info, source, integritychecked): else: info['peerdescription'] = source +def sanitize(val): + val = str(val) + for x in val.strip('\x00'): + if ord(x) < 32 or ord(x) > 128: + val = ':'.join(['{0:02x}'.format(ord(x)) for x in str(val)]) + break + return val + + def _extract_neighbor_data_b(args): """Build LLDP data about elements connected to switch @@ -144,12 +153,12 @@ def _extract_neighbor_data_b(args): iname = idxtoifname[remotename[0][-2]] if iname not in lldpdata: lldpdata[iname] = {} - lldpdata[iname]['peerport'] = str(remotename[1]) + lldpdata[iname]['peerport'] = sanitize(remotename[1]) for remoteid in conn.walk('1.0.8802.1.1.2.1.4.1.1.5'): iname = idxtoifname[remoteid[0][-2]] if iname not in lldpdata: lldpdata[iname] = {} - lldpdata[iname]['peerchassisid'] = str(remoteid[1]) + lldpdata[iname]['peerchassisid'] = sanitize(remoteid[1]) _neighdata[switch] = lldpdata