#******************************************************************************
# Name:		SNMPUpdate.pl
# By:		Robert Spitzer
# Email:	robert.spitzer@ngc.com
#
# The purpose of this script is to configure the SNMP parameters on a given list
# of servers.
#
# 1.1.0	Added functionality to lock down the TrapConfiguration Registry key
# permissions.
#
# 2.0.0	Added functionality to configure the other properties under the Agent,
# Traps, and Security tabs
#
# 2.0.1 Modified script to use Win32::TieRegisty and to process multiple
# servers
#
# Usage:	SNMPUpdate.pl [server list]
#
# Version:	2.0.1
# Created:	07/25/02	Revised:	06/04/03
#******************************************************************************
# Modules used by this script
use Win32::Lanman;
use Win32::TieRegistry;

# If no arguements are provided, print out help information.
if($ARGV[0] eq "/?" || @ARGV < 1) {
	Usage();
	exit 1;
}

# Variables and Constants used by this script
$Contact     = 'LAN Administrator';
$Location    = 'Company Name';
$Community   = 'CommName';
$CommRights  = "0x00000008"; # Read and Write
$Services    = "0x00000049"; # Physical, Applications, End-to-end
$EnableTraps = "0x00000000"; # Disable authentication traps
$Status      = " -Successful Update";
$SNMPPath    = "HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Services\\SNMP\\Parameters";
$Counter     = 1;
@Hosts       = ('172.16.160.123');

# Open the server list file
open (SERVERLIST, "<$ARGV[0]") or die "Couldn't open the file @ARGV[0]\n";

while ($Server = <SERVERLIST>) {
	chomp $Server;
	print "Now Processing: $Server\n";

	# Stop the SNMP Service on the given server
	if (Win32::Lanman::StopService("\\\\$Server", '', "SNMP", \%status)) {
		sleep(10); # Give it some time to shut down

		# Create a handle to the Registry for use through the rest of the script.
		if ($Key = $Registry->{"\\\\$Server\\$SNMPPath\\"}) {

			# Delete the old Registry values out of the affected keys
			DeleteRegValues("PermittedManagers");
			DeleteRegValues("RFC1156Agent");
			DeleteRegValues("ValidCommunities");
			DeleteRegValues("TrapConfiguration");
			DeleteRegValues("TrapConfiguration\\Public");
			DeleteRegValues("TrapConfiguration\\$Community");

			# Delete the "Public" TrapConfiguration subkey and create a new key
			# coresponding to the new community name
			delete $Key->{"TrapConfiguration\\Public\\"};
			delete $Key->{"TrapConfiguration\\$Community\\"};
			delete $Key->{"TrapConfiguration\\"};
			$Key->{"TrapConfiguration\\"} = {};
			$Key->{"TrapConfiguration\\$Community\\"} = {};

			# Create any new Registry values
			$Key->{"\\EnableAuthenticationTraps"}    = [$EnableTraps, "REG_DWORD"];
			$Key->{"RFC1156Agent\\\\sysContact"}     = [$Contact, "REG_SZ"];
			$Key->{"RFC1156Agent\\\\sysLocation"}    = [$Location, "REG_SZ"];
			$Key->{"RFC1156Agent\\\\sysServices"}    = [$Services, "REG_DWORD"];
			$Key->{"ValidCommunities\\\\$Community"} = [$CommRights, "REG_DWORD"];

			foreach (@Hosts) {
				$Key->{"PermittedManagers\\\\$Counter"}             = [$_, "REG_SZ"];
#				$Key->{"TrapConfiguration\\$Community\\\\$Counter"} = [$_, "REG_SZ"]; # Removed per Gary
				$Counter++;
			}

			# Close the Registry connection
			undef $Key;

			# Restart the server
			if (!Win32::Lanman::StartService("\\\\$Server", '', "SNMP")) {
				$Status = " -Unsuccessful SNMP restart";
			}
		}

		else { $Status = " -Unsuccessful SNMP stop"; }

		print "$Status\n";
	}
}

# Close the server list file
close SERVERLIST;

#******************************************************************************
sub DeleteRegValues {
	my $Name   = shift(@_);

	# Loop through the subkey and delete any values found.
	if (my $SubKey = $Key->{"$Name\\"}) {
		foreach $Entry ($SubKey->MemberNames) {
			if ($Entry =~ /^\\/) {	delete $SubKey->{$Entry}; }
		}

		undef $SubKey;
	}
}

#******************************************************************************
sub Usage {

print <<USAGE

Name:  SNMPUpdate v2.0.1
By:    Robert Spitzer
Email: spitzer_rl@naptheon.com

The purpose of this script is to configure the SNMP parameters on a given list
of servers.

Usage:	SNMPUpdate [server list]

USAGE

}