Skip to content

Instantly share code, notes, and snippets.

@daohoangson
Created March 26, 2015 01:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save daohoangson/60ce8e0317213bc45c30 to your computer and use it in GitHub Desktop.
Save daohoangson/60ce8e0317213bc45c30 to your computer and use it in GitHub Desktop.
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