-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patharp.py
211 lines (188 loc) · 6.83 KB
/
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
#!/opt/rh/rh-python35/root/usr/bin/python3.5
import sys
import json
import requests
import re
import ipaddress
from requests.packages.urllib3.exceptions import InsecurePlatformWarning
from requests.packages.urllib3.exceptions import InsecureRequestWarning
from requests.packages.urllib3.exceptions import SNIMissingWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
requests.packages.urllib3.disable_warnings(InsecurePlatformWarning)
requests.packages.urllib3.disable_warnings(SNIMissingWarning)
from napalm import get_network_driver
driver = get_network_driver("eos")
# generate API token at https://netbox.mydomain.com/user/api-tokens
token = ""
baseURI = "https://netbox.mydomain.com/api"
headers = {"Accept": "application/json", "Authorization": "Token " + token}
devices = ["DEVICE1", "DEVICE2"]
def interface_lookup(interface, device):
ilookup = "%s/dcim/interfaces/?name=%s&device=%s" % (baseURI, interface, device)
r = requests.get(ilookup, headers=headers, verify=False)
json = r.json()
if json["count"] == 1:
instance = json["results"][0]
return instance["id"]
elif json["count"] == 0:
return False
else:
raise Exception(
"The mentioned interface does not exist on this device or returned more than 1 result"
)
def ipaddress_lookup(ip):
ilookup = "%s/ipam/ip-addresses/?q=%s/" % (baseURI, ip)
r = requests.get(ilookup, headers=headers, verify=False)
json = r.json()
if json["count"] == 1:
instance = json["results"][0]
return instance["id"]
elif json["count"] == 0:
return False
else:
raise Exception(
"The mentioned ip address does not exist or returned more than 1 result"
)
def vm_macaddress_lookup(mac):
mlookup = "%s/virtualization/interfaces/?mac_address=%s" % (baseURI, mac)
r = requests.get(mlookup, headers=headers, verify=False)
json = r.json()
if json["count"] == 1:
instance = json["results"][0]
return instance["virtual_machine"]["id"]
elif json["count"] == 0:
return False
else:
raise Exception(
"The mentioned mac address does not exist or returned more than 1 result"
)
def devices_macaddress_lookup(mac):
mlookup = "%s/dcim/devices/?mac_address=%s" % (baseURI, mac)
r = requests.get(mlookup, headers=headers, verify=False)
json = r.json()
if json["count"] == 1:
instance = json["results"][0]
return instance["id"]
elif json["count"] == 0:
return False
else:
raise Exception(
"The mentioned mac address does not exist or returned more than 1 result"
)
def ipaddress_add(address, interface_id, vrf_id, status):
ipadd = "%s/ipam/ip-addresses/" % (baseURI)
data = {}
data["address"] = address
data["interface"] = interface_id
data["vrf"] = vrf_id
data["status"] = status
r = requests.post(ipadd, headers=headers, verify=False, data=data)
json = r.json()
if r.status_code != 201:
return False
raise Exception("Failed to add ip address")
else:
return json["id"]
def device_lookup(device):
dlookup = "%s/dcim/devices/?name=%s" % (baseURI, device)
r = requests.get(dlookup, headers=headers, verify=False)
json = r.json()
if json["count"] == 1:
instance = json["results"][0]
return instance["id"]
else:
raise Exception(
"The mentioned device does not exist or returned more than 1 result"
)
def device_lookup_by_id(id):
dlookup = "%s/dcim/devices/?id__in=%s" % (baseURI, id)
r = requests.get(dlookup, headers=headers, verify=False)
json = r.json()
if json["count"] == 1:
instance = json["results"][0]
return instance["name"]
elif json["count"] == 0:
return False
else:
raise Exception(
"The mentioned mac address does not exist or returned more than 1 result"
)
def vm_lookup(id):
vlookup = "%s/virtualization/virtual-machines/%s/" % (baseURI, id)
r = requests.get(vlookup, headers=headers, verify=False)
json = r.json()
if json["count"] == 1:
instance = json["results"][0]
return instance["name"]
else:
raise Exception(
"The mentioned device does not exist or returned more than 1 result"
)
# Loop through all the above devices
for device in devices:
try:
print("Trying to lookup %s" % (device))
device_id = device_lookup(device)
# lookup the device id
except Exception as e:
print(e)
print(device)
dev = driver(hostname=device, username="", password="")
dev.open()
arpt = dev.get_arp_table() # query the device for its ip addresses
dev.close()
data = {}
data2 = {}
# Loop through all of the ip addresses returned
for key in arpt:
try:
if ipaddress_lookup(key["ip"]):
pass
elif vm_macaddress_lookup(key["mac"]):
vm = vm_lookup(vm_macaddress_lookup(key["mac"]))
mac_url = "http://macvendors.co/api/%s" % (key["mac"])
r = requests.get(mac_url)
json = r.json()
fail = "Couldn't find {} ({}) in netbox, but found {} ({}) on {}"
print(
fail.format(
key["ip"],
key["interface"],
key["mac"],
json["result"]["company"],
vm,
)
)
elif devices_macaddress_lookup(key["mac"]):
device = device_lookup_by_id(devices_macaddress_lookup(key["mac"]))
mac_url = "http://macvendors.co/api/%s" % (key["mac"])
r = requests.get(mac_url)
json = r.json()
fail = "Couldn't find {} ({}) in netbox, but found {} ({}) on {}"
print(
fail.format(
key["ip"],
key["interface"],
key["mac"],
json["result"]["company"],
device,
)
)
else:
mac_url = "http://macvendors.co/api/%s" % (key["mac"])
r = requests.get(mac_url)
json = r.json()
fail = "Couldn't find {} ({}) or {} ({}) in netbox"
print(
fail.format(
key["ip"],
key["interface"],
key["mac"],
json["result"]["company"],
)
)
except KeyboardInterrupt:
break
except:
fail = "{} returned more than 1 result in Netbox, ignoring."
print(fail.format(key["ip"]))