Skip to content

Instantly share code, notes, and snippets.

@themillhousegroup
Last active March 26, 2025 01:50
Show Gist options
  • Save themillhousegroup/41fba50b448ba8f10166decbe2fcc890 to your computer and use it in GitHub Desktop.
Save themillhousegroup/41fba50b448ba8f10166decbe2fcc890 to your computer and use it in GitHub Desktop.
Convert a dnsmasq config file's DHCP entries into Mikrotik RouterOS format (v1)
#!/bin/bash
if [[ $# -lt 1 ]]; then
echo "ERROR: Need a filename. The file should contain dnsmasq configuration"
exit $#
fi
hosts=`cat $1 | grep dhcp-host | grep -v "#"`
#Set the field separator to new line
IFS=$'\n'
# Typical line:
# dhcp-host=00:11:22:33:44:55,hostname,10.240.0.100,15m[,optional-net-name]
REGEX="^dhcp-host=([a-f0-9:]+),([^,]+),([0-9.]+),([0-9]+[a-z]),?(\S*)?"
for host in $hosts
do
# https://stackoverflow.com/questions/1891797/capturing-groups-from-a-grep-regex
if [[ $host =~ $REGEX ]]
then
mac="${BASH_REMATCH[1]}"
hostname="${BASH_REMATCH[2]}"
ip="${BASH_REMATCH[3]}"
ttl="${BASH_REMATCH[4]}"
netname="${BASH_REMATCH[5]}"
# output in mikrotik format; first, the static dhcp entry:
echo /ip/dhcp-server/lease/add mac-address=${mac} comment=${hostname} address=${ip} lease-time=${ttl}
# and then, a static DNS entry:
echo /ip/dns/static/add name=${hostname} address=${ip} ttl=${ttl}
else
echo "$host doesn't match" >&2 # this could get noisy if there are a lot of non-matching lines
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment