Skip to content

Instantly share code, notes, and snippets.

@noperator
Last active June 14, 2020 01:39
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 noperator/d0a76d870eb1561d1ca3e4304809b591 to your computer and use it in GitHub Desktop.
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.
#!/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