code projects, python

Pull Subnets from Netbox with this Simple Python Script

I was recently tasked with helping the the network team double check their ACLs so I built the following small script to meet the task.

The networking team needed a easy way to find lists of the subnets under our control that we had in Netbox.

What is Netbox? It is opensource software used to help organize and document your computer networks made by DigitalOcean. More information can be found at the following link

https://netbox.readthedocs.io/en/stable/

Small tweaks to this script could pull all kinds of information out of Netbox. I use something similar to get a audit of all VMs under our control. Happy hacking

Github

https://github.com/interwebology/netbox_subnets

Code

#!/usr/bin/env python

import requests
import json
import time

url= 'http://<IP HERE>:8000/api/ipam/prefixes/'

counter=0
offset=50
while True:
    url_string = ''
    if counter == 0:
        url_string = url
        counter += 1 
    else:
        url_string = url + '?limit=50&offset=' + str(offset)

    response = requests.get(url_string)
    prefixes = json.loads(response.text)
    
    for i in prefixes['results']:
        print(i['prefix'])
    
    offset += 50
    if len(prefixes['results']) == 0:
        break