Linux automatically switching proxy gnome/kde4

16 May 2009
Posted by bain

I finally found a way to change the kde4 proxy automatically based on class of ip address i get on available network interface.
I already found a script from google to do that for gnome.
And now found a KDE3 script which had to be modified to do a KDE4 DBUS message to make applications reread the settings.
The info was especially hard to find on the net. Reproducing both scripts below.

Put below in /etc/NetworkManager/dispatcher.d/02proxy

Make sure you make appropriate changes to USERNAME and YOURPROXY etc. (Click read more to find the scripts)

#!/bin/bash
# The script for automatically setting the proxy server depending on location.
# Put it under /etc/NetworkManager/dispatcher.d/ and modify for your needs.
# Written by Anton Keks
USER=<USERNAME>
PWD=
WORK_IP=172.
function gconf() {
   sudo -E -u $USER gconftool-2 "$@"
}
INTERFACE=$1
COMMAND=$2
# init DBUS connection string in order to reach gconfd
XENV=`xargs -n 1 -0 echo </proc/$(pidof gnome-session)/environ`
eval export `echo "$XENV" | fgrep DBUS_SESSION_BUS_ADDRESS=`
if [ $COMMAND = 'up' ]; then
 if nm-tool | fgrep $WORK_IP; then
   echo work found
   gconf --type string --set /system/proxy/mode "manual"
   gconf --type bool --set /system/http_proxy/use_http_proxy "true"
   gconf --type string --set /system/http_proxy/host "<YOURPROXY>"
   gconf --type int --set /system/http_proxy/port "8080"
   gconf --type bool --set /system/http_proxy/use_same_proxy "true"
   su $USER -c "/home/bain/scripts/kproxyswitch.sh on"
 else
   gconf --type string --set /system/proxy/mode "none"
   gconf --type bool --set /system/http_proxy/use_http_proxy "false"
   gconf --type string --set /system/http_proxy/host ""
   gconf --type bool --set /system/http_proxy/use_authentication "false"
   su $USER -c "/home/bain/scripts/kproxyswitch.sh off"
 fi
fi

 

Put this somewhere on the system and make sure the script above uses the same path

 

#!/bin/bash
### Proxy-Switcher for KDE
### Version 0.2 (2006-06-28)
### Patrick Nagel  (mail AT patrick - nagel DOT net)
function print_help() {
        echo "Specify proxy as argument:"
        echo "$0 myproxy.example.com:3128"
        echo                             
        echo "or"                        
        echo                             
        echo "$0 off        switch off proxy"
        echo "$0 on         switch on proxy (sets last proxy again)"
        echo "$0 auto       switch to automatic proxy detection"   
        exit 0                                                     
}                                                                  
function set_off() {
        sed -e "s/ProxyType=[0-9]/ProxyType=0/" -i ${CF}
        return                                         
}                                                      
function set_on() {
        sed -e "s/ProxyType=[0-9]/ProxyType=1/" -i ${CF}
        return                                         
}                                                      
function set_auto() {
        sed -e "s/ProxyType=[0-9]/ProxyType=3/" -i ${CF}
        return                                         
}                                                      
function set_proxy() {
        sed -e "s/ProxyType=[0-9]/ProxyType=1/" \
            -e "s/ftpProxy=.*/ftpProxy=http:\/\/${PROXY}/" \
            -e "s/httpProxy=.*/httpProxy=http:\/\/${PROXY}/" \
            -e "s/httpsProxy=.*/httpsProxy=http:\/\/${PROXY}/" -i ${CF}
        return                                                        
}                                                                     
function tell_apps() {
        export DBUS_SESSION_BUS_ADDRESS=`cat /proc/\`pidof kwin\`/environ | tr "\000" "\n" | grep DBUS | sed -e 's/DBUS_SESSION_BUS_ADDRESS=//'`                                               
        echo `/usr/bin/dbus-send --type=signal /KIO/Scheduler org.kde.KIO.Scheduler.reparseSlaveConfiguration string:""`
}
# Main starts here
CF="${HOME}/.kde/share/config/kioslaverc"
echo "Proxy-Switcher for KDE"
echo
case "$1" in
        ("-h" | "--help" | "")
                print_help
        ;;
        ("off")
                set_off
        ;;
        ("on")
                set_on
        ;;
        ("auto")
                set_auto
        ;;
        *)
                PROXY=$1
                set_proxy
        ;;
esac
tell_apps

 

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters (without spaces) shown in the image.