Written by James McDonald

February 17, 2009

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
...

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

3 Comments

  1. John MacDonald

    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?

    Reply
    • james

      How about:

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

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

    Reply

Submit a Comment

Your email address will not be published. Required fields are marked *

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

The reCAPTCHA verification period has expired. Please reload the page.

You May Also Like…