Last active
December 15, 2024 23:57
-
-
Save SmartFinn/be417c7a7e0b3d9bee9c29e74d08ff78 to your computer and use it in GitHub Desktop.
a script for converting domain names to DHCP Option 119 (Domain Search Option)
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 | |
"""Command generator for setting DHCP Option 119 | |
This script converts the specified domain names to DHCP Option 119 | |
(Domain Search Option) and prints commands for various DHCP servers. | |
USAGE: | |
./dhcp_option119.py DOMAIN ... | |
EXAMPLE: | |
./dhcp_option119.py apple.com google.com | |
""" | |
from __future__ import print_function | |
import sys | |
hexlist = [] | |
for domain in sys.argv[1:]: | |
for part in domain.split('.'): | |
hexlist.append('%02x' % len(part)) | |
hexlist.extend(['%02x' % ord(char) for char in str.lower(part)]) | |
hexlist.append('00') | |
print(""" | |
MikroTik RouterOS | |
----------------- | |
/ip dhcp-server option | |
add code=119 name=domain-search value=0x""", ''.join(hexlist), sep='') | |
print(""" | |
Cisco IOS | |
--------- | |
ip dhcp pool POOL_NAME | |
option 119 hex """, ''.join([(".%s" % (x) if i and not i % 2 else x) | |
for i, x in enumerate(hexlist)]), sep='') | |
print(""" | |
Windows DHCP Server | |
------------------- | |
netsh dhcp server V4 set optionvalue 119 BYTE """, ' '.join(hexlist), sep='') | |
print(""" | |
Juniper SRX | |
------------ | |
set access address-assignment pool POOL_NAME family inet \ | |
dhcp-attributes option 119 hex-string """, ''.join(hexlist), sep='') | |
print(""" | |
ZyXEL Keenetic | |
-------------- | |
ip dhcp pool POOL_NAME option 119 hex """, ''.join(hexlist), sep='') |
wow! very useful, thank you
Nice one! 😉
Thanks, very useful! 👍
The spec requires DNS name compression to be used, see https://gist.github.com/grawity/7828db5ec8ab363dafb981f77a0aa02e for an implementation. (For internal search lists with a lot of subdomains under the same domain, it can make the option a lot shorter – e.g. if you want to list {foo,bar,baz,quux}.example.com
, the list is compressed from 69 down to just 36 bytes.)
thanks this was perfect for using with my windows dhcp server
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks, this worked nicely.