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)


import requests
import json



url="http://192.168.60.20:8000/api/ipam/ip-addresses/"
headers = {'Content-type': 'application/json', 'Authorization': 'Token 0123456789abcdef0123456789abcdef01234567'}

lastDigit=201
interfaceID=37
while interfaceID<47:
data={
"address": "10.72.72."+str(lastDigit)+"/30",
"status": "active",
"interface": interfaceID
}

response=requests.post(url,data=json.dumps(data), headers=headers)
print(data)
print(response.status_code)
interfaceID+=1
lastDigit+=4