Skip to content

Instantly share code, notes, and snippets.

@wrobelda
Last active May 27, 2025 18:19
Show Gist options
  • Save wrobelda/403fe4e7ff542ce14a4bba9a06e40777 to your computer and use it in GitHub Desktop.
Save wrobelda/403fe4e7ff542ce14a4bba9a06e40777 to your computer and use it in GitHub Desktop.
Convert OPNHome ISC DHCP mappings to Dnsmasq CSV import
import xml.etree.ElementTree as ET
import csv
import sys
csv_writer = csv.writer(sys.stdout)
# Write header
csv_writer.writerow([
"host", "domain", "ip", "client_id", "hwaddr",
"lease_time", "ignore", "set_tag", "descr", "aliases"
])
# Read entire XML config from stdin
xml_data = sys.stdin.read()
root = ET.fromstring(xml_data)
dhcpd = root.find('dhcpd')
if dhcpd is None:
sys.exit("No <dhcpd> section found in config.xml")
for iface in dhcpd:
for staticmap in iface.findall('staticmap'):
mac = staticmap.findtext('mac', '').strip()
ip = staticmap.findtext('ipaddr', '').strip()
hostname = staticmap.findtext('hostname', '').strip()
if mac and ip:
csv_writer.writerow([
"", # host
"", # domain
ip,
"", # client_id
mac,
"", "", "",
hostname, # descr
"" # aliases
])
@wrobelda
Copy link
Author

wrobelda commented May 16, 2025

This extracts the ISC static DHCP mappings and converts them into CSV file, which you can then import in OPNSense (starting with 25.1.6).

Note that the script puts the original ISC hostname into the description field, to allow the client report their own hostname to Dnsmasq DHCP server. This latter has previously not worked for me using the ISC, which is why I was forcing (overriding) the hostname reported by the client. If you still want to override the the hostname, you can do so by moving the hostname variable in writerow() method to first column.

You can execute the script easily with:

ssh user@opnsense 'cat /conf/config.xml' | python3 convert_opnhome_isc_to_dnsmasq_csv.py > dhcp-mappings.csv

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment