Created
January 19, 2022 13:36
-
-
Save luccamendonca/4bd7f31a915b6413052a31b0430b2e10 to your computer and use it in GitHub Desktop.
Script to configure DNS entries for all network adapters on macOS
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
#!/bin/bash -e | |
while IFS= read -r adapter_name; do | |
echo "DNS Servers configured on $adapter_name:" | |
eval "networksetup -getdnsservers \"$adapter_name\"" | |
echo "" | |
echo "Search domain configured on $adapter_name..." | |
eval "networksetup -getsearchdomains \"$adapter_name\"" | |
echo "" | |
echo "" | |
done < <(networksetup -listallhardwareports | grep -i 'hardware port:' | egrep -i -v 'thunderbolt|bridge' | cut -c 16-) |
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
#!/bin/bash -e | |
dns_servers=${1:-'8.8.8.8 8.8.4.4'} | |
search_domain=${2:-''} | |
echo "DNS Servers to be used: $dns_servers" | |
echo "Search domain to be used: $search_domain" | |
echo "" | |
while IFS= read -r adapter_name; do | |
echo "Setting DNS for $adapter_name..." | |
eval "networksetup -setdnsservers \"$adapter_name\" $dns_servers" | |
if [[ ! -z "$search_domain" ]]; then | |
echo "Setting search domain for $adapter_name..." | |
eval "networksetup -setsearchdomains \"$adapter_name\" $search_domain" | |
fi | |
echo "" | |
# The oneliner below gets all possible network hardware ports and removes the | |
# "generic" thunderbolt as well as the bridge ones, to make sure we only configure | |
# the DNS on actual networking devices (includes any Thunderbolt/USB-C network adapter) | |
done < <(networksetup -listallhardwareports | grep -i 'hardware port:' | egrep -i -v 'thunderbolt|bridge' | cut -c 16-) | |
echo "Flushing DNS cache and killind DNS service..." | |
sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder | |
echo "Done!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment