Last active
June 14, 2020 01:39
-
-
Save noperator/d0a76d870eb1561d1ca3e4304809b591 to your computer and use it in GitHub Desktop.
Fix broken DNS on macOS's primary interface. 60% of the time, it works every time.
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 | |
# Start trace. | |
set -x | |
# Get active primary interface. | |
IFACE=$(route -n get 0.0.0.0 2>/dev/null | awk '/interface: / {print $2}') | |
# Get networkservice name for interface. | |
NETSVC=$(networksetup -listnetworkserviceorder | grep "$IFACE" | cut -d ' ' -f3- | sed 's/,.*$//') | |
# Erase any DNS server entries, restoring entries provided by DHCP. | |
networksetup -setdnsservers "$NETSVC" empty | |
networksetup -setsearchdomains "$NETSVC" empty | |
# Bounce interface. | |
sleep 1 | |
networksetup -setnetworkserviceenabled "$NETSVC" off | |
sleep 2 | |
networksetup -setnetworkserviceenabled "$NETSVC" on | |
# End trace. | |
set +x |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment