Skip to content

Instantly share code, notes, and snippets.

@bwg
Created June 1, 2012 17:30
Show Gist options
  • Save bwg/2853846 to your computer and use it in GitHub Desktop.
Save bwg/2853846 to your computer and use it in GitHub Desktop.
Shell script for updating resolv.dnsmasq.conf in OS X
#!/bin/bash
# updates resolv.dnsmasq.conf with DHCP provided DNS servers for the
# currently active network interface on OS X
# the location of the dnsmasq resolv file
RESOLV=/etc/resolv.dnsmasq.conf
# get the list of active services from scutil
SERVICES=$(echo 'show Setup:/Network/Global/IPv4' | scutil | grep -e '[[:digit:]] :' | awk '{print $3}')
#echo "$SERVICES"
for service in $SERVICES
do
interface=$(echo "show Setup:/Network/Service/$service/Interface" | scutil | grep -e ^[[:space:]])
interfaceName=$(echo $interface | awk '{print $12 "(" $3 ")"}')
echo "Checking Interface $interfaceName"
# get dns info for this service from scutil. the dns info provided here appears
# to be from dhcp as its not affected by the dns setting in network preferences
# this is what we want to use for our resolv file. there will only be an entry
# in scutil for this service if the interface is active
nameservers=$(echo "show State:/Network/Service/$service/DNS" | scutil | grep -e '[[:digit:]] :' | awk '{print $3}')
if [ $nameservers ]
then
echo "Found DNS on Interface $interfaceName"
# empty out the RESOLV file
cat /dev/null > $RESOLV
for nameserver in $nameservers
do
echo "adding nameserver $nameserver to $RESOLV"
# append nameserver entry to RESOLV
echo "nameserver $nameserver" >> $RESOLV
done
# SERVICES is listed in active order, so we can break after the first hit
break
fi
done
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment