Skip to content

Commit

Permalink
Utilize regex for exact match
Browse files Browse the repository at this point in the history
  • Loading branch information
wen587 committed Nov 25, 2021
1 parent ac1f573 commit 7e4eb10
Showing 1 changed file with 18 additions and 7 deletions.
25 changes: 18 additions & 7 deletions show/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -1356,18 +1356,29 @@ 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:
*.* @IPv4:port
*.* @[IPv4]:port
*.* @[IPv6]:port
"""
syslog_servers = []
syslog_dict = {}
re_ipv4_1 = re.compile(r'^\*\.\* @(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}):\d+')
re_ipv4_2 = re.compile(r'^\*\.\* @\[(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\]:\d+')
re_ipv6 = re.compile(r'^\*\.\* @\[([0-9a-fA-F:]+)\]:\d+')
with open("/etc/rsyslog.conf") as syslog_file:
data = syslog_file.readlines()
for line in data:
if line.startswith("*.* @"):
end = line.find("]")
if end == -1:
continue
server = line[5:end+1]
syslog_servers.append(server)
if re_ipv4_1.match(line):
server = re_ipv4_1.match(line).group(1)
if 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=""))

Expand Down

0 comments on commit 7e4eb10

Please sign in to comment.