-
Notifications
You must be signed in to change notification settings - Fork 0
/
Device_Locator
75 lines (74 loc) · 3.45 KB
/
Device_Locator
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
from netmiko import ConnectHandler, ssh_exception
import json
import re
import sys
import getpass
# Function to find mac address from all access devices
def FIND_MAC_L2(FOUNDED_MAC,HOST,DEV_VARS):
try:
# add device parametter to var(device) for connecthandler
DEV_VARS['host'] = HOST
Connect_to_l2device = ConnectHandler(**DEV_VARS)
get_hostname = Connect_to_l2device.send_command(
f'show version | in uptime')
#Get hostname with show version command, split with space and get first item in list
print(f'--- Hostname: {get_hostname.split(" ")[0]} ---')
#Find Mac address using Regular Expression
MAC_L2 = Connect_to_l2device.send_command(
f'show mac address-table address {FOUNDED_MAC}')
get_interface_name = re.search(r"\bFa.+[0-9]\b|\bGi.+[0-9]\b", MAC_L2)
if get_interface_name:
#Connected switches receive MAC addresses from their trunk ports!
check_trunk = Connect_to_l2device.send_command(
f'show running-config interface {get_interface_name.group()}')
get_description = Connect_to_l2device.send_command(
f'show interface {get_interface_name.group()} description')
if 'trunk' not in check_trunk:
print('----------------------------------------------------------------------------')
print(get_description)
print('----------------------------------------------------------------------------')
sys.exit('END')
else:
print('MAC address on this device received via trunk port!')
print('show interface description: ')
print('----------------------------------------------------------------------------')
print(get_description)
print('----------------------------------------------------------------------------')
else:
print('Mac address not found in this device!')
except (ssh_exception.AuthenticationException, EOFError):
print(f'Authentication Error')
except ssh_exception.NetmikoTimeoutException:
print(f'Connection Timeout')
except:
raise
#Open JSON file to get list of network devices
with open('hosts.json','rt') as FILE:
NETWORK_DEVICES = json.load(FILE)
print('Username: ')
USERNAME = (str(input()))
PASSWORD = getpass.getpass(prompt='Password: ', stream=None)
L3_SWITCH = '172.16.1.1'
IOS_VARS = {'username': USERNAME,'password': PASSWORD , 'device_type': 'cisco_ios'}
print('Enter IP to find location:')
TARGET_IP = (str(input()))
try:
try:
print('Connecting to the device: ' + L3_SWITCH)
IOS_VARS['host'] = L3_SWITCH
Connect_To_Device = ConnectHandler(**IOS_VARS)
Connect_To_Device.enable()
GET_ARP_RESULT = Connect_To_Device.send_command(
'sh ip arp ' + TARGET_IP)
if FOUNDED_MAC := re.search("[0-9a-fA-F]{4}[.][0-9a-fA-F]{4}[.][0-9a-fA-F]{4}", GET_ARP_RESULT):
print(f'Mac address of {TARGET_IP} is {FOUNDED_MAC.group()}')
for HOST in NETWORK_DEVICES:
print('Connecting to the device: ' + HOST)
FIND_MAC_L2(FOUNDED_MAC.group(), HOST,IOS_VARS)
except (ssh_exception.AuthenticationException, EOFError):
print(f'Authentication Error')
except ssh_exception.NetmikoTimeoutException:
print(f'Connection Timeout')
except IndexError:
pass
FILE.close()