Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Toggle a proxy server on and off via scripts Network
I've recently configured my Mac to tunnel web traffic through my university to take advantage of some free services accessible only from their IPs. I accomplish this using a dynamic port tunnel over SSH with the great SSHKeychain tool (which handles SSH key management as well as tunneling). Enabling the tunnel is just a click in a menu bar drop-down.

Enabling/disabling the global proxy settings is not quite as easy, however. You have to manage everything through a secondary tab in the Network preference pane. So I wrote ToggleProxy (874KB download) to speed up the process. It will toggle the SOCKS proxy on and off automatically (make sure the server and port are already set in the Network preference pane), and display the status with a Growl notification if you have the growlnotify tool installed, or in a native dialog box otherwise.

Make sure to read the ReadMe file, as you may have to make a change to use the tool for Ethernet as opposed to Airport. For those of you who want to expand the tool to implement manual proxy settings or using other proxies, I recommend you look up the networksetup command-line tool. I also use scutil to discover my proxy settings, but that is also possible through networksetup.

Note: I use 10.5.2, and I don't know if networksetup is available in previous versions of OS X. If you do not have networksetup, then ToggleProxy will not function.

[robg adds: I haven't tested this one. If you'd like to see (or change) what it does, you can first view its bundled AppleScript and bash scripts. You can see the script sources by digging into the package -- Control-click on the app icon and pick Show Package Contents from the pop-up menu, then navigate into Contents » Resources » Scripts folder. The networksetup application is available on pre-10.5 systems, as described in this hint, but you'll probably want to copy/link it somewhere else to use it regularly.]
    •    
  • Currently 1.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (6 votes cast)
 
[27,686 views]  

Toggle a proxy server on and off via scripts | 6 comments | Create New Account
Click here to return to the 'Toggle a proxy server on and off via scripts' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Toggle a proxy server on and off via scripts
Authored by: dahjelle on Mar 05, '08 08:52:53AM

Somewhat unrelated—but does anyone know how to get 10.5.2's Mail.app to function through a SOCKS proxy? Mine doesn't seem to respect the system settings.



[ Reply to This | # ]
Toggle a proxy server on and off via scripts
Authored by: paulnojustpaul on Mar 05, '08 01:51:53PM

Maybe I'm missing something here, wouldn't it be easier just to have two network locations, one with the proxy set, the other without, and switch between the two? You can then also set keyboard shortcuts for each using QuickSilver and the like.



[ Reply to This | # ]
Toggle a proxy server on and off via scripts
Authored by: sveiki on Mar 06, '08 12:26:10AM
Seems to me like this would work just fine, as well. I also did not think to explore this option. The reason I have it set up this way is to quickly change my proxy setup to download a file or two and then quickly switch it back. Switching network locations could have one issue that might be problematic if the system insists on disconnecting and reconnecting to networks or renewing a DHCP lease (I'm not sure if this is the case. Other reasons networksetup might be a good option:
  • It never hurts to have multiple solutions.
  • Scripting proxy changes could be useful in some situations (ie doing something else via script or programmatically).
  • Possibly faster.
  • No need for additional network locations just to have choices for "Proxy On" and "Proxy Off". If you already have a few network locations, this just might be important.


[ Reply to This | # ]
Toggle a proxy server on and off via scripts
Authored by: hj45lp on May 30, '10 03:56:15AM

The other days I stayed at a hotel with wireless, and I wanted to use a SOCKS proxy (ssh) for accessing machines inside my company network and also for security: the hotel does not need to know what I am looking at. I have two locations set up with and without proxy. I needed a no-proxy location to enter the password for the wireless access, but when thereafter switching to the proxy location, Airport disconnected and reconnected, and the hotel's system required me to enter my password again, but for doing that I needed the no-proxy location. Argh! So the only way was going into System Preferences and toggle the proxy on/off.

This hint enables me to automise the process, always toggling the proxy on before starting ssh to create the socks proxy.

While I am not using the applescript solution this pointed me to the networksetup utility. Big thank you for that! And I confirm that that utility also works in Snow Leopard.

Thanks!



[ Reply to This | # ]
Toggle a proxy server on and off via scripts
Authored by: bazzoola on Oct 28, '08 10:05:52AM
I needed to toggle the http proxy not the socks proxy. So I modified the bash script and the apple script to do this. It used to crash before 10.5.5 but It seems that apple fixed that bug.

#! /bin/bash

################################
#
# Jeremy Cutler, 2008
# sveiki@gmail.com
#
# Currently, this script will activate and deactivate an already-specified SOCKS proxy server
# (for forwarding web traffic, especially useful through an SSH tunnel). Other types of proxies can be used
# but you'll have to figure it out yourself with the 'scutil' and 'networksetup' command-line tools
################################

###
# Network device to be managed.
# more options exist other than these, run 'networksetup -listallnetworkservices' from the terminal
###
device="Airport"
#device="Built-in Ethernet"

if [[ `scutil --proxy | grep HTTPEnable | awk '{ print $3 }'` == "1" ]]; then
	proxyState="enabled"
else
	proxyState="disabled"
fi

if [[ "$proxyState" == "enabled" ]]; then
	networksetup -setwebproxystate "$device" off
	proxyState="disabled"
	imagepath="${1}Contents/Resources/proxy_disconnected.icns"
else
	networksetup -setwebproxystate "$device" on
	proxyState="enabled"
	imagepath="${1}/Contents/Resources/proxy_connected.icns"
fi

capProxyState=`echo "${proxyState:0:1}" | tr a-z A-Z`
capProxyState=$capProxyState${proxyState:1}

if [[ -e /usr/local/bin/growlnotify ]]; then
	/usr/local/bin/growlnotify --image "$imagepath" -m "Proxy Server is now $proxyState." -t "Proxy Server $capProxyState"
else
	echo "$proxyState"
fi
The only thing I edited in the apple script was the word 'SOCKS' I just removed it. Thanks!

[ Reply to This | # ]
Toggle a proxy server on and off via scripts
Authored by: jmbakker on Oct 06, '11 04:23:52AM

I used the script and it worked nicely under Snow Leopard. Under Lion, the script networksetup no longer recognizes the device "Airport", which is renamed "Wi-Fi". To fix the ToggleProxy App
edit the following script:

ToggleProxy.app/Contents/Resources/Scripts/script.bash

and replace the line

#device="Airport"

with

device="Wi-Fi"

It then works as expected with Lion



[ Reply to This | # ]