Skip to content

Commit 7e4eb10

Browse files
committed
Utilize regex for exact match
1 parent ac1f573 commit 7e4eb10

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

show/main.py

+18-7
Original file line numberDiff line numberDiff line change
@@ -1356,18 +1356,29 @@ def show_run_snmp(db, ctx):
13561356
@runningconfiguration.command()
13571357
@click.option('--verbose', is_flag=True, help="Enable verbose output")
13581358
def syslog(verbose):
1359-
"""Show Syslog running configuration"""
1359+
"""Show Syslog running configuration
1360+
To match below cases:
1361+
*.* @IPv4:port
1362+
*.* @[IPv4]:port
1363+
*.* @[IPv6]:port
1364+
"""
13601365
syslog_servers = []
13611366
syslog_dict = {}
1367+
re_ipv4_1 = re.compile(r'^\*\.\* @(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}):\d+')
1368+
re_ipv4_2 = re.compile(r'^\*\.\* @\[(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\]:\d+')
1369+
re_ipv6 = re.compile(r'^\*\.\* @\[([0-9a-fA-F:]+)\]:\d+')
13621370
with open("/etc/rsyslog.conf") as syslog_file:
13631371
data = syslog_file.readlines()
13641372
for line in data:
1365-
if line.startswith("*.* @"):
1366-
end = line.find("]")
1367-
if end == -1:
1368-
continue
1369-
server = line[5:end+1]
1370-
syslog_servers.append(server)
1373+
if re_ipv4_1.match(line):
1374+
server = re_ipv4_1.match(line).group(1)
1375+
if re_ipv4_2.match(line):
1376+
server = re_ipv4_2.match(line).group(1)
1377+
elif re_ipv6.match(line):
1378+
server = re_ipv6.match(line).group(1)
1379+
else:
1380+
continue
1381+
syslog_servers.append("[{}]".format(server))
13711382
syslog_dict['Syslog Servers'] = syslog_servers
13721383
print(tabulate(syslog_dict, headers=list(syslog_dict.keys()), tablefmt="simple", stralign='left', missingval=""))
13731384

0 commit comments

Comments
 (0)