VBScript SNMP Queries

In the course of my work as a server administrator, it has become desirable to know what kind of hardware was currently being used in some of our servers. Well, it has been pretty easy to pull this kind of information from WMI. Nearly all of the hardware specs can be pulled using these traditional methods. More recently, I ran into a problem retrieving hard drive specs because the array controller on some servers would mask physical hard drive properties from the OS. Instead showing the hard drive as “Compaq Array Controller Driver”.

Well that wasn’t particularly useful information to me. I knew that this information was available to the SIM agents and that it could be viewed using Insight Manager console. After some poking around, figuring out how these things worked, I found out that all of this information could be found via SNMP queries. You can browse all of the available information with a few different free utilities.

I even found a command line utility called SNMPGET.exe that you can use to query specific OIDs providing you had the correct community string (thanks again SourceForge) Well since we can use this command line tool to extract information remotely from any server running SIM we could use this tool and a little VBScript to extract any data stored in an OID on any server we want.

First, though, we need a function that will simplify the process of extracting the value we are interested in:

Function SNMPGET(vServer, vCommunityString, vOID)
    On Error Resume Next

    'This function will return the data from an SNMP get from the specified 
    'server and the specified OID. The information will have to be parsed 
    'afterward. The info will be retuned as the string or variable "SNMPGET".
    'This script is provided under the Creative Commons license located
    'at http://creativecommons.org/licenses/by-nc/2.5/ . It may not
    'be used for commercial purposes with out the expressed written consent
    'of NateRice.com

    Const OpenAsDefault = -2
    Const FailIfNotExist = 0
    Const ForReading = 1
    vSNMPGetPath = "C:\ScriptFiles" 'Set path to snmpget.exe

    Set WshShell = CreateObject("WScript.Shell") 'Create filesystem objects
    sTemp = WshShell.ExpandEnvironmentStrings("%TEMP%")
    sTempFile = sTemp & "\runresult.tmp" 'Create working file string
    'Run the SNMPGET command and save results to tmp file
    WshShell.Run "%comspec% /C " & vSNMPGetPath & "\snmpget -c " & _
    vCommunityString & " -v 1 " & vServer & " " & vOID & " > " & _
    sTempFile, 0, True

    'Read tmp file for snmpget results
    Set oFSO = CreateObject("Scripting.FileSystemObject")
    Set fFile = oFSO.OpenTextFile(sTempFile, ForReading, FailIfNotExist, _
    SNMPGET = fFile.Readline
    If InStr(SNMPGET, "STRING:") > 0 Then
        aSNMPGET = Split(SNMPGET, "STRING:")
        vTemp = Replace(Trim(aSNMPGET(1)), Chr(34), "")
        SNMPGET = vTemp
    ElseIf InStr(SNMPGET, "INTEGER:") > 0 Then
        aSNMPGET = Split(SNMPGET, "INTEGER:")
        vTemp = Trim(aSNMPGET(1))
        vTemp = vTemp + 0
        SNMPGET = vTemp
    ElseIf InStr(SNMPGET, "IpAddress:") > 0 Then
        aSNMPGET = Split(SNMPGET, "IpAddress:")
        vTemp = Trim(aSNMPGET(1))
        SNMPGET = vTemp
    End If
    oFSO.DeleteFile (sTempFile)
End Function

This particular function is pretty simple. Provide the server you’re interested in, the community string to access this information, and the OID you’re interested in getting the information from and the function returns the value of the OID. You’ll have to alter the script so that it knows where to look for your SNMPGET.exe, but that should be the only modification you’ll need to get this function working in scripts of your own.

In my next post, I’ll elaborate on this information, showing you how to use this function in a sample VBScript file.

1 thought on “VBScript SNMP Queries”

Leave a Comment