You are not logged in.

#1 2009-12-11 01:13:59

Tyriel
Member
From: Melbourne, Australia
Registered: 2009-01-20
Posts: 161
Website

(net-proxy.sh) Auto proxy config

I thought I would share this script I wrote a few weeks ago in case there are others who often have to change their proxy settings.  I was getting annoyed at having to set my proxy settings two to three times a day so I decided to automate the process.  In short it creates a script that sets the proxy environment variables at login.

Your DE/WM can be configured a few different ways, for example if you are using KDE or Gnome you can tell them to use environment variables to auto setup the proxy settings.  Same can be said for many browsers like Chromium of Firefox.  If you are using Opera you can set OPERA="true" in the script and it will add the proxy to Opera's configuration as well.

It is designed to be run with netcfg, however it can also be run stand alone.  To use it you first have to change the USERNAME in the script to be your own then modify the network profiles that are used by netcfg and add something like:

POST_UP="sh /etc/net-proxy.sh --create proxy.myproxyserver.com.au:8080"
PRE_DOWN="sh /etc/net-proxy.sh --remove"
#!/bin/bash
AUTHOR="Terry Kapnoullas"
VERSION="0.1"
PROXY_SCRIPT="/etc/profile.d/proxy.sh"
USERNAME="tyriel"

OPERA="false"
OPERA_INI="/home/$USERNAME/.opera/operaprefs.ini"
OPERA_NEW="/home/$USERNAME/.opera/operaprefs.new"

function help
{
  echo $0: $*
  echo "Usage: $0 {--create proxy_url | --remove | --version}"
  echo "  -c --create [proxy]    Create the specified proxy settings"
  echo "  -r --remove            Remove the proxy settings"
  echo "  -v --version           Display the script version"
  exit 1
}

function setOperaProxy
{
  if [ $OPERA == "true" ]
  then
    if [ -f $OPERA_INI ]
    then 
      sed -e "s/^Use HTTP=.*/Use HTTP=$1/" -e "s/^Use HTTPS=.*/Use HTTPS=$1/" -e "s/^Use FTP=.*/Use FTP=$1/" $OPERA_INI > $OPERA_NEW
      rm $OPERA_INI
      mv $OPERA_NEW $OPERA_INI
      chown $USERNAME $OPERA_INI
      chgrp users $OPERA_INI
    fi
  fi
}

function createProxy
{
  echo -e "export http_proxy=$1\nexport https_proxy=$1\nexport ftp_proxy=$1" > $PROXY_SCRIPT
  chmod +x $PROXY_SCRIPT
  setOperaProxy 1
}

function removeProxy
{
  if [ -f $PROXY_SCRIPT ]
  then 
    rm $PROXY_SCRIPT
  else
    echo File: $PROXY_SCRIPT does not exist
  fi
  setOperaProxy 0
}

if [ "$(whoami)" != 'root' ] 
then
   echo $0 can only be run by root
   exit 1
fi

if [ $# -ne 1 ] && [ $# -ne 2 ]
then
  help invalid number of arguments
fi

argument=( -c --create -r --remove -v --version )

for i in 0 1
do
  if [ $1 = "${argument[i]}" ] && [ $# -ne 2 ]
    then
      help expecting proxy settings, example: $0 ${argument[i]} proxy.yourcompany.com:8080
  fi
done

for i in 2 3 4 5
do
  if [ $# -ne 1 ] && [ $1 = "${argument[i]}" ]
  then
    help ${argument[i]} should be used with no other argument
  fi
done

case "$1" in
  -c)
    createProxy $2
    ;;
  --create)
    createProxy $2
    ;;
  -r)
    removeProxy
  ;;
  --remove)
    removeProxy
  ;;
  -v)
    echo "netproxy version $VERSION"
    ;;
  --version)
    echo "netproxy version $VERSION"
    ;;
  *)
    help invalid argument used
esac

Warning:  If you run this script after login you will need to log out then back in again for the proxy variables to take effect.  Thus why I recommend it be run via netcfg during the boot process.

I am new to bash so if any of the more experienced bash coders have any suggestions I would love to hear them.  I hope someone else might find this script useful. wink


The software required Windows XP or better, so I installed archlinux.

Offline

Board footer

Powered by FluxBB