-
Notifications
You must be signed in to change notification settings - Fork 0
/
cisco-arp.py
340 lines (294 loc) · 10.3 KB
/
cisco-arp.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
"""
# !!!!! Helper Script - Does not change the running config !!!!!
References:
https://stackoverflow.com/questions/6545023/how-to-sort-ip-addresses-stored-in-dictionary-in-python/6545090#6545090
https://stackoverflow.com/questions/20944483/python-3-sort-a-dict-by-its-values
https://docs.python.org/3.3/tutorial/datastructures.html
Read the inventory file and create arp.json
Example
device-inventory-gl2
command line (executed from the netmiko-gl\port-maps folder)
python arp.py -s gl2
If using vs code debug, the launch.json file will look like this
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"justMyCode": true,
"args": "-s gl2",
"justMyCode": true
}
]
}
read a file containing the output of "sh ip arp" and create a sorted list of IP addresses and
IP/Mac Address conbinations.
In this example "sh ip arp vl 250". Save output as arp.txt
Internet 10.53.250.4 3 1060.4b9f.62f8 ARPA Vlan250
Internet 10.53.250.1 - 0012.00f3.febf ARPA Vlan250
Internet 10.53.250.2 0 1060.4b9d.db68 ARPA Vlan250
Internet 10.53.250.12 0 d8d4.3c2e.4b32 ARPA Vlan250
Internet 10.53.250.15 0 d8d4.3c2e.4b31 ARPA Vlan250
Internet 10.53.250.11 0 d8d4.3c2e.4b2f ARPA Vlan250
Internet 10.53.250.10 0 d8d4.3c2e.4b30 ARPA Vlan250
Run the script. Output is a list of correctly sorted list of IPs and MAC Addresses.
Output
10.53.250.1
10.53.250.2
10.53.250.4
10.53.250.10
10.53.250.11
10.53.250.12
10.53.250.15
10.53.250.1 0012.00f3.febf
10.53.250.2 1060.4b9d.db68
10.53.250.4 1060.4b9f.62f8
10.53.250.10 d8d4.3c2e.4b30
10.53.250.11 d8d4.3c2e.4b2f
10.53.250.12 d8d4.3c2e.4b32
10.53.250.15 d8d4.3c2e.4b31
# !!!!! Helper Script - Does not change the running config !!!!!
"""
import argparse
import json
import os
import re
import struct
import sys
from socket import inet_aton, inet_ntoa
from icecream import ic
import manuf
__author__ = "Michael Hubbard"
__author_email__ = "michael.hubbard999@gmail.com"
__copyright__ = ""
__license__ = "Unlicense"
# -*- coding: utf-8 -*-
# cisco_Config_Pull.py
# Procurve Change Request data collection
# Created by Michael Hubbard on 2023-12-20.
# comment out ic.disable() and uncomment ic.enable() to use icecream
# ic.enable()
ic.disable()
def get_current_path():
current_path: str = os.getcwd()
return current_path
def create_filename(sub_dir1: str, extension: str = "", sub_dir2="") -> str:
"""
returns a valid path regardless of the OS
Args:
sub_dir1 (str): name of the sub directory off the cwd required
extension (str, optional): string appended after hostname - ex. -interface.txt
sub_dir2 (str, optional): if a nested sub_dir is used Defaults to "".
Returns:
str: full pathname of the file to be written
"""
current_path = os.getcwd()
extension = hostname + extension
int_report = os.path.join(current_path, sub_dir1, sub_dir2, extension)
return int_report
def ip2long(ip):
packed = inet_aton(ip)
lng = struct.unpack("!L", packed)[0]
return lng
def long2ip(lng):
packed = struct.pack("!L", lng)
ip = inet_ntoa(packed)
return ip
# imported from cisco-Config-pull
def remove_empty_lines(filename):
if not os.path.isfile(filename):
print("{} does not exist ".format(filename))
return
with open(filename) as filehandle:
lines = filehandle.readlines()
with open(filename, "w") as filehandle:
lines = filter(lambda x: x.strip(), lines)
filehandle.writelines(lines)
parser = argparse.ArgumentParser(
description="-s site, -c core hostname in a Core/IDF deployment"
)
parser.add_argument(
"-s",
"--site",
help="Site name - ex. HQ",
)
parser.add_argument(
"-c",
"--coreswitch", # Optional (but recommended) long version
default="",
help="Coreswitch hostname",
)
args = parser.parse_args()
site = args.site
core = args.coreswitch
if site is None:
print("-s site name is a required argument")
sys.exit()
else:
dev_inv_file = "device-inventory-" + site + ".csv"
loc = get_current_path()
# dev_inv_file = loc + dev_inv_file
# check if site's device inventory file exists
if not os.path.isfile(dev_inv_file):
print("{} doesn't exist ".format(dev_inv_file))
sys.exit()
remove_empty_lines(dev_inv_file)
with open(dev_inv_file) as devices_file:
fabric = devices_file.readlines()
# print("-" * (len(dev_inv_file) + 23))
print("-" * (len(loc) + len(dev_inv_file) + 23))
print(f"Reading devices from: {loc}\{dev_inv_file}")
print("-" * (len(loc) + len(dev_inv_file) + 23))
uptime = []
for line in fabric:
line = line.strip("\n")
hostname = line.split(",")[2]
if core:
hostname = core
# - - - - - - - - Old Code - - - -
# loc = get_current_path()
# loc = loc + "\\data\\"
# arp_file = loc + hostname + "-arp.txt"
# ic(arp_file)
# # end import
# - - - - - - - - Old Code - - - -
arp_file = create_filename("port-maps", "-arp.txt", "data")
# unindent the rest
print()
# create a blank list to accept each line in the file
data1 = []
try:
f = open(arp_file, "r")
except FileNotFoundError:
print(f"{arp_file} does not exist")
else:
# MAC addresses are expressed differently depending on the manufacture and even model of the device
# the formats that this script can parse are:
# 0a:0a:0a:0a:0a:0a, 0a-0a-0a-0a-0a-0a, 0a0a0a.0a0a0a0 and 0a0a0a-0a0a0a
# this should cover most Cisco and HP devices.
for line in f:
device_name_loc = line.find("#")
if device_name_loc != -1:
device_name = line[0:device_name_loc]
device_name = device_name.strip()
if line.find("0000.0000.0000") == 38:
continue
match_PC = re.search(r"([0-9A-F]{2}[-:]){5}([0-9A-F]{2})", line, re.I)
match_Cisco = re.search(r"([0-9A-F]{4}[.]){2}([0-9A-F]{4})", line, re.I)
match_HP = re.search(r"([0-9A-F]{6}[-])([0-9A-F]{6})", line, re.I)
# strip out lines without a mac address
if match_PC or match_Cisco or match_HP:
data1.append(line)
f.close
# Save the cleaned up arp records
# If the file has a hostname# at the top save it as
# hostname-arp.txt, else switch-arp.txt
try:
if "device_name" in locals():
save_device = device_name + "-arp.txt"
device_file = open(save_device, "w")
else:
save_device = create_filename("port-maps", "-arp.txt")
# device_name = "switch"
# save_device = device_name + "-arp.txt"
device_file = open(save_device, "w")
for line in data1:
device_file.write(line)
device_file.close()
except FileNotFoundError as fnf_error:
print(fnf_error)
sys.exit(0)
# string length
i = len(data1) - 1
d = i + 1
counter = 0
sItems = []
IPs = []
data = {}
data2 = {}
while counter <= i:
IP = data1[counter]
# Remove Enter
IP = IP.strip("\n")
Mac = IP
MacAndVlan = IP
ic(IP)
Vlan = Mac.find("Vlan")
Mac = Mac[38 : Vlan - 9].rstrip("")
MacAndVlan = MacAndVlan[38:70].rstrip("")
ic(MacAndVlan)
MacAndVlan = MacAndVlan.replace("ARPA Vlan", "")
# MacAndVlan = MacAndVlan.replace(' ARPA FastEther', '')
ic(MacAndVlan)
# Find the space after IP
AfterIP = IP.find(" ")
# Remove Characters After IP
IP = IP[0:AfterIP].rstrip("")
ic(IP)
# Remove Internet before IP
IP = IP.replace("Internet ", "")
ic(IP)
IPs.append(str(IP))
# Convert IP to a long so it can be sorted.
# See https://stackoverflow.com/questions/6545023/how-to-sort-ip-addresses-stored-in-dictionary-in-python/6545090#6545090
IP = ip2long(IP)
# add IP address and MAC to dictionary
data[IP] = Mac
data2[IP] = MacAndVlan
counter = counter + 1
# Sort IPs
IPs = sorted(IPs, key=lambda ip: struct.unpack("!L", inet_aton(ip))[0])
print("Number of IP Addresses: %s " % d)
for IP in IPs:
print(IP)
print()
print("Number of IP and MAC Addresses: %s " % d)
# Create an empty dictionary to hold mac-ip pairs. Will be used with macaddr.py to output ip with interface
Mac_IP = {}
s = [(k, data[k]) for k in sorted(data)]
for k, v in s:
# Convert IP back to dotted quad notation.
k = long2ip(k)
print(k, v)
Mac_IP[v] = k
if "1cc1.de43.aeb7" in Mac_IP:
print("The IP for MACA %s is %s" % (v, k))
# print(Mac_IP)
print()
print("Number of IP, MAC and VLAN: %s " % d)
s = [(k, data2[k]) for k in sorted(data2)]
for k, v in s:
k = long2ip(k)
print(k, v)
#
#
# look up manufacture from MAC
print()
#
p = manuf.MacParser()
# Print IP, MAC, Manufacture
print("Number of IP, MAC and Manufacture: %s " % d)
print()
s = [(k, data[k]) for k in sorted(data)]
for k, v in s:
# Convert IP back to dotted quad notation.
k = long2ip(k)
v = v.split()
v = v[0]
manufacture = p.get_manuf(v)
print(k, v, manufacture)
# Write the dictionary out as Mac2IP.json so that it can be used in macaddr.py
# mydatafile = hostname + "-Mac2IP.json"
mydatafile = create_filename("port-maps", "-Mac2IP.json")
with open(mydatafile, "w") as f:
json.dump(Mac_IP, f, indent=4)
# If this is a core/idf deployment stop here
if core:
break