-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathip_route_checker.py
68 lines (63 loc) · 2.5 KB
/
ip_route_checker.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
"""
This script will take a target IP address and determine
how each device will process that IP address
"""
import getpass
import os
from ipaddress import ip_network, ip_address
from nornir import InitNornir
from nornir_scrapli.tasks import send_command
from rich import print as rprint
CLEAR = "clear"
os.system(CLEAR)
nr = InitNornir(config_file="config.yaml")
#The above line is telling nornir where the config file is located
user = input("Enter your username: ")
password = getpass.getpass(prompt="Enter your password: ")
nr.inventory.defaults.username = user
nr.inventory.defaults.password = password
#The above lines will prompt the user to enter their username and password and use that input to connect to the devices.
target = input("Enter the target IP: ")
ipaddr = ip_address(target)
my_list = []
def get_routes(task):
"""
Parse routing table and determine if target IP finds a match
"""
response = task.run(task=send_command, command="show ip route")
task.host["facts"] = response.scrapli_response.genie_parse_output()
prefixes = task.host["facts"]["default"]["address_family"]["ipv4"]["routes"]
for prefix in prefixes:
net = ip_network(prefix)
if ipaddr in net:
source_proto = prefixes[prefix]["source_protocol"]
if source_proto == "connected":
try:
outgoing_intf = prefixes[prefix]["next_hop"]["outgoing_interface"]
for intf in outgoing_intf:
exit_intf = intf
my_list.append(
f"{task.host} is connected to {target} via interface {exit_intf}"
)
except KeyError:
pass
else:
try:
next_hop_list = prefixes[prefix]["next_hop"]["next_hop_list"]
for key in next_hop_list:
next_hop = next_hop_list[key]["next_hop"]
exit_intf = next_hop_list[key]["outgoing_interface"]
my_list.append(
(
f"{task.host} can reach {target} via interface {exit_intf}"
f" ~~ next hop: {next_hop} ({source_proto})"
)
)
except KeyError:
pass
results = nr.run(task=get_routes)
if my_list:
sorted_list = sorted(my_list)
rprint(sorted_list)
else:
rprint(f"{target} is not reachable")