We help IT Professionals succeed at work.

Need a script to add a SNMP Community String

Medium Priority
2,477 Views
Last Modified: 2012-08-13
I need a script to automatically add a SNMP Community String to 600 servers.  The community string is "test1" and its readonly.  Does anyone have a nice script that will read in 600 servers in a file and add the SNMP community string.  I would like to leave the other existing community strings intact.

Thanks!
Comment
Watch Question

DonNetwork Administrator
CERTIFIED EXPERT

Commented:
Have you tried to configure this in group policy?
 
Administrative Templates\Network\SNMP - determine SNMP communities, permitted SNMP managers, and SNMP traps for public commmunities.

Author

Commented:
Too complicated, since there are many domains we administer, some gpo's could be setup, but not worthwhile in this environment.

Commented:
Hi There

A while ago i helped someone create a ADM file for Group Policy that will do something like this for you. Have a look and let me know if there is anything you don't understand or need amending.

http://www.experts-exchange.com/Software/Server_Software/File_Servers/Active_Directory/Q_23974300.html?cid=295

Regards
Krystian

Author

Commented:
At this time I am looking for a VB or Batch that will take care of my issue.  I don't want to use GPO's at the moment.
Give this a try on one of your test servers. SNMP will use the first community name in the registry alphabetically. After running check the SNMP service for the new values.

In the last two lines edit YourOldComName with the old community name to delete it then YourNewComName with the new community and YourNewHost with the new host.
Dim strKey, FSO
 
Set FSO = Wscript.CreateObject("Scripting.FileSystemObject")
 
Set objShell = WScript.CreateObject("WScript.Shell")
Set objNet = CreateObject("WScript.Network")
 
Const HKEY_LOCAL_MACHINE = &H80000002
strComputer = "."
Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _ 
    strComputer & "\root\default:StdRegProv")
 
strKey = "HKLM\SYSTEM\CurrentControlSet\Services\SNMP\Parameters"
 
objshell.RegDelete strKey & "\TrapConfiguration\YourOldComName\"'
 
objShell.RegWrite strKey & "\TrapConfiguration\YourNewComName\" & "1", "YourNewHost", "REG_SZ"

Open in new window

Sorry you did say leave the other one in there.

Delete or remark out:
objshell.RegDelete strKey & "\TrapConfiguration\YourOldComName\"'

Author

Commented:
I don't want to delete anything, just leave everything intact that already exists.
This this will add test one community...test1 host.
Dim strKey, FSO
 
Set FSO = Wscript.CreateObject("Scripting.FileSystemObject")
 
Set objShell = WScript.CreateObject("WScript.Shell")
Set objNet = CreateObject("WScript.Network")
 
Const HKEY_LOCAL_MACHINE = &H80000002
strComputer = "."
Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _ 
    strComputer & "\root\default:StdRegProv")
 
strKey = "HKLM\SYSTEM\CurrentControlSet\Services\SNMP\Parameters"
 
 
objShell.RegWrite strKey & "\TrapConfiguration\test1\" & "1", "test1", "REG_SZ"

Open in new window

Author

Commented:
Thanks!  How do I add multiple computers?
Do you mean add multiple hosts? If so add this to the end of the script....the numbers will increase as the number of hosts increases.

objShell.RegWrite strKey & "\TrapConfiguration\test1\" & "2", "test2", "REG_SZ"
objShell.RegWrite strKey & "\TrapConfiguration\test1\" & "3", "test3", "REG_SZ"

Open in new window

Author

Commented:
Yes multiple hosts.  Can it read a file of all of the hosts I want to add?  I have about 600 servers to do...
Okay scratch the first part as I did not have luck with WMI and the remote registry.

This one will read c:\computers.txt and add the test1 community name and test1, test2 and test3 hosts.
There is no error checking but as long as your servers are reachable and spelled correctly it will go line by line in computers.txt and now reg add your keys.
Dim strKey, FSO
Set FSO = Wscript.CreateObject("Scripting.FileSystemObject")
Set objShell = WScript.CreateObject("WScript.Shell")
 
 
arrComputers = Split(FSO.OpenTextFile("C:\Computers.txt").ReadAll,vbNewLine)
For Each strComputer in arrComputers
 
'wscript.echo strComputer
 
objshell.Run "reg add \\" & strComputer & "\HKLM\SYSTEM\CurrentControlSet\Services\SNMP\Parameters\TrapConfiguration\test1\ /v 1 /d test1", True
objshell.Run "reg add \\" & strComputer & "\HKLM\SYSTEM\CurrentControlSet\Services\SNMP\Parameters\TrapConfiguration\test1\ /v 2 /d test2", True
objshell.Run "reg add \\" & strComputer & "\HKLM\SYSTEM\CurrentControlSet\Services\SNMP\Parameters\TrapConfiguration\test1\ /v 3 /d test3", True
 
Next

Open in new window

Author

Commented:
I just want all of the hosts in the computers.txt file, what does test1, test2 and test3 hosts are?  Every server in the computer.txt file should get the community string updated.  Maybe I am confused.

objshell.Run "reg add \\" & strComputer & "\HKLM\SYSTEM\CurrentControlSet\Services\SNMP\Parameters\TrapConfiguration\test1\ /v 1 /d test1", True
objshell.Run "reg add \\" & strComputer & "\HKLM\SYSTEM\CurrentControlSet\Services\SNMP\Parameters\TrapConfiguration\test1\ /v 2 /d test2", True
objshell.Run "reg add \\" & strComputer & "\HKLM\SYSTEM\CurrentControlSet\Services\SNMP\Parameters\TrapConfiguration\test1\ /v 3 /d test3", True
 
Next
 
Open in New Window Select All Accept Multiple Solutions Accept as Solution
Okay computer.txt is the servers you want to change the SNMP settings on.
\TrapConfiguration\test1 is the SNMP community name.
the /d test2...etc is the trap destinations for the community name test1



So running the script on one server will provide the following in SNMP on the server in computers.txt

snmp.jpg

Author

Commented:
Ok gotcha, I don't need to add any trap destionations, just need to add the community name to the security tab, sorry if I misled you.
No worries then all you would need is the following. It will add the community name of test1
Dim strKey, FSO
Set FSO = Wscript.CreateObject("Scripting.FileSystemObject")
Set objShell = WScript.CreateObject("WScript.Shell")
 
 
arrComputers = Split(FSO.OpenTextFile("C:\Computers.txt").ReadAll,vbNewLine)
For Each strComputer in arrComputers
 
'wscript.echo strComputer
objshell.Run "reg add \\" & strComputer & "\HKLM\SYSTEM\CurrentControlSet\Services\SNMP\Parameters\TrapConfiguration\test1\", True
 
Next

Open in new window

Author

Commented:
Thanks I will start testing this week and let you know..
Okay...remember there is no error checking so I would do it on a small number of servers first.

Author

Commented:
The script ran fine, but its for the Trap Configuration, not for adding SNMP Community String to the Security Tab.
"\HKLM\SYSTEM\CurrentControlSet\Services\SNMP\Parameters\TrapConfiguration\test1

I want the Community String Test1 (read only) to get added to the Security Tab.  Also is there anyway to check after the community String is entered for each server?  
You are correct you did mention that. Try this one....it will add test1 one as READ_ONLY in the security tab.

What do you mean by:

Also is there anyway to check after the community String is entered for each server?  
Dim strKey, FSO
Set FSO = Wscript.CreateObject("Scripting.FileSystemObject")
Set objShell = WScript.CreateObject("WScript.Shell")
 
 
arrComputers = Split(FSO.OpenTextFile("C:\Computers.txt").ReadAll,vbNewLine)
For Each strComputer in arrComputers
 
'wscript.echo strComputer
objshell.Run "reg add \\" & strComputer & "\HKLM\SYSTEM\CurrentControlSet\Services\SNMP\Parameters\TrapConfiguration\test1\", True
objshell.Run "reg add \\" & strComputer & "\HKLM\SYSTEM\CurrentControlSet\Services\SNMP\Parameters\ValidCommunities\ /v test1 /t REG_DWORD /d 4", True
Next

Open in new window

After reading it again I think I see what you mean. The following will create another text file for you (serversdone.txt) and it loops. Serversdone.txt will display "servername Done At the current date/time"

This way you can tell where the script stopped if there was an error or the host was unavailable for some reason.

JW
Dim strKey, FSO
logfile = "C:\serversdone.txt"
Set FSO = Wscript.CreateObject("Scripting.FileSystemObject")
Set objShell = WScript.CreateObject("WScript.Shell")
 
arrComputers = Split(FSO.OpenTextFile("C:\Computers.txt").ReadAll,vbNewLine)
For Each strComputer in arrComputers
 
Set filetemp = FSO.OpenTextFile(logfile, 8, True)
'wscript.echo strComputer
objshell.Run "reg add \\" & strComputer & "\HKLM\SYSTEM\CurrentControlSet\Services\SNMP\Parameters\TrapConfiguration\test1\", True
objshell.Run "reg add \\" & strComputer & "\HKLM\SYSTEM\CurrentControlSet\Services\SNMP\Parameters\ValidCommunities\ /v test1 /t REG_DWORD /d 4", True
 
filetemp.Write strComputer
filetemp.Write " Done At "
filetemp.Write Now
filetemp.Write VbCrLf
filetemp.Close
 
Next

Open in new window

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
Thanks much I will check it out!
Let me know how it goes and test on a small set of servers first. I added the time stamp to the output file so you could see exactly how long it takes to accomplish the task. This will give you a idea of how many to do in a single batch. I tested on 10 servers and it completed in about 4 seconds (LAN). I am curious how it would work across a WAN which I assume you are running it on.
mystikal1000,

How did this work out? Were you able to do many servers in a short period of time? I only did 10 so checking to see if you did a bulk of them.

Thanks,

JW

Author

Commented:
Yes it was great, so fast too, over 400 servers in about 2 min.

Author

Commented:
Awesome solution, what I wanted!!!
Could you show me how to delete Community name in both trap and security tabs and add another name in both ? I need to delete public in both ( as it is security risk) and add two different names.

I would really appreciate any help

Regards
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.