-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathduplicate_IP_detector.py
64 lines (58 loc) · 1.87 KB
/
duplicate_IP_detector.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
"""
Script will detect duplicate IP addresses within a topology.
"""
import logging
import os
from collections import Counter
from nornir import InitNornir
from nornir_scrapli.tasks import send_command
from rich import print as rprint
nr = InitNornir(config_file="config.yaml")
CLEAR = "clear"
os.system(CLEAR)
ip_list = []
def get_ip(task):
"""
Parse IP addresses from all interfaces and append to ip_list
"""
response = task.run(
task=send_command, command="show interfaces", severity_level=logging.DEBUG
)
task.host["facts"] = response.scrapli_response.genie_parse_output()
interfaces = task.host["facts"]
for intf in interfaces:
try:
ip_key = interfaces[intf]["ipv4"]
for ip in ip_key:
ip_addr = ip_key[ip]["ip"]
ip_list.append(ip_addr)
except KeyError:
pass
def locate_ip(task):
"""
Pull all interfaces information
Identify the interface and Device configured with duplicate address
"""
response = task.run(
task=send_command, command="show interfaces", severity_level=logging.DEBUG
)
task.host["facts"] = response.scrapli_response.genie_parse_output()
interfaces = task.host["facts"]
for intf in interfaces:
try:
ip_key = interfaces[intf]["ipv4"]
for ip in ip_key:
ip_addr = ip_key[ip]["ip"]
if ip_addr in targets:
rprint(f"[yellow]{task.host} {intf} - {ip_addr}[/yellow]")
except KeyError:
pass
nr.run(task=get_ip)
targets = [k for k, v in Counter(ip_list).items() if v > 1]
if targets:
rprint("[red]ALERT: DUPLICATES DETECTED![/red]")
rprint(targets)
rprint("\n[cyan]Locating addresses in topology...[/cyan]")
nr.run(task=locate_ip)
else:
rprint("[green]SCAN COMPLETED - NO DUPLICATES DETECTED[/green]")