-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmac_address_search.py
62 lines (44 loc) · 2.09 KB
/
mac_address_search.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
from nornir import InitNornir
from nornir_scrapli.tasks import send_command
import sys
def get_host_data(task, command):
"""function gets and parses outputs from devices. Client - scrapli, parser - textfsm """
try:
result = task.run(task=send_command, command=command)
structured_result = dict((k, v.scrapli_response.textfsm_parse_output()) for k, v in result.items())
except Exception as err:
print(f'Unable to pull data or parse it\n Reason is: {err}')
sys.exit()
return structured_result
def search_mac_address(task, mac):
"""search for mac address in collected data"""
# pull 'show mac address-table' output and parse it via textfsm
structured_mac_table = get_host_data(task, 'show mac address-table')
# pull 'show interface status' output and parse it
structured_interface_status = get_host_data(task, 'show interface status')
# pull 'show interfaces' output and parse it
structured_interfaces = get_host_data(task, 'show interfaces')
for host in task.inventory.hosts.keys():
for record in structured_mac_table[host]:
if record['destination_address'] == mac:
for interface in structured_interface_status[host]:
if interface['port'] == record['destination_port'] and interface['vlan'] != 'trunk':
return host, interface['port']
for interface in structured_interfaces[host]:
if interface['address'] == mac:
return host, interface['interface']
print(f'mac address {mac} was not found in the domain')
sys.exit()
def main():
"""main function"""
try:
mac_address = sys.argv[1]
except IndexError:
print('Enter a mac address to search for in the following format - xxxx.yyyy.zzzz')
sys.exit(1)
nr = InitNornir(config_file='nornir_lab_config.yaml')
switches = nr.filter(type='switch')
switch, interface = search_mac_address(switches, mac_address)
print(f'MAC Address {mac_address} is connected to switch {switch} interface {interface}')
if __name__ == '__main__':
main()