SHA2562f99d7a8a9f45d171758187d639d26220c41cee851698fab25163ba9e275b2b2Verify after downloading: echo "2f99d7a8a9f45d171758187d639d26220c41cee851698fab25163ba9e275b2b2 netdivide.sh" | sha256sum -c
Read it first, then run it. The complete source is further down on this page. There is deliberately no curl … | bash one-liner here.
Use at your own risk — no liability for data loss or system damage. See the terms of use.
What the script is for
In some networks, every VLAN ID maps to its own subnet — a convention that saves you from looking things up in docs or a spreadsheet, but still means doing math in your head once you're dealing with a whole range of VLANs or with different subnet sizes. The script does that conversion in one step.
What it does
Calculates the matching subnets for a VLAN range (START-VLAN END-VLAN).
Shows the start network, end network, and how many /24-sized units are needed.
Base network (--base), anchor VLAN (--anchor, the VLAN ID that maps to
x.x.0.0/PREFIX), and prefix length (--prefix, 8 to 30, default 27) are all configurable.
Warns if the range overflows the third octet (base network or prefix too small).
Provides Bash autocompletion for its options via --completion (enable it using source <(netdivide.sh --completion) or save it permanently in /etc/bash_completion.d/).
How it works
Instead of a lookup table, the script computes the position purely arithmetically. Each VLAN ID gets an index relative to the anchor VLAN, and that index maps directly to a position in the address space via integer division and modulo — depending on whether the subnet is smaller or larger than a /24:
local block_size=$(( 2 ** (32 - prefix) ))
if [ "$prefix" -ge 24 ]; then
local per_24=$(( 256 / block_size ))
s_third=$(( s_idx / per_24 ))
s_fourth=$(( (s_idx % per_24) * block_size ))
else
local step3=$(( block_size / 256 ))
s_third=$(( s_idx * step3 ))
s_fourth=0
fi
From /24 upward, the subnet fits entirely into the fourth octet, just like classic /27 or /26 user subnets. If the prefix is smaller than /24 (e.g. /23 or /22), the subnet is larger than a /24 and spans multiple units in the third octet — the fourth octet then stays at 0, and the step size in the third octet grows accordingly. Both cases boil down to the same idea: don't maintain a table, derive the position from the definition of the anchor VLAN and the block size.
Why I automated this
I’ve often found myself having to calculate multiple /27 (or other) subnets because each VLAN required its own interface. Since using a calculator for this felt too tedious and confusing, I eventually put together a quick script to simplify the process and have been expanding it ever since.