Recently, I built a new HP-SIM server and I had to change the SNMP settings on a 1000+ servers to send the traps to a new destination. Here is a script that I made. It should be self-explanatory.

# Powershell to configure SNMP - K.Andreev 20141117
# Usage: ./SetSNMP.ps1 -server <server_name> -community <community_string> -target <target_hostname> -test $true/$false
# Adds target_hostname as a SNMP target, community_string as SNMP string on the server server_name
# If parameter -test is $true, nothing is added to the registry. If you ommit the parameter, nothing is added as well

param(
[string]$server = "",
[string]$community = "",
[string]$target = "",
[bool]$test = $true
)

if (($server -eq "") -or ($community -eq "") -or ($target -eq "")){
	Write-Host "-------------------------------"
	Write-Host "Kliment Andreev - November 2014"
	Write-Host "-------------------------------"
	Write-Host "Usage: ./SetSNMP.ps1 -server <server_name> -community <community_string> -target <target_hostname> -test true/false"
	Write-Host "Adds target_hostname as a SNMP target, community_string as SNMP string on the server server_name"
	Write-Host "If parameter -test is $true, nothing is added to the registry"
	Write-Host ""
	exit
}
try {
	$check = Get-WMIObject Win32_Service -ComputerName $server -ErrorAction Stop | Where-Object {$_.name -eq "SNMP"}
}
catch {
	$Exception = $_
    Write-Host "SNMP not installed on" $server ", exception: $($_.Exception.Message)" -ForegroundColor Red
}
if ($check.Status -eq "OK") {
	Write-Host "SNMP installed on" $server", proceeding with configuration..." -ForegroundColor Green
	$objReg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $server)
	
	# Tab Security, Accept SNMP packets from these hosts
	Write-Host "Accept packets from these hosts:" -ForegroundColor Yellow
	$Values_counter = 1
	$objRegKeyPM = $objReg.OpenSubKey("SYSTEM\\CurrentControlSet\\Services\\SNMP\\Parameters\\PermittedManagers",$true)
	$Duplicate = $false
	while ($true) {
		$value = $objRegKeyPM.GetValue($Values_counter.ToString())
		if ($value -eq $target) {
			$Duplicate = $true
			}
		if ($value -eq $null) {break}
		Write-Host `t $value -ForegroundColor White
		$Values_counter++
	}
	if (!$Duplicate) {
		Write-Host "===> Adding" $Values_counter $target "to HKLM\..\SNMP\Parameters\PermittedManagers"-ForegroundColor Green
		if (!$test) {$objRegKeyPM.SetValue($Values_counter.ToString(), $target)}
		} else {
		Write-Host $target "already exists." -ForegroundColor Red
		}
	$devnull = $objRegKeyPM.Close
	
	# Tab Security, Accepted Community Names
	Write-Host "Accepted Community Names:" -ForegroundColor Yellow
	$objRegKeyVC = $objReg.OpenSubKey("SYSTEM\\CurrentControlSet\\Services\\SNMP\\Parameters\\ValidCommunities",$true)
	$Duplicate = $false
	foreach($value in $objRegKeyVC.GetValueNames()){
		Write-Host `t $value, $objRegKeyVC.GetValue($value).ToString()
		if ($value -eq $community){
			$Duplicate = $true
		}
	}
	if (!$Duplicate) {
		Write-Host "===> Adding " $community "to HKLM\..\SNMP\Parameters\ValidCommunities" -ForegroundColor Green
		if (!$test) {$objRegKeyVC.SetValue($community, 8)}
		} else {
		Write-Host $community "already exists." -ForegroundColor Red
		}
	$devnull = $objRegKeyVC.Close
	
	# Tab Traps, Community Name
	$objRegKeyCN = $objReg.OpenSubKey("SYSTEM\\CurrentControlSet\\Services\\SNMP\\Parameters\\TrapConfiguration",$true)
	$devnull = $objRegKeyCN.CreateSubKey($community)
	$devnull = $objRegKeyCN.Close
	
	# Tab Traps, Trap Destinations
	Write-Host "Trap destinations:" -ForegroundColor Yellow
	$Values_counter = 1
	$strKey = "SYSTEM\\CurrentControlSet\\Services\\SNMP\\Parameters\\TrapConfiguration\\"
	$strKeyTD = $strKey + $community
	$objRegKeyTD = $objReg.OpenSubKey($strKeyTD,$true)
	$Duplicate = $false
	foreach($value in $objRegKeyTD.GetValueNames()){
		$value_name = $objRegKeyTD.GetValue($value).ToString()
		Write-Host `t $value, $value_name
		$Values_counter++
		if ($value_name -eq $target){
			$Duplicate = $true
		}
	}
	if (!$Duplicate) {
		Write-Host "===> Adding" $values_counter $target "to HKLM\..\SNMP\Parameters\TrapConfiguration\"$community -ForegroundColor Green
		if (!$test) {$objRegKeyTD.SetValue($Values_counter.ToString(), $target)}
		} else {
		Write-Host $target "already exists." -ForegroundColor Red
		}
	$devnull = $objRegKeyTD.Close
	$devnull = $objReg.Close
}

More Posts


Migrating WordPress site from one server to another
November 5, 2014

Migrating WordPress site from one server to another

FreeBSD 10: icecast & ices
September 28, 2014

FreeBSD 10: icecast & ices

FreeBSD 10: openvpn 2.3.4
July 1, 2014

FreeBSD 10: openvpn 2.3.4

FreeBSD 10: pf firewall
June 6, 2014

FreeBSD 10: pf firewall