Skip to content

Instantly share code, notes, and snippets.

@Aikhjarto
Created September 14, 2021 11:26
Show Gist options
  • Save Aikhjarto/ad0a601163bdc246f5ef8a554c0e2c7f to your computer and use it in GitHub Desktop.
Save Aikhjarto/ad0a601163bdc246f5ef8a554c0e2c7f to your computer and use it in GitHub Desktop.
Convert dnsmasq.conf to config entries suitable for /etc/config/dhcp in OpenWrt
"""
Converts cname, host-records, and dhcp-host entries from a dnsmasq config file
called dnsmasq.conf in the current working directory to corresponding lines
suitable for OpenWrt's /etc/config/dhcp config file.
"""
import re
from io import StringIO
def convert_dnsmasq_conf(filename):
buff=StringIO()
with open('dnsmasq.conf', 'r') as f:
lines=f.readlines()
for line in lines:
line=line.strip() # remove leading and trailing whitespaces, e.g. the \n
split = re.split('=|,',line)
typ=split[0]
if typ=='cname':
buff.write('\nconfig cname\n')
buff.write(f"\toption cname '{split[1]}'\n")
buff.write(f"\toption target '{split[2]}'\n")
elif typ=='host-record':
buff.write('\nconfig domain\n')
buff.write(f"\toption ip '{split[2]}'\n")
buff.write(f"\toption name '{split[1]}'\n")
elif typ=='dhcp-host':
buff.write('\nconfig host\n')
buff.write(f"\toption name '{split[2]}'\n")
buff.write(f"\toption dns '1'\n")
buff.write(f"\toption mac '{split[1]}'\n")
buff.write(f"\toption ip '{split[3]}'\n")
return buff.getvalue()
if __name__=="__main__":
print(convert_dnsmasq_conf('dnsmasq.conf'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment