Auto proxy settings using .pac file

Dec 16 2011 Published under Linux

I always have this problem:
Switching proxy on/off depending on where I am: ( isAtWorkplace ) ? proxyOn : proxyOff;
And it’s pretty annoying at times. I have been looking for an elegant way to fix this problem for a long time and I think I finally found a satisfying solution.
My approach is to use a pac file to check my current IP address and apply proxy settings accordingly.
Note that this approach will not work if your workplace and home LAN happens to use the same subnet, ie something like 192.168.1.xx. You might want to come up with something else if that’s case, for example change your home subnet address to something else.

Using a PAC file:
This is the straight forward part. We will create a PAC file to tell the system (or browser if you want proxy settings to be effective only in browsers) how to configure our proxy settings. PAC file is a JavaScript file which tell the system/browser to load its proxy configuration information dynamically. Create a file ‘proxy.pac’ (or whatever filename you want) and place it anywhere you want with the following content. Change the subnet mask accordingly:

//file: proxy.pac
function FindProxyForURL(url, host) {
    if (
    	isInNet(myIpAddress(), "", "")
    ) {
        return "PROXY";
    } else {
        return "DIRECT";

This will configure the system/browser to check if the current computer IP address is in the subnet and apply proxy settings accordingly.
Now set the system/browser proxy settings to point to the proxy.pac file. In my case, Automatic proxy configuration URL is set to file:///home/birdy/Documents/scripts/proxy.pac

Well there is a problem. On my system, ‘myIpAddress()’ does not return the eth0 or wlan0 address but the host address This address is set in /etc/hosts file.

$ more /etc/hosts	localhost	HAL2000

So in other words the condition to match the IP address will never be run and proxy settings will not be set correctly.

Auto update /etc/hosts file upon establishing internet connection
To fixed this problem, we have to update /etc/hosts file so that it looks something like this:

$ more /etc/hosts	localhost
current_ip_add	HAL2000

Two separate shell scripts are used to automatically achieve this. To do so, prepare a first script which is responsible for updating /etc/hosts file with current ip address. Place this script anywhere you like.


set -e

if [ "$IFACE" = lo ]; then
    exit 0


# Remove current line with hostname at the end of line
sed -i '/'$myHostName'$/ d' /etc/hosts 

# Add new entry to hosts file (wlan0)
ipaddr=$(ifconfig  | grep -A 1 wlan0 | grep 'inet addr:'| grep -v '' | cut -d: -f2 | awk '{ print $1}')
if [ -n "$ipaddr" ] 
	echo "$ipaddr $myHostName" >> /etc/hosts

# Add new entry to hosts file (eth0)
ipaddr=$(ifconfig  | grep -A 1 eth0 | grep 'inet addr:'| grep -v '' | cut -d: -f2 | awk '{ print $1}')
if [ -n "$ipaddr" ]
	echo "$ipaddr $myHostName" >> /etc/hosts

Prepare a second script which will automatically run the script when internet connection is established. Put this file in ‘/etc/NetworkManager/dispatcher.d/’. See this post for more information on this.


if [ "$IF" == "eth0" ] || [ "$IF" == "wlan0" ]
    case "$2" in
        logger -s "NM Script up triggered"
        logger -s "NM Script down triggered"
        logger -s "NM Script pre-up triggered"
        logger -s "NM Script post-down triggered"

Now, when internet conenction is established, ’’ script will be executed, in which will evoke ‘’ script to update /etc/hosts file, which is used by proxy.pac file to set proxy settings.


No responses yet

Comments are closed at this time.