Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save demarey-baker/0749af7783ff52fb14df82439f3b92a8 to your computer and use it in GitHub Desktop.
Save demarey-baker/0749af7783ff52fb14df82439f3b92a8 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
])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment