This project provides a subnet allocation tool that takes a pool of a given network (e.g., a Class B like 172.16.0.0/16) and, based on a customer’s host requirement, allocates:
- The tightest subnet mask possible (just enough addresses to satisfy the demand).
- The first available subnet inside the pool.
The allocation logic ensures no overlap with previously assigned prefixes.
On top of that, the tool integrates with NetBox (a popular IP Address Management tool), overcoming a limitation of NetBox:
NetBox is excellent at managing IP prefixes and metadata, but it does not allocate subnets for you.
This project bridges that gap:
- Allocates the subnet automatically.
- Creates the prefix entry in NetBox via its REST API.
- Lets you view existing allocations for a customer.
- Allows deallocation of prefixes when no longer needed.
- Standalone Subnet Allocation: Read customer requests from CSV, allocate subnets, and output results to CSV.
- NetBox Integration: Automatically create prefixes in NetBox after allocation.
- View Allocations: Query NetBox for subnets allocated to a specific customer.
- Deallocate Subnets: Free up or shrink a prefix in NetBox when a customer no longer needs it.
- Tight Masking: Always allocates the smallest possible subnet to fit the demand.
-
subnet_allocator.py
Standalone version.- Reads input from
requests.csv(customer name and number of hosts). - Outputs allocated subnets into
allocations.csv. - Good for testing or offline subnet planning.
- Reads input from
-
netbox_allocator.py
NetBox-integrated version.- Reads API token and NetBox URL from
config.json. - Allocates subnets, creates prefixes in NetBox, and provides a menu for:
- Adding a new prefix for a customer.
- Viewing existing allocations.
- Deallocating a prefix.
- Reads API token and NetBox URL from
-
config.json
Stores NetBox API connection details. Example:{ "NETBOX_URL": "https://demo.netbox.dev/api", "NETBOX_TOKEN": "YOUR_API_TOKEN_HERE" }
- Input file:
requests.csvwith customer demands. - Output file:
allocations.csvwith allocated subnets.
allocations.csv (after running subnet_allocator.py)
When you start, your NetBox IPAM may be empty:
After running netbox_allocator.py and allocating prefixes, the subnets are created automatically in NetBox:
The tool provides a simple command-line menu for managing allocations.
Follow these steps to create a free NetBox Cloud account and generate your API token for use with the subnet allocator tool.
- Go to NetBox Cloud Free Tier.
- Sign up for a free account or log in if you already have one.
- Once created, you’ll have access to your own NetBox instance (e.g.,
https://your-tenant.netbox.dev).
- Log in to your NetBox instance.
- Click on your username in the top-right corner.
- Select API Tokens from the dropdown menu.
- Click Add Token.
- Enter a name/description for the token (e.g.,
Subnet Allocator). - Ensure Read/Write permissions are enabled.
- Save the token and copy the value (you’ll need it for config).
- Open the
config.jsonfile in your repo. - Add your NetBox instance URL and the token you just generated:
{
"NETBOX_URL": "https://your-tenant.netbox.dev/api",
"NETBOX_TOKEN": "YOUR_API_TOKEN_HERE"
}