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:

Visual Basic

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

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

FunctionSNMPWALK(vServer,vCommunityString,vOID,vReturnOID)

On ErrorResumeNext

'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

IfIsNull(vReturnOID)OrvReturnOID=0Then

vReturnOID=False

EndIf

ConstOpenAsDefault=-2

ConstFailIfNotExist=0

ConstForReading=1

vSNMPWalkPath="C:\ScriptFiles"

SetWshShell=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

SetoFSO=CreateObject("Scripting.FileSystemObject")

SetfFile=oFSO.OpenTextFile(sTempFile,ForReading,FailIfNotExist,_

OpenAsDefault)

DoUntilfFile.AtEndOfStream

SNMPWALKREAD=fFile.Readline

IfInStr(SNMPWALKREAD,"INTEGER:")>0Then

aSNMPWALK=Split(SNMPWALKREAD,"INTEGER:")

vTemp=Trim(aSNMPWALK(1))

vTemp=vTemp+0

IfvReturnOID=TrueThen

aSNMPOID=Split(SNMPWALKREAD," = ")

vTemp=Trim(aSNMPOID(0))&" = "&vTemp

EndIf

ElseIfInStr(SNMPWALKREAD,"STRING:")>0Then

aSNMPWALK=Split(SNMPWALKREAD,"STRING:")

vTemp=Trim(aSNMPWALK(1))

IfvReturnOID=TrueThen

aSNMPOID=Split(SNMPWALKREAD," = ")

vTemp=Trim(aSNMPOID(0))&" = "&vTemp

EndIf

ElseIfInStr(SNMPWALKREAD,"IpAddress:")>0Then

aSNMPWALK=Split(SNMPWALKREAD,"IpAddress:")

vTemp=Trim(aSNMPWALK(1))

IfvReturnOID=TrueThen

aSNMPOID=Split(SNMPWALKREAD," = ")

vTemp=Trim(aSNMPOID(0))&" = "&vTemp

EndIf

ElseIfInStr(SNMPWALKREAD,"Hex-STRING:")>0Then

aSNMPWALK=Split(SNMPWALKREAD,"IpAddress:")

vTemp=Trim(aSNMPWALK(1))

IfvReturnOID=TrueThen

aSNMPOID=Split(SNMPWALKREAD," = ")

vTemp=Trim(aSNMPOID(0))&" = "&vTemp

EndIf

EndIf

IfIsEmpty(SNMPWALK)Then

SNMPWALK=vTemp

Else

SNMPWALK=SNMPWALK&"|"&vTemp

EndIf

Loop

SNMPWALK=Replace(SNMPWALK,"iso",".1")

fFile.Close

oFSO.DeleteFile(sTempFile)

EndFunction

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:

Visual Basic

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

FunctionGETFROMSNMPWALK(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,"|")

ForEachvValue InaValues

aCompareString=Split(vValue," = ")

IfTrim(aCompareString(0))=Trim(vOID)Then

GETFROMSNMPWALK=aCompareString(1)

ExitFor

EndIf

Next

IfIsEmpty(GETFROMSNMPWALK)ThenGETFROMSNMPWALK=False

EndFunction

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.