Tag Archives: shell

Using a shell script to enable Proxy servers under MacOS X

I often use a proxy server over a SSH tunnel when i’m in public places.   I found it sometimes annoying to click through the dialogs Controlpanel -> Network -> Advanced -> Proxies. I thought i would create a script which enables or disables the proxy settings for me for either the Ethernet or Wireless adapter on my macbook.

The shell scripts uses the commandline tool “networksetup”  to alter the settings.

The “enable proxy” script: (this script also builds the tunnel to a proxy server over port 443)

#!/bin/sh
echo "Connecting to , provide password for username@server.com if prompted"
echo "Enabling proxy for HTTP (please provide admin password if prompted)"
sudo networksetup -setwebproxystate Ethernet on
sudo networksetup -setwebproxystate wi-fi on
echo "Enabling proxy for HTTPS (please provide admin password if prompted)"
sudo networksetup -setsecurewebproxystate Ethernet on
sudo networksetup -setsecurewebproxystate wi-fi on
ssh user@server.com -p443 -L3128:localhost:3128 -o TCPKeepAlive=yes -g
exit

 

The “disable proxy” script:

#!/bin/sh
echo "Disabling proxy for HTTP and HTTPS"
sudo networksetup -setwebproxystate Ethernet off
sudo networksetup -setsecurewebproxystate Ethernet off
sudo networksetup -setwebproxystate wi-fi off
sudo networksetup -setsecurewebproxystate wi-fi off
echo "Done."
exit