Skip to content


Scripting SNMPGet.exe using VBScript

Every month I have to submit page counts to our printer supplier. Usually it involves connecting to each printers administration webpage counting up the different totals (black/white, colour) and then sending that back to them.

Here is a VBScript function that calls snmpget.exe and takes three arguments

  1. IP Address of device you are querying
  2. SNMP Community name (usually public or private)
  3. The SNMP OID

Get snmpget from here

To find out what OID’s are available use snmpwalk and pipe it to a file:
snmpwalk -v 1 -c public 10.2.3.4 > oids.txt
...
iso.3.6.1.2.1.1.5.0 = STRING: "Aficio MP C3000"
iso.3.6.1.2.1.2.2.1.4.1 = INTEGER: 1500
iso.3.6.1.2.1.2.2.1.5.1 = Gauge32: 100000000
iso.3.6.1.2.1.2.2.1.11.3 = Counter32: 0
...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
option explicit
dim name_oid
name_oid = "1.3.6.1.2.1.25.3.2.1.3.1"
wscript.echo getSNMPVal("10.2.3.4", "public", name_oid)
 
function getSNMPVal( server, community, oid)
	on error resume next
	dim strCommand
	dim oShell
	dim oScriptExec
	dim snmpget_stdout
	dim retval
	dim aRetSplit
 
	' define your command
	strCommand = "C:\myprograms\sbin\snmpget.exe -c " & _
        community & " -v 1 " & server & " " & oid
 
	Set oShell = CreateObject("WScript.Shell")
 
	' run the snmpget command
	Set oScriptExec = oShell.Exec(strCommand)
 
	'pipe the result into a variable
	snmpget_stdout = oScriptExec.StdOut.ReadAll
 
	if InStr(snmpget_stdout, "STRING:") > 0 then
		aRetSplit = Split(snmpget_stdout, "STRING:")
		retval	= replace(Trim(aRetSplit(1)), Chr(34), "")
                     'delete double quotes
		retval = replace(retval, vbcrlf, "")
                     ' with the stdout.readall method you get
                      'crlf's at the end of your strings. e.g. asc(15) asc(10)
	elseif InStr(snmpget_stdout, "INTEGER:") > 0 then
	 	aRetSplit = Split(snmpget_stdout, "INTEGER:")
		retval	= Trim(aRetSplit(1))
		retval = retval + 0 ' make sure it's a number
	elseif InStr(snmpget_stdout, "IpAddress:") > 0 then
		aRetSplit = Split(snmpget_stdout, "IpAddress:")
		retval	= replace(trim(aRetSplit(1)), Chr(34), "")
	Else
               ' if there is no return or you haven't defined something
               ' in the elseif section (e.g. Counter32: or Gauge32:)
               ' it returns a generic error
		retval = "ERROR: " & snmpget_stdout
	end if
 
	getSNMPVal = retval
 
end function
Bookmark and Share

Posted in IT Tips, Microsoft Tech Tips.

Tagged with , , , .


3 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

  1. John MacDonald says

    Hello. This code is exactly what I was looking for – thank you.
    Is there any way to hide the snmp.exe window from the user when the code is executed?

    • james says

      How about:

      start /MIN %WINDIR%\system32\cscript.exe yourSNMPget.vbs
  2. Nishant says

    Excellent code, made my life super easy, Do you have something for SNMP set aswel ?



Some HTML is OK

or, reply to this post via trackback.