Skip to content

Instantly share code, notes, and snippets.

@SmartFinn
Last active February 4, 2023 08:27
Show Gist options
  • Star 17 You must be signed in to star a gist
  • Fork 11 You must be signed in to fork a gist
  • Save SmartFinn/be417c7a7e0b3d9bee9c29e74d08ff78 to your computer and use it in GitHub Desktop.
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)
#!/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='')
@bjmgeek
Copy link

bjmgeek commented Jan 28, 2020

Thanks, this worked nicely.

@rletocart
Copy link

wow! very useful, thank you

@bcrisp4
Copy link

bcrisp4 commented Apr 24, 2021

Nice one! 😉

@brettus78
Copy link

Thanks, very useful! 👍

@grawity
Copy link

grawity commented Sep 27, 2021

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.)

@scyto
Copy link

scyto commented Feb 20, 2022

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