Mac OSX – Update DNS Based On WiFi

| | | |

I often have to bounce between WiFi at the office and the public WiFi so I finally put this script together.

You will need to get the BSSID of your WiFi connection to replicate this but here is the code, of course you need to run this with sudo privileges

If you chmod a+x the file and set the default launcher to terminal you can double click on the file and it will do the work.

#!/bin/bash

sudo -s clear

sBSSID=$(echo $(/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport -I | grep BSSID)|awk '{print $2}')

clear

echo "Current BSSID: $sBSSID"
echo "Current DNS:"
cat /etc/resolv.conf | grep nameserver

if [ "$sBSSID" == "44:ad:d9:a1:29:4f" ]
then
	echo " "
	echo "Setting Corp DNS"
	sudo networksetup -setdnsservers Wi-Fi empty
	sudo networksetup -setdnsservers Wi-Fi 192.168.10.20 192.168.10.21
	sudo killall -HUP mDNSResponder
	echo "Current DNS:"
	cat /etc/resolv.conf | grep nameserver
else
	echo " "
	echo "Setting Google DNS"
	sudo networksetup -setdnsservers Wi-Fi empty
	sudo networksetup -setdnsservers Wi-Fi 8.8.8.8 8.8.4.4
	sudo killall -HUP mDNSResponder
	echo "Current DNS:"
	cat /etc/resolv.conf | grep nameserver
fi
All information on this site is shared with the intention to help. Before any source code or program is ran on a production (non-development) system it is suggested you test it and fully understand what it is doing not just what it appears it is doing. I accept no responsibility for any damage you may do with this code.