Skip to content

Instantly share code, notes, and snippets.

@yoannmoinet
Last active March 24, 2023 13:58
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 yoannmoinet/284ecc8528464228ed64d47c4d7bdba5 to your computer and use it in GitHub Desktop.
Save yoannmoinet/284ecc8528464228ed64d47c4d7bdba5 to your computer and use it in GitHub Desktop.
Bash functions to change DNS from terminal.
function setGoogleDNS() {
CHANNEL=`route get google.com | grep interface | sed -n -e 's/^.*interface: //p'`
NETWORK=`networksetup -listnetworkserviceorder | grep $CHANNEL | sed -n -e 's/^.*Port: //p' | sed -n -e 's/,.*//p'`
echo "Setting Google's DNS on channel '$CHANNEL' for network '$NETWORK'."
sudo networksetup -setdnsservers $NETWORK 8.8.8.8 8.8.4.4 2001:4860:4860::8888 2001:4860:4860::8844
}
function setCloudflareDNS() {
CHANNEL=`route get google.com | grep interface | sed -n -e 's/^.*interface: //p'`
NETWORK=`networksetup -listnetworkserviceorder | grep $CHANNEL | sed -n -e 's/^.*Port: //p' | sed -n -e 's/,.*//p'`
echo "Setting Cloudflare's DNS on channel '$CHANNEL' for network '$NETWORK'."
sudo networksetup -setdnsservers $NETWORK 1.1.1.1 1.0.0.1 2606:4700:4700::1111 2606:4700:4700::1001
}
function resetDNS() {
CHANNEL=`route get google.com | grep interface | sed -n -e 's/^.*interface: //p'`
NETWORK=`networksetup -listnetworkserviceorder | grep $CHANNEL | sed -n -e 's/^.*Port: //p' | sed -n -e 's/,.*//p'`
echo "Resetting DNS on channel '$CHANNEL' for network '$NETWORK'."
sudo networksetup -setdnsservers $NETWORK empty
}
function showDNS() {
CHANNEL=`route get google.com | grep interface | sed -n -e 's/^.*interface: //p'`
NETWORK=`networksetup -listnetworkserviceorder | grep $CHANNEL | sed -n -e 's/^.*Port: //p' | sed -n -e 's/,.*//p'`
networksetup -getdnsservers $NETWORK
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment