Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Set IE Proxy for different DHCP networks

by AcidHawk (Vicar)
on Feb 04, 2003 at 11:25 UTC ( [id://232515]=CUFP: print w/replies, xml ) Need Help??

I have a problem in that I consult to several customers, all of which have DHCP. This suites me fine as all I have to do is set up DHCP on my laptop and I can connect to the customer network as soon as I boot up. I have a rule not to run any login scripts on any customer sites which makes it difficult for me to have customer pertinent settings set at logon. (I.e. proxy, mail servers etc...)

So each time I go to another customer I have to setup my proxy settings or start certain services...(Well this is where it all started..) So I came up with the idea of running a script (which I compiled) at startup.. I may put in a delay in case I have to wait for the address to be issued.. I also got an address reserved for me on the DHCP server.. not too difficult to arrange.

#! /usr/bin/perl use strict; use warnings; use Win32::Service; use Win32::Registry; my (%ipconf); #Change IP to DHCP Addresses and Proxy-xx:Port to Proxy Address #[n/y] Start services or not. %ipconf = ( "196.10.xx.xx" => ["Proxy-ONE:8080", "n"], "172.21.xx.xx" => ["PROXY-TWO:80", "y"]); #Check the current IP Address $::HKEY_LOCAL_MACHINE->Open("SYSTEM\\CurrentControlSet\\Services\\{199 +1FDD6-910A-44EA-B6A4-FDA4E1165BC7}\\Parameters\\Tcpip", my $ip) or di +e "Can't read IP: $^E"; $ip->QueryValueEx("DhcpIPAddress", my $type, my $value) or die "No IP +Address available: $^E"; print "Your IP Address is: $value\n"; #If Address is Work if (exists($ipconf{$value})) { if (&SetProxy($ipconf{$value}->[0])) { print " - Successful\n"; } else { print " - Failed!\n"; print "$^E\n"; } } else { print "IP $value not found!\n"; } if ($ipconf{$value}->[1] eq "y") { if (&StartSvc()) { print " - Successful\n"; } else { print " - Failed!\n"; print "$^E\n"; } } $ip->Close; ###################################################################### +################## ## ## SUB ROUTINES ## ###################################################################### +################## #Set Proxy value in Win32 Registry sub SetProxy { my $proxy = $_[0]; my $rc = 1; my $type = REG_SZ; my ($reserved, $proxy_str); print "Setting Proxy for $value to $proxy"; if ($::HKEY_CURRENT_USER->Open("Software\\Microsoft\\Windows\\Curr +entVersion\\Internet Settings", $proxy_str)) { unless ($proxy_str->SetValueEx("ProxyServer", $reserved, $type +, $proxy)) { $rc = 0; } } else { $rc = 0; } $proxy_str->Close; return($rc); } #Start Services. sub StartSvc { my $rc = 1; my (%list); my @svc_list = ("MSSQLSERVER"); foreach my $svc (@svc_list) { print "Starting $svc"; if (Win32::Service::GetServices($ENV{COMPUTERNAME}, \%list)) { unless (Win32::Service::StartService($ENV{COMPUTERNAME}, $ +list{"$svc"})) { $rc = 0; } } else { $rc = 0; } } return ($rc); }

Anyway the above code is a starting point for checking your local ip address and changing the registry to the corresponding proxy address.

Update: Code Changed to add the starting of services based on what you need to run on different networks.

-----
Of all the things I've lost in my life, its my mind I miss the most.

Replies are listed 'Best First'.
Re: Set IE Proxy for different DHCP networks
by DrZaius (Monk) on Feb 04, 2003 at 15:16 UTC
    This is interesting.

    Just yesterday, me and the other sysadmins were talking about a technology built into IE for doing this. I can't remember what it was called, but it was something like IE Config Packs.

    Basically, it was just a bunch of C style if statements for figuring out what your proxy settings should be.

    We were going to use it for our laptop users who have broad band at home and wouldn't be able to use our proxy without using the VPN.

    I'm sure someone else out there will know what I'm talking about.

      Do you mean the Administration kit? IEAK

      Still the code looks interesting. Especially if you don't wan't to deploy IEAK.

Re: Set IE Proxy for different DHCP networks
by jonas_b (Novice) on Feb 04, 2003 at 20:34 UTC
    I visit places with staticly assigned addresses, which posed a similar problem. So I wrote up a little script which utilizes tcpdump to sniff for the network address, then automatically reset my ip to a random or ARGV number based on the nework address. It defaults the gateway to .1, athough that can be specified on cmd line as well. Obviously this method only works as-is for linux, but it saves me a lot of time. There is room for quite a bit of improvement by someone who has experience with the Packet::sniff module, not to mention someone who knows how to this kinda thing on Windows.

      On the Windows Platform these settings (DHCP and Static) are all in the registry, and can be changed. Actually these settings are found in the same place as in the previous code but the DHCPIPAddress is set to 0.0.0.0 and the IPAddress value is changed.

      I was thinking of adding some kind of gui functionality which would ask you if you wanted to set a static IP\gateway etc.. The problem is that I wanted it to run at startup, either in the startup program menu or specified in the Windows registry and dont want to be bugged everytime I start my machine. Maybe when I have more time...I might even look into the Packet::sniff module.

      -----
      Of all the things I've lost in my life, its my mind I miss the most.

      Hmmm I am kinda bored right now and want to try something new. I will give the Packet::sniff modules a looksy! Thanks!

      For Windows, http://www.netswitcher.com Under $15, no code needed. Works like a charm.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: CUFP [id://232515]
Approved by rob_au
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (8)
As of 2024-03-28 11:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found