Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2135
  • Last Modified:

Need a script to add a SNMP Community String

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!
0
mystikal1000
Asked:
mystikal1000
  • 12
  • 12
  • +2
1 Solution
 
Donald StewartNetwork AdministratorCommented:
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.
0
 
mystikal1000Author Commented:
Too complicated, since there are many domains we administer, some gpo's could be setup, but not worthwhile in this environment.
0
 
Krys_KCommented:
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
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
mystikal1000Author 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.
0
 
JustWorkingCommented:
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

0
 
JustWorkingCommented:
Sorry you did say leave the other one in there.

Delete or remark out:
objshell.RegDelete strKey & "\TrapConfiguration\YourOldComName\"'
0
 
mystikal1000Author Commented:
I don't want to delete anything, just leave everything intact that already exists.
0
 
JustWorkingCommented:
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

0
 
mystikal1000Author Commented:
Thanks!  How do I add multiple computers?
0
 
JustWorkingCommented:
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

0
 
mystikal1000Author 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...
0
 
JustWorkingCommented:
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

0
 
mystikal1000Author 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
0
 
JustWorkingCommented:
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
0
 
mystikal1000Author 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.
0
 
JustWorkingCommented:
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

0
 
mystikal1000Author Commented:
Thanks I will start testing this week and let you know..
0
 
JustWorkingCommented:
Okay...remember there is no error checking so I would do it on a small number of servers first.
0
 
mystikal1000Author 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?  
0
 
JustWorkingCommented:
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

0
 
JustWorkingCommented:
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

0
 
mystikal1000Author Commented:
Thanks much I will check it out!
0
 
JustWorkingCommented:
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.
0
 
JustWorkingCommented:
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
0
 
mystikal1000Author Commented:
Yes it was great, so fast too, over 400 servers in about 2 min.
0
 
mystikal1000Author Commented:
Awesome solution, what I wanted!!!
0
 
Felicity_HarteCommented:
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
0

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 12
  • 12
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now