Linux Proxy Automation

From BNL Physics Computing

The following perl script will determine the IP address of a Red Hat Linux or Scientific Linux machine (if the output of the ifconfig command is the same on other Linux distributions, then the script should work with those operating systems also) and make a link between the appropriate pac file (the three files whose contents are available on the previous page) and the generic wpad.pac file that your browser or application should be configured to use.

You will need to customize the script by setting the path to the four pac files in the $WPAD... variables. A chdir is executed just before linking to the appropriate file, so the paths in the variable needs to be relative to that location or absolute.

#! /usr/bin/perl -w
#
#  Script to check IP address and, based on that, set the active
#  proxy file used by Firefox or other applications
#
# Three cases;
#
#  inside Lab  - wpad_inside.pac
#  on Corus    - wpad_corus.pac
#  outside Lab - wpad_outside.pac
#
# Assume that most applications will read the file at startup.
# If the application is running when the script si run, you will
# need to refresh or reload the pac file in the application
#
use strict;

my $WPAD_Onsite = 'wpad_inside.pac';
my $WPAD_Offsite = 'wpad_outside.pac';
my $WPAD_Corus = 'wpad_corus.pac';
my $WPAD = 'wpad.pac';
my $WPAD_Active;

my $IFCONFIG = '/sbin/ifconfig';
my $GREP = '/bin/grep';
my $CUT = '/usr/bin/cut';

my $ip;

#------------------------------------------------------------#
# Under Linux, a machine's IP address can be optained with ifconfig
# (do all flavors of Linux have the same output format of ifconfig?)
#
# ifconfig | grep "inet " | grep -v 127 | cut -d ":" -f 2
#

$ip = `$IFCONFIG | $GREP "inet " | $GREP -v 127 | $CUT -d ":" -f 2`;
chomp $ip;

# Known Corus subnets are 130.199.152, 153, and 155, there may be others
#  at the moment do one check for 130.19.15 - if this overlaps with an internal
#  subnet you might use, then you will have to make a more specific test

if ($ip =~ /130\.199\.15/)
  {
    $WPAD_Active = $WPAD_Corus;
  }
elsif ($ip =~ /130\.199\./)
  {
    $WPAD_Active = $WPAD_Onsite;
  }
else
  {
    $WPAD_Active = $WPAD_Offsite;
  }

# go to the home directory - assuming that is where the .pac files are
chdir;

# Check if current file linked to $WPAD is the one we want.  If so, exit,
#  else set the link

if (readlink($WPAD) eq $WPAD_Active)
  {
    # OK, just exit
  }
else
  {
    # change it
    unlink $WPAD;
    symlink $WPAD_Active, $WPAD;
  }

exit;

Using the Script

Once you have installed the three pac files and the above script with your customization, all you have to do is run the script and you will be ready to use your browser from any location.

After the script is run, starting your browser will pick up the correct pac file and you are ready to access the Web. If your browser was already running before the script was run, you need to have it reload the pac file. If you use Firefox, you can use an add-on such as FoxyProxy to get access to the proxy configuration using the mouse. For any browser, you can always go into the preferences GUI and refresh the pac file there.

You will have to initiate your tunnel to the internal BNL HTTP proxy for *.bnl.gov addresses to work. You only have to start the tunnel if you are outside the Lab or on Corus, but it will not hurt to start the tunnel if you are inside the Lab (it will just not get used).

Full Automation

If you do not want to run the script by hand each time you change location, you can run the script out of cron. You can also set FoxyProxy to periodically reload the pac file. Using these method, your proxy setup will be correct after some delay due to the frequency of the cron job and the proxy file refresh.

A program such as autossh can be used for the SSH tunnel. This program will detect when the tunnel has failed and will attempt to reestablish the connection.