-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex1.py
134 lines (100 loc) · 3.86 KB
/
ex1.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
#!/usr/bin/env python
'''
this script depends on the paramiko and netmiko python modules
netmiko https://github.com/ktbyers/netmiko
paramiko https://github.com/paramiko/paramiko
usage:
$ python ex1.py
Enter the IP Address: 10.1.1.11
Querying routers...
Two arp entries found.
Routers see 10.1.1.11 with MAC address 0800.2726.4a23
'''
import re
import netmiko
# Turn on verbose screen output
DEBUG = False
# Ask the user the IP address of the host to search/display
IP_ADDRESS = raw_input('Enter the IP Address: ')
# Define the routers that route host networks (access routers)
# Nested dictionary defining the router paramters to pass into
# the netmiko-paramiko module to establish an SSH connection
ROUTERS = {
'R1': {
'ip': '10.0.0.2',
'device_type': 'cisco_ios',
'username': 'admin',
'password': 'cisco',
'secret': 'cisco',
'verbose': False
},
'R2': {
'ip': '10.0.0.3',
'device_type': 'cisco_ios',
'username': 'admin',
'password': 'cisco',
'secret': 'cisco',
'verbose': False
}
}
def connect_to_device(device_params, command):
'''
create a function to utilize the netmiko python module
pass in the device paramaters (ip, username, password)
issue the command passed into the function
set DEBUG to true to get entire command output
'''
SSHClass = netmiko.ssh_dispatcher(device_type=device_params['device_type'])
net_connect = SSHClass(**device_params)
output = net_connect.send_command(command)
# print the output of the command if DEBUG is enabled.
if DEBUG:
print "-" * 20 + "[Command Output Debug]" + "-" * 20
print output
print "-" * 62 + "\n"
return output
def get_mac_from_routers():
'''
show ip arp 10.0.0.5
Protocol Address Age (min) Hardware Addr Type Interface
Internet 10.0.0.5 5 0800.2725.d985 ARPA FastEthernet0/0
'''
# Create a list to store all the MAC addresses found.
macs_found = []
# Connect to each router, issue show ip arp {ip_address}
print "Querying routers..."
for router in ROUTERS:
arp_entry = connect_to_device(ROUTERS[router], "show ip arp {}".format(IP_ADDRESS))
# Find the MAC address in the screen output
if re.search(r'(..............) ARPA', arp_entry):
match = re.search(r'(..............) ARPA', arp_entry)
# Save the MAC address found to the mac_address object
mac_address = match.group(1)
# DEBUG: Print the information found for this router.
if DEBUG:
print "\nRouter: {}\
\nIP Address {} has MAC address {}\n".format(router, IP_ADDRESS, mac_address)
# Add the MAC addres found to the macs_found list
macs_found.append(mac_address)
else:
if DEBUG:
print "\nRouter: {}\
\nMAC address not found.".format(router)
# Determine how many MAC addresses are found and compare results
if len(macs_found) == 2:
print "Two arp entries found."
if macs_found[0] == macs_found[1]:
print "Routers see {} with MAC address {}".format(IP_ADDRESS, mac_address)
return macs_found[0]
else:
print "[WARNING] Two different MAC addresses found! {}".format(macs_found)
return None
elif len(macs_found) == 1:
print "One arp entry found.\
\nRouter sees {} with MAC address {}".format(IP_ADDRESS, mac_address)
return macs_found[0]
else:
print "IP Address is not seen on the network. (No ARP entries found)"
return None
if __name__ == "__main__":
get_mac_from_routers()