-
Notifications
You must be signed in to change notification settings - Fork 2
/
parse_with_regex.py
executable file
·74 lines (61 loc) · 2.07 KB
/
parse_with_regex.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/env python3
"""
Example code for Network-Notes.com entry on Parsing Network Device Output
"""
import getpass
import re
import sys
from netmiko import ConnectHandler
"""
Test parsing network device configuration
"""
# Gather the needed credentials
net_device_username = input("Username: ")
net_device_password = getpass.getpass()
# Set enable/secret = password for now
net_device_secret = net_device_password
# Setup a dict with our ASAvs in it, in real world this could be read
# from a CSV or the CLI or any other source
firewalls = {
"ASAv1": {"ip": "10.10.10.50", "platform": "cisco_asa"},
"ASAv2": {"ip": "10.10.60.2", "platform": "cisco_asa"},
}
# Setup an empty dict for our results:
results = {}
# Instantiate netmiko connection objects and gather the output of
# `show version | inc So|Serial| up` on these two firewalls
for fw_name, fw_data in firewalls.items():
print(f"Connecting to {fw_name}...")
fw_connection = ConnectHandler(
ip=fw_data["ip"],
device_type=fw_data["platform"],
username=net_device_username,
password=net_device_password,
secret=net_device_secret,
)
results[fw_name] = fw_connection.send_command(
command_string="show version | inc So|Serial| up"
)
# Parse our results:
print("Parsing Results...")
parsed_results = {}
for fw_name, result in results.items():
parsed_results[fw_name] = {}
parsed_results[fw_name]["version"] = re.search(
r"Software Version (\d.\d{1,2}(?:\(?\d{1,2}?\)?\d{1,2}?)?)\W*$",
result,
re.MULTILINE,
)
parsed_results[fw_name]["uptime"] = re.search(
r"up (.*)$", result, re.MULTILINE
)
parsed_results[fw_name]["serial"] = re.search(
r"Serial Number: (\S*)$", result, re.MULTILINE
)
# Print our results
for fw_name in results.keys():
print(f"{fw_name} information:\n")
print(f"\tVersion: {parsed_results[fw_name]['version'].group(1)}")
print(f"\tUptime: {parsed_results[fw_name]['uptime'].group(1)}")
print(f"\tSerial Number: {parsed_results[fw_name]['serial'].group(1)}")
sys.exit()