-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpushnetbox.py
61 lines (51 loc) · 1.85 KB
/
pushnetbox.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
from netbox import NetBox
import requests
import json
import csv
from re import search
requests.packages.urllib3.disable_warnings()
def createAddress(address, description, vrf, network, role):
#netbox settings
netbox = NetBox(host='10.255.X.X', port=443, use_ssl=True, auth_token='5d6dfe9f6f39785eb86f')
# set VRF ID
id = 0
#attempt to create prefix if its not already there
if vrf == "GRT":
id = 1
try:
netboxPrefix = netbox.ipam.create_ip_prefix(prefix = network, vrf = id)
except:
print("prefix already exists")
elif not vrf:
try:
netboxPrefix = netbox.ipam.create_ip_prefix(prefix = network)
except:
print("prefix already exists")
#set VRF ID
id2 = 0
#attempt to create address
if vrf == "GRT":
id2 = 1
loopback = "Loopback"
if search(loopback, role):
try:
netboxAPIcall = netbox.ipam.create_ip_address(address = address, vrf = id2, description = description, role = "10")
except:
print("ip address already exists")
else:
try:
netboxAPIcall = netbox.ipam.create_ip_address(address = address, vrf = id2, description = description)
except:
print("ip address already exists")
elif vrf == "":
loopback = "Loopback"
if search(loopback, role):
try:
netboxAPIcall = netbox.ipam.create_ip_address(address = address, description = description, role = "10")
except:
print("ip address already exists")
else:
try:
netboxAPIcall = netbox.ipam.create_ip_address(address = address, description = description)
except:
print("ip address already exists")