Bulk add IP's to Netbox IPAM and attach to Interface
I need to add a /30 IP to every interface on a switch to enable host based routing in a virtualization cluster. I'm lazy and didn't want to key them all in by hand. So I wrote this script in Pyhton3
It uses a starting interface ID(Assuming that all of the interfaces on your switch are in numerical order) and it uses a starting IP.
It loops through each interface ID until it's the number define in the where loop which should be set to the last interface ID of your device.
It adds 4 to the last digit of the IP address(Assuming you are working with /30's)
1import requests
2import json
3
4url="http://192.168.60.20:8000/api/ipam/ip-addresses/"
5
6headers = {'Content-type': 'application/json', 'Authorization': 'Token 0123456789abcdef0123456789abcdef01234567'} lastDigit=201
7
8while interfaceID<47:
9
10 data = { "address": "10.72.72."+str(lastDigit)+"/30",
11
12 "status": "active",
13
14 "interface": interfaceID
15
16 }
17
18 response = requests.post(url,data=json.dumps(data), headers=headers)
19
20 print(data)
21
22 print(response.status_code)
23
24 interfaceID + =1
25
26 lastDigit += 4