Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Switch DNS in Mac terminal, fast and with multiple options!
#/bin/bash
NETWORK_SERVICE="Wi-Fi"
SERVERS=(google opendns uflix)
DNS_GOOGLE="8.8.8.8 8.8.4.4"
DNS_OPENDNS="208.67.222.222 208.67.220.220"
DNS_UFLIX="203.143.83.123 108.61.169.104"
if [ $# -ge 1 ]; then
# get server name in uppercase
SERVER=`echo $1 | tr '[:lower:]' '[:upper:]'`
else
SERVER="NA"
fi
# try to find the dns addresses
DNS_VARNAME=`echo DNS_$SERVER`
eval SERVER_DNS=\$$DNS_VARNAME
len=${#SERVER_DNS}
if [ $len -eq 0 ]; then
echo Usage: $0 server [network-service]
echo Available servers: ${SERVERS[*]}
exit 1
fi
echo Server: $SERVER = $SERVER_DNS
# use a different network service if specified
if [ $# -ge 2 ]; then
NETWORK_SERVICE="$2"
fi
echo Network Service: $NETWORK_SERVICE
echo Changing DNS configuration now, you may be asked for password...
sudo networksetup -setdnsservers $NETWORK_SERVICE $SERVER_DNS
echo Have fun!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment