Jan 30 2015
I frequently switch between proxies, mostly SOCKS proxies to force all my traffic through SSH and I usually have to go through the whole process of Click On WiFi -> Open Network Preference -> Advance -> Proxies -> SOCKS Proxy -> OK
I was wondering if there was a way to automate the whole process, so I did a bit of reading to see if it was possible to do just this … to my not so surprise (since OSX is Unix based), it seems you can control every aspect of your network through the networksetup command.
So to set your proxy settings, you would do something like this:
networksetup -setsocksfirewallproxy Wi-Fi localhost 5673 off
To switch on / off your proxy, you would do this:
networksetup -setsocksfirewallproxystate Wi-Fi on / off
Now to open a connection to your server so that the proxy can actually proxy data, use:
ssh -C -D 5673 user@server
And my final script that will enable the proxy if it’s disabled and vice versa:
#!/bin/bash # check if proxy is on or off ENABLED=$(networksetup -getsocksfirewallproxy Wi-Fi | grep "Enabled: Yes") DISABLED=$(networksetup -getsocksfirewallproxy Wi-Fi | grep "Enabled: No") # determine if proxy should be turned on or off if [ "$ENABLED" = "Enabled: Yes" ] then echo "Proxy Enabled, now disabling"; networksetup -setsocksfirewallproxystate Wi-Fi off -setsocksfirewallproxy Wi-Fi localhost 5673 off else echo "Proxy Disabled, now enabling"; networksetup -setsocksfirewallproxystate Wi-Fi on -setsocksfirewallproxy Wi-Fi localhost 5673 off ssh -C -D 5673 user@server fi
Now if you combine this script with AWS or some other cloud provider, you can build a server and use it as a proxy and shut it down once you are done and at the same time have all your proxy settings taken care of all in one bash script. Combine it with a keyboard shortcut and you can literally build a VM, setup a proxy and have your Mac proxy via that machine all using one command.
If you’ve done anything interesting using the networksetup command, drop me a comment.