Set HTTP proxy (or anything else) with NetworkManager on Ubuntu

I move around a lot with my laptop – home, church, Panera, airports – and hate having to make changes to various settings each time I connect to a different network.

NetworkManager provides a facility to run scripts when an network interface is brought up or shutdown. If you place a script in /etc/NetworkManager/dispatcher.d and make it executable, it will be run when NetworkManager changes the state of an interface. NM will pass the script the interface name, such as ‘wlan0”, and the status, either ‘up’ or ‘down’.

This is all well and good, but those scripts are run as the ‘root’ user, and I wanted to run scripts as my user, preventing my marginal shell scripting skills from running havoc on the rest of the system. So, I created a quick script to do just that.

First, I created directories to hold my scripts in my home directory:

mkdir -p ~/.network-manager/ifup.d
mkdir -p ~/.network-manager/ifdown.d

Then I added the following script as ‘/etc/NetworkManager/dispatcher.d/999userscripts’ and set it executable.

#!/bin/sh
# run scripts from ~$USER/.network-manager/if{up,down}.d
# when the status of NetworkManager changes
# scripts run as $USER who own nm-applet
#
#  with some useful shell vars:
# IF - interface effected by status change - passed by network-manager
# STATUS - 'up' or 'down' - passed in from network-manager
# ESSID - access point ID - blank on 'down' status
# SEARCH_DOMAIN - search domain from DHCP - blank on 'down' status
# IP_ADDR - IP address assigned on 'up' status
#
# copyright "Chris Jackson" <chris@faithhack.com> May 2010
# Licensed under GPL v3 http://www.gnu.org/licenses/gpl.html</code>

#get vars from NM
export IF=$1
export STATUS=$2

# who is running nm-applet
NM_APPLET='/usr/bin/nm-applet'
tmp=`pidof $NM_APPLET`
tmp=`ps --no-heading up $tmp`

USER=`echo $tmp | cut -f1 -d' '`

# get other useful vars for user scripts:
# for ifup: $ESSID, $IP_ADDR, $SEARCH_DOMAIN
export ESSID=`/sbin/iwgetid -r $IF`
export SEARCH_DOMAIN=`grep search /etc/resolv.conf | cut -d' ' -f2`
export IP_ADDR=`ifconfig  eth2 | grep 'inet addr:'| cut -d: -f2 | awk '{ print $1}'`

# run scripts as $USER
su $USER -c "run-parts -v ~$USER/.network-manager/if$STATUS.d/"

By adding scripts to my ifup.d and ifdown.d directories, I can do useful stuff like set my http proxy when I connect to my home network:

#!/bin/sh
#turn off system wide proxy when the network is down
if [ "$STATUS" = "down" ]; then
    gconftool-2 -t string -s /system/proxy/mode "none"
fi

#turn on if at home
if [ "$STATUS" = "up" ]; then
    if [ "$SEARCH_DOMAIN" = "91courtstreet.net" ]; then
        gconftool-2 -t string -s /system/proxy/mode "manual"
    else
       gconftool-2 -t string -s /system/proxy/mode "none"
    fi
fi

Once installed, I set Firefox to use the “system proxy” and I don’t have to mess with proxy settings again! Other script ideas might be to login to Panera’s captive portal page on connect, setting up an ssh tunnel when I connect to the free wifi downtown, or setting my default printer when I connect to my church’s network.
Hope you find this helpful.
One quick note – This works for me, but I hacked it together over a cup of coffee at Panera. If it doesn’t work for you, causes damage to your system, or worse, replaces your Ubuntu Lucid system with Windows 7, don’t blame me – you have been warned!

This entry was posted in Coding, linux. Bookmark the permalink.

Leave a Reply