yet another proxy_config.vbs

Author Message
bigwill

  • Total Posts : 12
  • Scores: 0
  • Reward points : 0
  • Joined: 8/22/2006
  • Status: offline
yet another proxy_config.vbs Thursday, November 30, 2006 7:26 AM (permalink)
0
Hello All,

As you may or may not be aware, you must configure the proxy settings for each user on a windows domain. The following script will configure the gateway as the proxy for each user that runs it. Since our organization uses linux based VPN routers with Squid web proxy to do content filtering, and access control, the gateway is always the proxy. We use the 172.x.x.x private range, so Im not 100% certain of the performance of this under other private net ranges.

On a side note, someone will probably mention that there's a way to retrieve the gateway with WMI, and they would be correct. The problem I've encountered with using WMI is that, sometimes, I get back information about the wrong network card on computers with more than one network card (especially our wireless tablets running windows XP tablet edition). The scripts that I have which use WMI, I still use a function similar to GrabGateway [GrabIP in my other scripts] as a fall-back if I get a 169.x.x.x IP.

If you are confused about what this script is doing, simply set DebugMode = true at the top of the script, and you'll get plenty of information out of it.

 '######################################################################
 '# proxy_config.vbs    modified   2006.06.29                          #
 '# by big will                                                        #
 '#                                                                    #
 '# purpose - determine local gateway IP, and set Internet Explorer    #
 '# to use that IP as it's proxy, thereby enabling Squid as the proxy. #
 '######################################################################
 Option Explicit
 
 const DebugMode = false
 
 function RegKeyExists(sKey)
   'This function based on Günter Born's example code at
   'http://ourworld.compuserve.com/homepages/Guenter_Born/WSHBazaar/WSH2.htm
   'this is much simpler than the mostrosity that I wrote. 
   dim temp
   dim oShell
   
   set oShell = CreateObject("Wscript.Shell")
   on error resume next
   temp = oShell.RegRead(sKey)
   if Err.Number <> 0 then
     if DebugMode then
       wscript.echo "Registry Key : " & sKey & " - did NOT exist."
     end if
     RegKeyExists = false
   else
     if DebugMode then
       wscript.echo "Registry Key : " & sKey & " - FOUND!"
     end if
     RegKeyExists = true
   end if
   Err.Clear
 end function
 
 function GrabGateway()
   dim objShell
   dim objExec
   dim strConsoleResults
   dim strArr, ipArr
   dim i
   dim sTemp
   
   'Shell out to run "route print", capture the output, and look
   'for the default gateway.  This is a dirty use of Split to do 
   'some work for us.
   on error resume next
   set objShell = CreateObject("WScript.Shell")
   
   set objExec = objShell.Exec("route print")
   GrabGateway = ""
   strConsoleResults = lcase(objExec.StdOut.ReadAll)
   strArr = split(strConsoleResults, chr(13))
   for i = 0 to Ubound(strArr) 
     if inStr(strArr(i), "default gateway:") then
       ipArr = split(strArr(i), " ")
       sTemp = ipArr(ubound(ipArr))
       GrabGateway = sTemp
       if DebugMode then
         wscript.echo sTemp
       end if
     end if
   next
 end function
 
 function setProxy(strIP)
   ' Proxy Registry Keys
   ' [HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Internet settings]
   ' "ProxyServer"="172.x.x.x:80"
   ' "ProxyEnable"=dword:00000001
   ' "ProxyOverride"="127.0.0.1"
   const regKeyProxyServer = "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet settings\ProxyServer"
   const regKeyProxyEnable = "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet settings\ProxyEnable"
   const regKeyProxyOverride = "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet settings\ProxyOverride"
   dim oShell
   dim regValueProxyServer
   dim regValueProxyEnable
   
   set oShell = CreateObject("WScript.Shell")
   
   if RegKeyExists(regKeyProxyServer) then
     regValueProxyServer = oShell.RegRead(regKeyProxyServer)
   end if
   if RegKeyExists(regKeyProxyEnable) then
     regValueProxyEnable = oShell.RegRead(regKeyProxyEnable)
   end if
   if DebugMode then
     wscript.echo "Current regValueProxyServer : " & regValueProxyServer
     wscript.echo "strIP : " & strIP
   end if
   'change the following line to your class A range to enable for a different
   'private net. ie: "192." or "10."
   if (instr(strIP, "172.")) and (regValueProxyServer <> strIP & ":80") or (regValueProxyEnable <> 1) then
     oShell.RegWrite regKeyProxyServer, strIP & ":80", "REG_SZ"
     oShell.RegWrite regKeyProxyEnable, 1, "REG_DWORD"
     oShell.RegWrite regKeyProxyOverride, "127.0.0.1;<local>", "REG_SZ"
     if DebugMode then
       wscript.echo "ran registry update"
     end if
   else
     if DebugMode then
       wscript.echo "Logic determined that current settings = derived settings."
     end if
   end if
 end function
 
 
 if DebugMode then
   wscript.echo "Debug Mode ON!"
 end if
 setProxy GrabGateway
 


This script was written entirely in Notepad++, with tabstops set to 2.
 
#1
    kirrilian

    • Total Posts : 629
    • Scores: 3
    • Reward points : 0
    • Joined: 3/15/2005
    • Location:
    • Status: offline
    RE: yet another proxy_config.vbs Saturday, December 02, 2006 12:36 PM (permalink)
    0
    We just set the web proxy (in IE) using a GPO and then disable the user's ability to change it via the same GPO.

    This works great until they use another browser though.
    Have you searched [url="http://www.google.com"]here [/url]?
    [url="http://tinyurl.com/as7xm"]VBScript Fundamentals[/url]
    [url="http://kirrilian.dyndns.org/projects/code/"]My Site[/url]
     
    #2
      bigwill

      • Total Posts : 12
      • Scores: 0
      • Reward points : 0
      • Joined: 8/22/2006
      • Status: offline
      RE: yet another proxy_config.vbs Wednesday, February 28, 2007 10:34 AM (permalink)
      0
      I have over 40 offices. That means 40 different subnets. And to top that off, many of my users go from one office to another. A nice script that is applied at the top level via GPO was the simplest way to manage affairs. :)


      -Bigwill
      <message edited by bigwill on Wednesday, February 28, 2007 10:37 AM>
       
      #3

        Online Bookmarks Sharing: Share/Bookmark

        Jump to:

        Current active users

        There are 0 members and 1 guests.

        Icon Legend and Permission

        • New Messages
        • No New Messages
        • Hot Topic w/ New Messages
        • Hot Topic w/o New Messages
        • Locked w/ New Messages
        • Locked w/o New Messages
        • Read Message
        • Post New Thread
        • Reply to message
        • Post New Poll
        • Submit Vote
        • Post reward post
        • Delete my own posts
        • Delete my own threads
        • Rate post

        2000-2012 ASPPlayground.NET Forum Version 3.9