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.