SNMPGET? How about an SNMPWALK?

Earlier I posted a blog about how to use SNMPGET to return information from any SNMP OID, or Object ID, that we were interested in retrieving information from. It was pretty easy and straight forward to pull this information. We used a utility from NETSNMP called SNMPGET.exe to get this information.

Well in that same SNMP package there is another utility called SNMPWALK.exe. Well this utility is particularly interesting because we can use it to walk an entire OID sub-key and return all of the values listed. So instead of pulling every single value individually via a loop, which can be a time consuming process because we have to negotiate security every time and re-establish our network connectivity, we can just query the whole sub key, and then parse that information client side.

So, in order to utilize this SNMPWALK.exe functionality, we’ll need another script:

Function SNMPWALK(vServer, vCommunityString, vOID, vReturnOID)
    On Error Resume Next

    'This function will return the data from an SNMP walk from the specified
    'server and the specified OID. The information will have to be parsed
    'afterward. The info will be retuned as the variable "SNMPWALK".
    '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
    
    If IsNull(vReturnOID) Or vReturnOID = 0 Then
      vReturnOID = False
    End If

    Const OpenAsDefault = -2
    Const FailIfNotExist = 0
    Const ForReading = 1

    vSNMPWalkPath = "C:\ScriptFiles" 

    Set WshShell = CreateObject("WScript.Shell") 'Create filesystem objects
    sTemp = WshShell.ExpandEnvironmentStrings("%TEMP%")
    sTempFile = sTemp & "\runresult.tmp" 'Create working file

    'Run the SNMPWALK command and save results to tmp file
    WshShell.Run "%comspec% /C " & vSNMPWalkPath & "\snmpwalk -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, _
    OpenAsDefault)
    
    Do Until fFile.AtEndOfStream
      SNMPWALKREAD = fFile.Readline
    
      If InStr(SNMPWALKREAD, "INTEGER:") > 0 Then
          aSNMPWALK = Split(SNMPWALKREAD, "INTEGER:")
          vTemp = Trim(aSNMPWALK(1))
          vTemp = vTemp + 0
          If vReturnOID = True Then
            aSNMPOID = Split(SNMPWALKREAD, " = ")
            vTemp = Trim(aSNMPOID(0)) & " = " & vTemp
          End If
      ElseIf InStr(SNMPWALKREAD, "STRING:") > 0 Then
          aSNMPWALK = Split(SNMPWALKREAD, "STRING:")
          vTemp = Trim(aSNMPWALK(1))
          If vReturnOID = True Then
            aSNMPOID = Split(SNMPWALKREAD, " = ")
            vTemp = Trim(aSNMPOID(0)) & " = " & vTemp
          End If
      ElseIf InStr(SNMPWALKREAD, "IpAddress:") > 0 Then
          aSNMPWALK = Split(SNMPWALKREAD, "IpAddress:")
          vTemp = Trim(aSNMPWALK(1))
          If vReturnOID = True Then
            aSNMPOID = Split(SNMPWALKREAD, " = ")
            vTemp = Trim(aSNMPOID(0)) & " = " & vTemp
          End If
      ElseIf InStr(SNMPWALKREAD, "Hex-STRING:") > 0 Then
          aSNMPWALK = Split(SNMPWALKREAD, "IpAddress:")
          vTemp = Trim(aSNMPWALK(1))
          If vReturnOID = True Then
            aSNMPOID = Split(SNMPWALKREAD, " = ")
            vTemp = Trim(aSNMPOID(0)) & " = " & vTemp
          End If
      End If
      
      If IsEmpty(SNMPWALK) Then
          SNMPWALK = vTemp
      Else
          SNMPWALK = SNMPWALK & "|" & vTemp
      End If
    Loop
    
    SNMPWALK = Replace(SNMPWALK, "iso", ".1")
    
    fFile.Close
    oFSO.DeleteFile (sTempFile)
End Function

The SNMPWALK function works pretty much the same way as the SNMPGET function, the only big difference is that it returns a string separated by the “|” character. You also have the option of returning the OID, that the information was returned from, in the string returned by the function. This can be particularly handy if you have a bunch of OIDs you would like to check against but don’t want the overhead of calling them all one by one via the SNMPGET function.

I wrote one more function to parse the information from the SNMPWALK function to retrieve the information you are looking for from the results of the SNMPWALK:

Function GETFROMSNMPWALK(vOID, vStringFromSNMPWalk)
  'This function will take a walk from the walk function and find the value
  'returned from the walk corresponding to vOID. If the OID can't be found
  'the function will return "FALSE".
  '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

  aValues = Split(vStringFromSNMPWalk, "|")
  For Each vValue In aValues
    aCompareString = Split(vValue, " = ")
    If Trim(aCompareString(0)) = Trim(vOID) Then
      GETFROMSNMPWALK = aCompareString(1)
      Exit For
    End If
  Next
  If IsEmpty(GETFROMSNMPWALK) Then GETFROMSNMPWALK = False

End Function

So there it is. I will post some sample scripts that utilize all three of these functions in an upcoming blog. The script I will post was written to pull in hard drive and controller information from Compaq and HP servers but these scripts can be used for any device that has SNMP network connectivity.

Feel free to leave comments or questions and good luck with your scripting.

1 thought on “SNMPGET? How about an SNMPWALK?”

Leave a Comment