Windows Proxy Automation

From BNL Physics Computing

The following Visual Basic script is meant to be run after you bring your machine and network connection up. The file should have a .vbs extension. It will determine your IP address and, based on that address, put the appropriate pac file in place. It will also start the PuTTY program with a stored proxy session to open up the tunnel where it is needed. Once the script is run, you can start your application and it will function correctly if it uses the pac file. If your machine was hibernating and your application is already running, then you will need to reload the pac file into the application.

You will need to customize six lines at the top of the script. The first four contain the full path to the generic pac file and the three specific pac files. The script will overwrite the generic file with that appropriate specific file for you location. The fifth file is the path to the PuTTY program. Multiple double quotes are required to handle the space in the path name. In the fifth line ends with the "-load" to PuTTY and a final space. The sixth line contains the name of the tunnel session you stored in PuTTY.

Note: It is recommended that you use the PuTTY SSH agent (pageant) if you want to initiate the tunnel using the script.

Note: Comment lines begin with a single quote.

Visual Basic Script - setProxy.vbs

' Script to determine a machines IP address, set the wpad.pac file and start the tunnel
' The file should have a .vbs extension
' Assume only one interface is up
' Assume interfaces that are down will have IP = 0.0.0.0
' Assume if more then one result is not 0.0.0.0, that they are the same
' Assume IP address is last non 0.0.0.0 address
' If no network interfaces are up, the script will crash

' Path to the pac file to be used by your browser and overwritten with the
' appropriate file for your location
strWpad =      "C:\Documents and Settings\user\wpad.pac"
' Path to the inside pac file
strWpadIn =    "C:\Documents and Settings\user\wpad_inside.pac"
' path to the outside pac file
strWpadOut =   "C:\Documents and Settings\user\wpad_outside.pac"
' Path to the pac file used on Corus
strWpadCorus = "C:\Documents and Settings\user\wpad_corus.pac"
' Path to the PuTTY executable.  Multiple quotes are needed to handle spaces in the path name
strPuTTY     = """C:\Program Files\PuTTY\putty.exe"" -load "
' The PuTTY session name for your tunnel connection
strPuTTYArg =  "Proxy"

' Get the IP address of the machine
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") 

Set IPConfigSet = objWMIService.ExecQuery _
    ("Select * from Win32_NetworkAdapterConfiguration Where IPEnabled=TRUE")
 
For Each IPConfig in IPConfigSet
    If Not IsNull(IPConfig.IPAddress) Then 
        For i=LBound(IPConfig.IPAddress) to UBound(IPConfig.IPAddress)
 	    If StrComp(IPConfig.IPAddress(i),"0.0.0.0") <> 0 Then
              myIPAddr = IPConfig.IPAddress(i)
            End If
        Next
    End If
Next

' Split the IP address into octets
octets = Split(myIPAddr, Chr(46), -1, 1)

' Three cases - inside, Corus or outside
' at the moment, believe that Corus is 130.199.152, 153 and 155
' If there are more subnets, add another ElseIf
If StrComp(octets(0),"130") = 0 Then
  If StrComp(octets(1),"199") = 0 Then
    'BNL, now Corus or not?
     If StrComp(octets(2),"152") = 0 Then
        myLoc = "Corus"
     ElseIf StrComp(octets(2),"153") = 0 Then
        myLoc = "Corus"
     ElseIf StrComp(octets(2),"155") = 0 Then
        myLoc = "Corus"
     Else
        myLoc = "Inside"
     End If
  End If
Else
  myLoc = "Outside"
End If

' Put proper file into position and start the tunnel if necessary (no tunnel inside)
Set FSO = CreateObject("Scripting.FileSystemObject")
Set WshShell = WScript.CreateObject("WScript.Shell")

If StrComp(myLoc, "Outside") = 0 Then
  FSO.CopyFile strWpadOut, strWpad
  ' The 2 in the following line will start the tunnel session in a minimized window
  ' Change to 1 for a normal window
  WshShell.Run strPuTTY & strPuTTYArg, 2, false
ElseIf StrComp(myLoc, "Inside") = 0 Then
  FSO.CopyFile strWpadIn, strWpad
ElseIf StrComp(myLoc, "Corus") = 0 Then
  FSO.CopyFile strWpadCorus, strWpad
  ' The 2 in the following line will start the tunnel session in a minimized window
  ' Change to 1 for a normal window
  WshShell.Run strPuTTY & strPuTTYArg, 2, false
Else
  WScript.Echo "Failed to set proxy file."
End If

' All done

If you do not want the tunnel to be started by this script, delete or comment out the "WshShell.Run" lines near the end of the script. Also, the options to the Run method will start the PuTTY program minimized. If you want it to start normally, change the 2 to a 1.

Usage of 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. You may want to make a Desktop shortcut to the script to make it convenient to run.

After the script is run, starting your browser should pick up the correct pac file and you are ready to access the Web. If the correct pac file is not loaded when you restart your browser, or 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 to click on the appropriate configuration to reload the file. For any browser, you can always go into the preferences GUI and refresh the pac file there.

Full Automation

For most people, double clicking on an icon to put the correct pac file in place and start the tunnel is sufficient automation. Under certain conditions, it is possible to make the process even more automatic.

If you shutdown your machine when you change locations, then you can add the script to the startup menu so that it is run automatically when you login. The caveat here is that the network has to be up for the tunnel to connect. If the network is not up, then you can comment out the starting of the tunnel and start it from the Pageant tray icon when you need it.