Web development and useful information

Nathan's Blog

Easy DNS switching on OS X

I’m currently using my Raspberry Pi B+ as an ad-blocker. It works great.

Install Ad-blocker on your Raspberry Pi

If you have an up-to-date Raspberry Pi running Raspbian, you can install this by running a single command, here: http://jacobsalmela.com/instant-raspberry-pi-ad-blocker/

Make sure you assign a static IP to your Raspberry Pi: http://www.modmypi.com/blog/tutorial-how-to-give-your-raspberry-pi-a-static-ip-address

By default your Pi will be assigned a dynamic IP by your router. Assigning a static IP means you can connect using the same IP address every time, and setup easy DNS switching below.

Point your DNS server to your Raspberry Pi

To use your new Ad-blocker, set your DNS server to the static IP of your Raspberry Pi.
In this example I’m using OS X.
Changing your DNS in OS X is done by opening System Preferences > Network > Advanced > DNS and setting the static IP here.

Set DNS server on OS X

Try it out

Your Raspberry Pi ad-blocker should now be working. You can test if it’s working by visiting this URL: http://ads-blocker.com/testing.
If you don’t see ads on the page, it’s working.

Disabling Ad-blocking

I ran into an issue using my Raspberry Pi ad-blocker where clicking sponsored Google Ads in search results does not redirect to the link.
I wondered if there was an easier way to switch back to my default DNS server in OS X, than manually changing it in System Preferences.

Enter OS X’s Automator. It comes with OS X.

Easy DNS switching on OS X using Automator

1. Open Automator

2. Choose the ‘Application’ type

Automator - Application Type

3. Type ‘Shell’ into the filter to find the ‘Run Shell Script’ action. Double-click it to open the action in the right hand pane.

Screenshot 2015-03-29 19.07.23

4. Copy and paste this shell script, replacing 192.168.0.69 with YOUR Raspberry Pi’s static IP:

#!/bin/bash
# Use the DNS server hosted on my Raspberry Pi
networksetup -setdnsservers Wi-Fi 192.168.0.69

Automator - Run Shell Script

Note: If you use an Ethernet cable instead, change Wi-Fi above to Built-in Ethernet. I can’t bring myself to buy the £25 Thunderbolt to Ethernet adapter so I use Wi-Fi.

If you connect another way, you can find a list of all the network services on the server’s hardware ports by typing:

networksetup -listallnetworkservices

Change Wi-Fi to the network service you use to connect to the Internet.

5. Save and choose a name for your app, something like ‘Block-ads.app’

6. By default, your app will have the Automator icon. If you’d like to change the icon that’s easily done. All you need is to find a 128×128 PNG.

Default automator icon

7. Using the excellent www.iconfinder.com makes this simple. I found a nice free Raspberry Pi 128×128 PNG icon – https://www.iconfinder.com/icons/386490/raspberry_icon#size=128

8. Download the PNG to your computer, open it in ‘Preview’ and copy the icon using ⌘C.

9. Right click your app’s icon and select ‘Get Info’. Click on the current Automator icon in the top left.

Change icon

10. Press ⌘V to change the icon to the icon in your clipboard

Screenshot 2015-03-29 18.33.38

You can now use this app to easily turn ad-blocking on.
It will prompt for your password when you run it.

11. To create your ‘Show ads’ app, repeat the above steps and change the DNS server to your default DNS server address.

For my ‘Show ads’ app I used this icon: https://www.iconfinder.com/icons/386450/apple_ios_icon#size=128

Icon Downloads

Download Raspberry Pi icon (block ads) – 128×128 PNG
Download Apple icon – (show ads) 128×128 PNG

Previous

tmux – ‘attach session’ alias with tab completion

Next

Music of 2015

3 Comments

  1. Outstanding post, you have pointed out some fantastic points.

  2. should work even on non-pi machine?

Leave a Reply to marcos Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Powered by WordPress & Theme by Anders Norén