Last active
March 24, 2023 13:58
-
-
Save yoannmoinet/284ecc8528464228ed64d47c4d7bdba5 to your computer and use it in GitHub Desktop.
Bash functions to change DNS from terminal.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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