From 5172972fdf3887e1c21f599f7063c3fdee89d01e Mon Sep 17 00:00:00 2001 From: jingwenxie Date: Sat, 4 Dec 2021 03:08:44 +0800 Subject: [PATCH] Fix invalid output of syslog IPv6 servers (#1933) #### What I did Modify the filter function of `show runningconfiguration syslog` #### How I did it Filter by ending "]" rather than split by ":" #### How to verify it add a syslog v6 server `sudo config syslog add f587::1:1` run command `show runningconfiguration syslog` #### Previous command output (if the output of a command-line utility has changed) ``` admin@vlab-01:~$ show runningconfiguration syslog Syslog Servers ---------------- [10.0.0.5] [10.0.0.6] [f587 ``` #### New command output (if the output of a command-line utility has changed) ``` admin@vlab-01:~$ show runningconfiguration syslog Syslog Servers ---------------- [10.0.0.5] [10.0.0.6] [f587::1:1] ``` --- show/main.py | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/show/main.py b/show/main.py index 11c746378882..ded7843684a0 100755 --- a/show/main.py +++ b/show/main.py @@ -1358,16 +1358,32 @@ def show_run_snmp(db, ctx): @runningconfiguration.command() @click.option('--verbose', is_flag=True, help="Enable verbose output") def syslog(verbose): - """Show Syslog running configuration""" + """Show Syslog running configuration + To match below cases(port is optional): + *.* @IPv4:port + *.* @@IPv4:port + *.* @[IPv4]:port + *.* @@[IPv4]:port + *.* @[IPv6]:port + *.* @@[IPv6]:port + """ syslog_servers = [] syslog_dict = {} + re_ipv4_1 = re.compile(r'^\*\.\* @{1,2}(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})(:\d+)?') + re_ipv4_2 = re.compile(r'^\*\.\* @{1,2}\[(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\](:\d+)?') + re_ipv6 = re.compile(r'^\*\.\* @{1,2}\[([0-9a-fA-F:.]+)\](:\d+)?') with open("/etc/rsyslog.conf") as syslog_file: data = syslog_file.readlines() for line in data: - if line.startswith("*.* @"): - line = line.split(":") - server = line[0][5:] - syslog_servers.append(server) + if re_ipv4_1.match(line): + server = re_ipv4_1.match(line).group(1) + elif re_ipv4_2.match(line): + server = re_ipv4_2.match(line).group(1) + elif re_ipv6.match(line): + server = re_ipv6.match(line).group(1) + else: + continue + syslog_servers.append("[{}]".format(server)) syslog_dict['Syslog Servers'] = syslog_servers print(tabulate(syslog_dict, headers=list(syslog_dict.keys()), tablefmt="simple", stralign='left', missingval=""))