#! /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\\{1991FDD6-910A-44EA-B6A4-FDA4E1165BC7}\\Parameters\\Tcpip", my $ip) or die "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\\CurrentVersion\\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); }