Created
November 9, 2019 22:16
-
-
Save TheTechromancer/bf389d6bdabc7fd8355af6caa122fa5f to your computer and use it in GitHub Desktop.
DHCP Option 121 (Routes) with Python 3 for pfSense, etc.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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