Version 1 of ProxySwitch

Updated 2004-09-18 10:01:08

HZe I use a Windows notebook at work and at home. At work, there is internet access through a proxy server while at home I can access internet directly without proxy through a DSL router.

DHCP works fine to get the IP-addresses without reconfiguration, but everytime I switched the networks I had to enable/disable the proxy settings. So I came up with this little script, which does the same based on the current network connection. I added it to my windows AutoStart group and now I'm fine.

    # Switch to enable/disable Proxy depending on Company net or HOME net
    # Tested on Windows XP

    proc usage {} {
        tk_messageBox -message {

    ProxySwitch

        sets the proxy in your Internet Explorer depending the network connection

    Author:
        Holger Zeinert (holger DOT zeinert AT gmx DOT de)

    usage:
        tclsh proxyswitch.tcl <re-expr> <proxyhost:port> ...
        proxyswitch.exe <re-expr> <proxyhost:port> ...

    Description:
        Depending on the output of ipconfig, the setting of the Proxy in the Registry is changed.

        To do so, the output of ipconfig is matched against the regular expressions. The first
        fit determines the proxy host and port to be used. Since the whole output of ipconfig
        is base for regexp, you can make a decision on IP-Address, DNS suffix or whatever.

        If no match fits, the default is to disable the proxy.

    Examples:
        tclsh proxyswitch.tcl ":[ \t]*10.2." "gate:3128"
        proxyswitch.exe ":[ \t]*192.168." "gate:8888" "DNS-Suffix: home" "world.home.de:3200"

    Appendix:
        An output of ipconfig looks e.g. like this:

            Windows-IP-Konfiguration


            Ethernetadapter WLAN-Verbindung :

                    Verbindungsspezifisches DNS-Suffix: home
                    IP-Adresse. . . . . . . . . . . . : 192.168.1.2
                    Subnetzmaske. . . . . . . . . . . : 255.255.255.0
                    Standardgateway . . . . . . . . . : 192.168.1.1
        }
    }

    package require registry

    # no main window
    wm withdraw .

    if {$argv == ""} {
        usage
        exit
    }

    if {[catch {set ipconfig [exec ipconfig]}]} {
        tk_messageBox -message "could not start ipconfig! Not running Windows?"
        exit
    }

    set server "off"
    foreach {re proxy} $argv {
        if {[regexp $re $ipconfig]} {
            set server $proxy
            break
        }
    }

    # depending on proxy, set override proxy for local addresses
    if {$server == "off"} {
        set server ""
        set override ""
        set switch 0
    } else {
        set override <local>
        set switch 1
    }

    # set the entries in the registry
    set keyName {HKEY_CURRENT_USER\SOFTWARE\microsoft\windows\currentversion\internet settings}
    registry set $keyName "ProxyEnable" $switch dword
    registry set $keyName "ProxyServer" $server
    registry set $keyName "ProxyOverride" $override

    if {$switch} {
        tk_messageBox -message "Proxy set to $server!" -icon warning
    } else {
        tk_messageBox -message "Proxy disabled!" -icon error
    }


    exit