Occasionally I need to switch out from using DHCP-assigned DNS servers to something else, like Google’s public DNS. To do this normally, I’d have to navigate into my System Preferences, find my Wi-Fi connection, authenticate, type in Google’s public DNS server addresses manually….etc etc.
But why do all that when you can automate it with Keyboard Maestro? This macro is designed to switch to Google’s DNS when invoked, notify you of the change through OS X’s Notification Center, or swap you back to DHCP-assigned DNS if you’re already using Google’s.
km-dns-switcheroo
You may notice that I’m not using a trigger. This is because I’ve recently begun using Ian Sinnot’s Alfred Maestro workflow to invoke my macros directly from Alfred.
km-invoke
Here’s how it works in detail:

  • The first action runs a shell script that gets the current DNS servers being used on the wireless interface and saves the results to a variable called ‘currentDNS’
  • The second action begins an If statement:
    • If the ‘currentDNS’ variable contains ‘8.8.8.8’ (the first of Google’s public DNS servers) then:
      • execute a shell script that clears the manual Google DNS entries*
    • Else set the Wi-Fi DNS servers to 8.8.8.8 and 8.8.4.4
  • The third action checks the current DNS servers and passes the results to a variable called ‘currentDNS2’
  • The fourth action begins an If statement:
    • If the variable ‘currentDNS2’ contains ‘8.8.8.8’ then:
      • Send a notification that I’m using Google’s DNS via Notification Center
    • Else send a notification that I’m using DHCP-assigned DNS via Notification Center

* I’m sure there’s a better method of sending the password through to the shell script, but clear text was all I could get to work, despite noodling around with the Keychain Password action.
km-notify
If you’re interested in importing it into your own Keyboard Maestro Macro library, you can download the DNS Switcheroo KM Macro on GitHub.