-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathget_l3_facts.py
194 lines (176 loc) · 5.73 KB
/
get_l3_facts.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#!/usr/bin/env python3
import argparse
import csv
from concurrent.futures import ThreadPoolExecutor
import getpass
import ipaddress
import napalm
import os
import tqdm
def get_args():
"""
Function to gather all of the needed arguments
Returns supplied args
"""
parser = argparse.ArgumentParser()
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument(
"--hosts",
"-H",
help="Comma-delimited list of IPs and/or FQDNs to query",
type=arg_list,
)
group.add_argument(
"--input",
"-i",
help="Text file with list of IPs and/or FQDNs to query (one per line)",
type=str,
)
parser.add_argument(
"--username", "-u", help="Username to use when logging into HOSTS"
)
parser.add_argument(
"--password", "-p", help="Password to use when logging into HOSTS"
)
parser.add_argument("--secret", "-s", help="Enable secret to pass to NAPALM")
parser.add_argument(
"--output",
"-o",
help="Full path to save CSV output to (default: ./l3_facts.csv)",
dest="csv_path",
default="./l3_facts.csv",
)
parser.add_argument(
"--driver",
help="Network driver for NAPALM to use (default: ios)",
default="ios",
)
parser.add_argument(
"--ssh-config",
"-c",
help="SSH config file for NAPALM to use (default: ~/.ssh/config)",
default="~/.ssh/config",
dest="ssh_config",
)
parser.add_argument(
"--max-threads",
"-t",
help="Maximum number of concurrent connections. (default: Python version default)",
default=None,
dest="threads",
)
parser.add_argument(
"--timeout",
"-T",
help="Connection timeout (sec) to pass to NAPALM. (default: 120s)",
default=120,
dest="timeout",
)
args = parser.parse_args()
if args.input:
if not os.path.exists(args.input):
raise FileNotFoundError(f"File {args.input} does not exist")
if not args.username:
username = getpass.getuser()
args.username = input(f"Username [{username}]: ") or username
if not args.password:
args.password = getpass.getpass()
if not args.secret:
args.secret = getpass.getpass("Enable secret: ")
if not (args.hosts or args.input):
parser.error(
"No host input, cannot continue. Must provide either --hosts or --input."
)
exit(1)
return args
def arg_list(string):
return string.split(",")
def open_device(host, driver, username, password, secret, timeout, ssh_config):
""" Opens connection to host and returns NAPALM object """
napalm_driver = napalm.get_network_driver(driver)
device = napalm_driver(
hostname=host,
username=username,
password=password,
timeout=timeout,
optional_args={"secret": secret, "ssh_config_file": ssh_config},
)
device.open()
return device
def get_iface_facts(host, args):
""" Returns formatted interface facts for a single device """
device = open_device(
host,
args.driver,
args.username,
args.password,
args.secret,
args.timeout,
args.ssh_config,
)
ifaces = device.get_interfaces()
ifaces_ip = device.get_interfaces_ip()
device.close()
results = []
for iface, attrs in ifaces.items():
if ifaces_ip.get(iface):
attrs.update(ifaces_ip.get(iface))
ipv4 = attrs.get("ipv4")
for address, prefix in ipv4.items():
prefix_length = prefix.get("prefix_length")
cidr = "{}/{}".format(address, prefix_length)
addr = ipaddress.ip_interface(cidr)
network = str(addr.network)
netmask = str(addr.netmask)
results.append(
{
"hostname": device.hostname,
"interface": iface,
"description": attrs.get("description"),
"address": address,
"prefix_length": prefix_length,
"cidr": cidr,
"network": network,
"netmask": netmask,
"is_enabled": attrs.get("is_enabled"),
"is_up": attrs.get("is_up"),
}
)
return results
def save_csv(csv_path, output):
""" Saves output to CSV """
fieldnames = [
"hostname",
"interface",
"address",
"prefix_length",
"cidr",
"netmask",
"network",
"is_enabled",
"is_up",
"description",
]
with open(csv_path, "w", newline="") as f:
writer = csv.DictWriter(f, fieldnames=fieldnames)
writer.writeheader()
writer.writerows(output)
def main():
""" Save L3 interface info from a collection of network devices to CSV. """
args = get_args()
results = []
hosts = []
if args.input:
with open(args.input, "r") as f:
hosts = f.read().splitlines()
else:
hosts = args.hosts
with ThreadPoolExecutor(max_workers=args.threads) as pool:
threads = []
for host in hosts:
threads.append(pool.submit(get_iface_facts, host, args))
for thread in tqdm.tqdm(threads, desc="Progress", unit="Device", ascii=True):
results = results + thread.result()
save_csv(args.csv_path, results)
if __name__ == "__main__":
main()