#!/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 io import struct import sys class DnsPacketWriter(io.BytesIO): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self._suffixes = {} def write_u8(self, value): return self.write(struct.pack("B", value)) def write_u16_be(self, value): return self.write(struct.pack(">H", value)) def write_domain(self, domain): # Note: Label list must end with an empty label. labels = domain.encode().lower().strip(b".").split(b".") + [b""] for i, label in enumerate(labels): # Note: Suffix lookup for compression must be case-insensitive, even # if we later decide not to lower() the entire domain. suffix = b".".join(labels[i:]).lower() if len(suffix) > 0: if suffix in self._suffixes: offset = self._suffixes[suffix] self.write_u16_be(0xC000 | offset) return else: self._suffixes[suffix] = self.tell() self.write_u8(len(label)) self.write(label) packet = DnsPacketWriter() for domain in sys.argv[1:]: packet.write_domain(domain) print(""" Raw data -------- """, packet.getvalue(), sep='') hexlist = ["%02x" % x for x in packet.getvalue()] 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='')