Script for changing GNOME proxy settings

Most of us are working in corporate environments and corporate environments do love proxies. And I hate them;

  • I hate setting them every morning
  • I hate un-setting them every evening

I am assuming that you also use the same laptop at work and at home.

Since I am so sick of changing proxy settings every time, I created the following bash script to set and unset proxy settings.

#!/bin/sh
PROXY_HOST=$1
PROXY_PORT=$2
PROXY_USERNAME=$3
PROXY_PASSWORD=$4
# Your .gconf directory under your home
CONF="/home/john.doe/.gconf"

if [ -n "$PROXY_HOST" ]
then
    echo "Setting proxy configuration : $PROXY_HOST:$PROXY_PORT"

    gconftool-2 --direct --config-source xml:readwrite:/etc/gconf/gconf.xml.mandatory --type string --set /system/proxy/mode "manual"
    gconftool-2 --direct --config-source xml:readwrite:$CONF --type string --set /system/http_proxy/host "$PROXY_HOST"
    gconftool-2 --direct --config-source xml:readwrite:$CONF --type int    --set /system/http_proxy/port "$PROXY_PORT"
    gconftool-2 --direct --config-source xml:readwrite:$CONF --type bool   --set /system/http_proxy/use_same_proxy "TRUE"
    gconftool-2 --direct --config-source xml:readwrite:$CONF --type bool   --set /system/http_proxy/use_http_proxy "TRUE"

    #gconftool-2 --direct --config-source xml:readwrite:$CONF --type list   --set /system/http_proxy/ignore_hosts [localhost,127.0.0.0/8,*.local]

    if [ -n "$PROXY_USERNAME" ]
    then
        echo "Using authentication information : $PROXY_USERNAME:$PROXY_PASSWORD"

        gconftool-2 --direct --config-source xml:readwrite:$CONF --type=bool   --set /system/http_proxy/use_authentication "TRUE"
        gconftool-2 --direct --config-source xml:readwrite:$CONF --type=string --set /system/http_proxy/authentication_user "$PROXY_USERNAME"
        gconftool-2 --direct --config-source xml:readwrite:$CONF --type=string --set /system/http_proxy/authentication_password "$PROXY_PASSWORD"
    else
        gconftool-2 --direct --config-source xml:readwrite:$CONF --type=bool   --set /system/http_proxy/use_authentication "FALSE"

    fi
else
    echo "Removing proxy configuration."

    gconftool-2 --direct --config-source xml:readwrite:/etc/gconf/gconf.xml.mandatory --type string --set /system/proxy/mode "none"

    gconftool-2 --direct --config-source xml:readwrite:$CONF --unset /system/proxy/mode
    gconftool-2 --direct --config-source xml:readwrite:$CONF --unset /system/http_proxy/host
    gconftool-2 --direct --config-source xml:readwrite:$CONF --unset /system/http_proxy/port
    gconftool-2 --direct --config-source xml:readwrite:$CONF --unset /system/http_proxy/use_same_proxy
    gconftool-2 --direct --config-source xml:readwrite:$CONF --unset /system/http_proxy/use_http_proxy
    gconftool-2 --direct --config-source xml:readwrite:$CONF --unset /system/http_proxy/use_authentication
    gconftool-2 --direct --config-source xml:readwrite:$CONF --unset /system/http_proxy/authentication_user
    gconftool-2 --direct --config-source xml:readwrite:$CONF --unset /system/http_proxy/authentication_password
fi

pkill gconfd

You can run it as follows. In order to unset all the proxy settings, just run it without parameters.

change_proxy.sh <proxy_host> <proxy_port> <proxy_username> <proxy_password>

Now you can set and unset your GNOME proxy settings with one simple command.

If you prefer, you don’t even have to run it at all –by yourself I mean :)– Just create another script under /etc/network/if-up.d and with the following contents. Now every time your computer connects to a network it will call the first script ‘change_proxy.sh’ with appropriate parameters depending on the your IP address.

#!/bin/sh
# you can change this part, I am just checking if there is a network interface
# with and IP address starting with '43.' (my companies network)

PROXY_HOST="your_proxy_host"
PROXY_PORT="your_proxy_port"
PROXY_USERNAME="username"
PROXY_PASSWORD="password"

IP_COUNT=`ifconfig|alych 'inet addr'|awk '{print $2}'|sed -e 's/addr\://'|alych \^43|wc -l`
if [ "$IP_COUNT" = "0" ]
then
        echo "No need for proxy, removing if there is one."

        change_proxy.sh
        cp /home/alych/.m2/settings.home.xml /home/alych/.m2/settings.xml
        sed -i 's/^http-proxy*/# http-proxy/' /home/alych/.subversion/servers
else
        echo "In sony network setting proxy."

        change_proxy.sh $PROXY_HOST $PROXY_PORT #$PROXY_USERNAME $PROXY_PASSWORD if needed
        cp /home/alych/.m2/settings.sony.xml /home/alych/.m2/settings.xml
        sed -i 's/^# http-proxy*/http-proxy/' /home/alych/.subversion/servers
fi

*UPDATE: The script above also puts proper my maven settings.xml and also enables/disables proxy for subversion according to the environment. You can add more stuff to this script, if you have more settings that you need to change.*

Leave a Reply

Your email address will not be published. Required fields are marked *