Skip to content

Instantly share code, notes, and snippets.

@TheTechromancer
Created November 9, 2019 22:16
Show Gist options
  • Save TheTechromancer/bf389d6bdabc7fd8355af6caa122fa5f to your computer and use it in GitHub Desktop.
Save TheTechromancer/bf389d6bdabc7fd8355af6caa122fa5f to your computer and use it in GitHub Desktop.
DHCP Option 121 (Routes) with Python 3 for pfSense, etc.
#!/usr/bin/env python3
from ipaddress import *
routes = [
# destination # router
('10.0.0.0/24', '172.16.0.2'),
('0.0.0.0/0', '172.16.0.1')
]
route_bytes = []
for dest, router in routes:
netmask_cidr = int(dest.split("/")[-1])
destnet = ip_network(dest)
dest = list(destnet.network_address.packed)
netmask = destnet.netmask.packed
for octet in netmask[::-1]:
if octet == 0:
dest = dest[:-1]
router = list(ip_address(router).packed)
route_bytes += [netmask_cidr]
route_bytes += dest
route_bytes += router
print(':'.join([f'{o:02x}' for o in route_bytes]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment