This is a simple example of how to read SNMP values across the network from remote SNMP enabled devices like printers, routers, etc.
Note: in order to compile this example, you will need PB9 and the new include files from Jose Roca's site:
His new include files closely match the original C++ include files which covers much more than the ones that came with PB9. For example the ones that came with PB did not have the SNMP stuff. And since they match the MSDN versions, you can see which ones to include based on the API help. I have converted a couple hundred of my programs to using his include files and they only required small changes such as adding additional include statements as needed. I highly recommend them.
For more information on what MIBs/OIDs to use with SNMP, I recommend
searching the internet. (ex: http://www.oid-info.com/get/1.3 )
To post questions or comments, go here:
:ven: Enjoy:
Note: in order to compile this example, you will need PB9 and the new include files from Jose Roca's site:
His new include files closely match the original C++ include files which covers much more than the ones that came with PB9. For example the ones that came with PB did not have the SNMP stuff. And since they match the MSDN versions, you can see which ones to include based on the API help. I have converted a couple hundred of my programs to using his include files and they only required small changes such as adding additional include statements as needed. I highly recommend them.
For more information on what MIBs/OIDs to use with SNMP, I recommend
searching the internet. (ex: http://www.oid-info.com/get/1.3 )
To post questions or comments, go here:
:ven: Enjoy:
Code:
'------------------------------------------------------------------------------ ' SNMP_Read.bas by William Burns (modified on 09/19/2008) ' ' Sample program to show how to read SNMP information using the SNMP ' object identifer (also known as MIB) ' ' IMPORTANT: In order to compile this, you will need PowerBasic 9 and the ' full set of WinAPI includes from Jose Roca's site (ver 1.05 or newer): ' http://www.jose.it-berater.org/smfforum/index.php?board=318.0 ' (his include files cover a lot more of windows header files than the ' include files that came with PB, so they are definately worth using) ' '------------------------------------------------------------------------------ ' ' NOTE: I did not need to SET/CHANGE any SNMP values so I did not ' create a function to set the values. My program is read only. ' ' But if you need this, it can be done fairly easy using the following: ' ' 1.) use existing code to connect using a community string that has write access ' 2.) set up the OID vars and bind them (same as in read function) using something like: ' SnmpUtilOidCpy([email protected], ASNOID) ' [email protected] = %ASN_NULL ' 3.) assign the variable using the correct UDT member according to the type ' (this is kinda the reverse of my SNMP_AnyToStr() function) - let me know if you need help ' 4.) call the SnmpMgrRequest() function with %SNMP_PDU_SET instead of %SNMP_PDU_GET ' 5.) free the needed buffers and do cleanup. ' 6.) Done ' '------------------------------------------------------------------------------ #COMPILE EXE #DIM ALL %USEMACROS = 1 #IF NOT %DEF(%WINAPI) #INCLUDE "WIN32API.INC" 'using the wrapper that Jose Roca created #ENDIF #INCLUDE "MgmtAPI.inc" 'MgmtAPI.h converted to PB by Jose Roca #INCLUDE "SNMP.inc" 'SNMP.h converted to PB by Jose Roca '================================================== %IDD_DIALOG1 = 101 %IDC_FRAME1 = 1001 %IDC_TXTIPADDRESS = 1002 %IDC_BUTCONNECT = 1003 %IDC_FRAME2 = 1004 %IDC_CBMIB = 1005 %IDC_BUTGET = 1006 %IDC_BUTGETSUB = 1007 %IDC_BUTGETALL = 1008 %IDC_BUTSAVELIST = 1009 %IDC_FRAME3 = 1010 %IDC_LBRESULTS = 1011 %IDC_LABMSG = 1012 %IDC_FRAME4 = 1013 %IDC_TXTCOMMUNITY = 1014 '================================================== %SNMP_TIMEOUT = 1000 %SNMP_RETRIES = 3 GLOBAL ghDlg AS DWORD 'global handle to our window GLOBAL ghSession AS DWORD 'global handle for our current SNMP session '================================================== ' ** Declarations ** '================================================== DECLARE CALLBACK FUNCTION DIALOG1Proc() '------------------------------------------------------------------------------ '================================================== ' WinErrMsg - Translate Error Messages '================================================== FUNCTION WinErrMsg(BYVAL dCode AS DWORD) AS STRING LOCAL zBuffer AS ASCIIZ * 255 'the next line is a built in Windows API that translates a numeric code to the text version of a error code CALL FormatMessage(%FORMAT_MESSAGE_FROM_SYSTEM, BYVAL %NULL, dCode, %NULL, zBuffer, SIZEOF(zBuffer), BYVAL %NULL) FUNCTION = FORMAT$(dCode, "##### ") & TRIM$(zBuffer, ANY CHR$(0,32,13,10)) END FUNCTION '================================================== ' MyMsg - shows the message on the IDC_LABMSG label '================================================== SUB MyMsg(BYVAL sText AS STRING) CONTROL SET TEXT ghDlg, %IDC_LABMSG, sText END SUB '================================================== ' AddMyList - adds a line to the listbox '================================================== SUB AddMyList(BYVAL sLine AS STRING) LOCAL iRet AS LONG LOCAL ASZ AS APISIZE LOCAL hCtl AS LONG LOCAL hDC AS LONG LOCAL hFont AS LONG STATIC iLstWidth AS LONG CONTROL HANDLE ghDlg, %IDC_LBRESULTS TO hCtl CONTROL SEND ghDlg, %IDC_LBRESULTS, %WM_GETFONT, 0, 0 TO hFont hDC = GetDC(hCtl) 'get the DC for the listbox IF hDC THEN IF hFont THEN hFont = SelectObject(hDC, hFont) 'select font in to it GetTextExtentPoint32 hDC, BYCOPY sLine + "M", LEN(sLine) + 1, ASZ 'get text length IF ASZ.cx > iLstWidth THEN iLstWidth = ASZ.cx CONTROL SEND ghDlg, %IDC_LBRESULTS, %LB_SETHORIZONTALEXTENT, iLstWidth, 0 END IF LISTBOX ADD ghDlg, %IDC_LBRESULTS, sLine END IF END SUB '================================================== ' Save_List will save the listbox contents to a file '================================================== SUB Save_List(BYVAL hWin AS DWORD, BYVAL iListBox AS LONG, BYVAL sSaveFile AS STRING) LOCAL iCount AS LONG LOCAL iTotal AS LONG LOCAL hFile AS DWORD LOCAL Filter AS STRING LOCAL iLen AS LONG LOCAL sText AS STRING LISTBOX GET COUNT hWin, iListBox TO iTotal IF sSaveFile <> "" AND iTotal > 0 THEN TRY hFile = FREEFILE OPEN sSaveFile FOR OUTPUT AS #hFile FOR iCount = 1 TO iTotal LISTBOX GET TEXT hWin, iListBox, iCount TO sText PRINT #hFile, TRIM$(sText, ANY CHR$(0,32)) NEXT iCount CLOSE hFile MSGBOX "File " + sSaveFile + " was saved.",,"Done" CATCH CLOSE hFile MSGBOX "Error saving file " + sSaveFile,,"Error " + STR$(ERR) + " " + ERROR$(ERR) END TRY END IF END SUB '================================================== ' SnmpErrMsg - Translate Snmp Error Messages '================================================== FUNCTION SnmpErrMsg(BYVAL dStatus AS LONG) AS STRING SELECT CASE dStatus CASE %SNMP_ERRORSTATUS_NOERROR FUNCTION = "SNMP_ERRORSTATUS_NOERROR" CASE %SNMP_ERRORSTATUS_TOOBIG FUNCTION = "SNMP_ERRORSTATUS_TOOBIG" CASE %SNMP_ERRORSTATUS_NOSUCHNAME FUNCTION = "SNMP_ERRORSTATUS_NOSUCHNAME" CASE %SNMP_ERRORSTATUS_BADVALUE FUNCTION = "SNMP_ERRORSTATUS_BADVALUE" CASE %SNMP_ERRORSTATUS_READONLY: FUNCTION = "SNMP_ERRORSTATUS_READONLY" CASE %SNMP_ERRORSTATUS_GENERR FUNCTION = "SNMP_ERRORSTATUS_GENERR" CASE %SNMP_ERRORSTATUS_NOACCESS FUNCTION = "SNMP_ERRORSTATUS_NOACCESS" CASE %SNMP_ERRORSTATUS_WRONGTYPE FUNCTION = "SNMP_ERRORSTATUS_WRONGTYPE" CASE %SNMP_ERRORSTATUS_WRONGLENGTH FUNCTION = "SNMP_ERRORSTATUS_WRONGLENGTH" CASE %SNMP_ERRORSTATUS_WRONGENCODING FUNCTION = "SNMP_ERRORSTATUS_WRONGENCODING" CASE %SNMP_ERRORSTATUS_WRONGVALUE FUNCTION = "SNMP_ERRORSTATUS_WRONGVALUE" CASE %SNMP_ERRORSTATUS_NOCREATION FUNCTION = "SNMP_ERRORSTATUS_NOCREATION" CASE %SNMP_ERRORSTATUS_INCONSISTENTVALUE FUNCTION = "SNMP_ERRORSTATUS_INCONSISTENTVALUE" CASE %SNMP_ERRORSTATUS_RESOURCEUNAVAILABLE FUNCTION = "SNMP_ERRORSTATUS_RESOURCEUNAVAILABLE" CASE %SNMP_ERRORSTATUS_COMMITFAILED FUNCTION = "SNMP_ERRORSTATUS_COMMITFAILED" CASE %SNMP_ERRORSTATUS_UNDOFAILED FUNCTION = "SNMP_ERRORSTATUS_UNDOFAILED" CASE %SNMP_ERRORSTATUS_AUTHORIZATIONERROR FUNCTION = "SNMP_ERRORSTATUS_AUTHORIZATIONERROR" CASE %SNMP_ERRORSTATUS_NOTWRITABLE FUNCTION = "SNMP_ERRORSTATUS_NOTWRITABLE" CASE %SNMP_ERRORSTATUS_INCONSISTENTNAME FUNCTION = "SNMP_ERRORSTATUS_INCONSISTENTNAME" CASE ELSE FUNCTION = "Unknown Error" END SELECT END FUNCTION '================================================== ' SNMP_Connect will connect to an IP address and return the session handle '================================================== FUNCTION SNMP_Connect(BYVAL sIP AS STRING, BYVAL sCommunity AS STRING) AS DWORD LOCAL iRet AS LONG LOCAL hRet AS DWORD LOCAL zAddress AS ASCIIZ * 20 LOCAL zCommunity AS ASCIIZ * 50 zAddress = sIP zCommunity = sCommunity hRet = SnmpMgrOpen(zAddress, zCommunity, %SNMP_TIMEOUT, %SNMP_RETRIES) IF hRet = 0 THEN iRet = GetLastError() MyMsg "Error while connecting to " & sIP & " Error:" & WinErrMsg(iRet) ELSE MyMsg sIP & " Connected." END IF FUNCTION = hRet 'return the session handle END FUNCTION '================================================== ' SNMP_Disconnect will close a session handle '================================================== SUB SNMP_Disconnect(BYVAL hSession AS DWORD) CALL SnmpMgrClose(hSession) MyMsg "Disconnected." END SUB '================================================== ' SNMP_AnyToStr converts a AsnAny object to a string ' replacement for SnmpMgrOidToStr() '================================================== FUNCTION SNMP_AnyToStr(sAny AS AsnAny) AS STRING LOCAL iCount AS LONG LOCAL uLen AS DWORD LOCAL puData AS BYTE PTR LOCAL pText AS STRING PTR LOCAL sText AS STRING 'msgbox "SNMP_AnyToStr value:" & str$(sAny.asnType) SELECT CASE sAny.asnType CASE %ASN_INTEGER FUNCTION = FORMAT$(sAny.asnValue.number) CASE %ASN_RFC1155_COUNTER FUNCTION = FORMAT$(sAny.asnValue.counter) CASE %ASN_RFC1155_GAUGE FUNCTION = FORMAT$(sAny.asnValue.gauge) CASE %ASN_RFC1155_TIMETICKS FUNCTION = FORMAT$(sAny.asnValue.ticks) CASE %ASN_OCTETSTRING 'SAME as ASN_RFC1213_DISPSTRING IF (sAny.asnValue.arbitrary.length) THEN uLen = sAny.asnValue.string.length puData = sAny.asnValue.string.stream IF puData <> 0 THEN FOR iCount = 0 TO uLen -1 sText = sText & CHR$(@puData[iCount]) NEXT iCount 'function = sText & " First chars are:" & str$(asc(sText,1)) & str$(asc(sText,2)) & str$(asc(sText,3)) FUNCTION = TRIM$(sText, ANY CHR$(0,1,10,12,21,32)) ELSE FUNCTION = "Invalid pointer on ASN_OCTETSTRING value" END IF END IF CASE %ASN_SEQUENCE ' Same as ASN_SEQUENCEOF IF (sAny.asnValue.arbitrary.length) THEN uLen = sAny.asnValue.sequence.length puData = sAny.asnValue.sequence.stream IF puData <> 0 THEN FOR iCount = 0 TO uLen -1 sText = sText & CHR$(@puData[iCount]) NEXT iCount FUNCTION = sText ELSE FUNCTION = "Invalid pointer on ASN_SEQUENCE value" END IF END IF CASE %ASN_RFC1155_IPADDRESS IF (sAny.asnValue.address.length ) THEN uLen = sAny.asnValue.address.length puData = sAny.asnValue.address.stream FOR iCount = 0 TO uLen - 1 sText = sText & FORMAT$(@puData[iCount]) IF (iCount < uLen-1) THEN sText = sText & "." NEXT iCount FUNCTION = sText END IF CASE %ASN_RFC1155_OPAQUE IF (sAny.asnValue.arbitrary.length) THEN uLen = sAny.asnValue.arbitrary.length puData = sAny.asnValue.arbitrary.stream IF puData <> 0 THEN FOR iCount = 0 TO uLen -1 sText = sText & CHR$(@puData[iCount]) NEXT iCount FUNCTION = sText ELSE FUNCTION = "Invalid pointer on ASN_RFC1155_OPAQUE value" END IF END IF CASE %ASN_OBJECTIDENTIFIER IF (sAny.asnValue.object.idLength ) THEN FOR iCount = 0 TO sAny.asnValue.object.idLength - 1 IF sAny.asnValue.object.ids <> 0 THEN sText = sText & FORMAT$([email protected][iCount]) END IF IF (iCount < sAny.asnValue.object.idLength-1) THEN sText = sText & "." NEXT iCount FUNCTION = sText END IF CASE ELSE ' Unrecognised data type FUNCTION = "Unknown Type(" & FORMAT$(sAny.asnType) & ")" END SELECT END FUNCTION '================================================== ' SNMP_GetOidText will convert the numeric OID to a readable text '================================================== FUNCTION SNMP_GetOidText(BYVAL hSession AS DWORD, BYVAL sMIB AS STRING) AS STRING LOCAL zBuff AS ASCIIZ * 255 LOCAL pzNewBuff AS ASCIIZ PTR LOCAL ASNOID AS AsnObjectIdentifier zBuff = sMIB IF SnmpMgrStrToOid(zBuff, ASNOID) THEN IF SnmpMgrOidToStr(ASNOID, BYVAL VARPTR(pzNewBuff)) THEN 'this call sets up its own buffer FUNCTION = TRIM$(@pzNewBuff) SnmpUtilMemFree (pzNewBuff) 'free the buffer setup above ELSE FUNCTION = "SnmpMgrOidToStr failed" END IF ELSE FUNCTION = sMIB END IF END FUNCTION '================================================== ' SNMP_Getrequest will query the device for info on a MIB '================================================== FUNCTION SNMP_Getrequest(BYVAL hSession AS DWORD, BYVAL sMIB AS STRING) AS DWORD LOCAL iErrIndex AS LONG LOCAL iErrStatus AS LONG LOCAL sTextOut AS STRING LOCAL ASNOID AS AsnObjectIdentifier LOCAL snmpVarList AS SnmpVarBindList LOCAL snmpVar AS SnmpVarBind LOCAL zOID AS ASCIIZ * 255 LISTBOX RESET ghDlg, %IDC_LBRESULTS zOID = sMIB IF ISFALSE SnmpMgrStrToOid(zOID, ASNOID) THEN MyMsg "Failed to convert MIB string to OID structure" ELSE snmpVarList.list = VARPTR(snmpVar) snmpVarList.len = 1 IF snmpVarList.list = 0 THEN MyMsg "Something failed in the SnmpUtilMemReAlloc call. Aborting..." GOTO Terminate END IF 'Assigning OID to variable bindings list SnmpUtilOidCpy([email protected], ASNOID) [email protected] = %ASN_NULL ' initiates the GET request IF ISFALSE SnmpMgrRequest(hSession, %SNMP_PDU_GET, snmpVarList, iErrStatus, iErrIndex) THEN MyMsg "Snmp Request Failed. " & STR$(iErrIndex) & " Error:" & SnmpErrMsg(iErrStatus) GOTO Terminate END IF IF (iErrStatus > 0) THEN MyMsg "Snmp Request status. " & STR$(iErrIndex) & " Error:" & SnmpErrMsg(iErrStatus) GOTO Terminate END IF 'note: if you need it the type for this data is stored in snmpVarList.list.value.asnType sTextOut = SNMP_AnyToStr([email protected]) AddMyList sTextOut MyMsg "Done" END IF Terminate: SnmpUtilVarBindListFree(snmpVarList) SnmpUtilOidFree(ASNOID) END FUNCTION '================================================== ' SNMP_GetAll will query the device for info on a MIBs and ' it will traverse to the next item until it finds a stopping point '================================================== FUNCTION SNMP_GetAll(BYVAL hSession AS DWORD, BYVAL sMIB AS STRING, BYVAL iStopAtSub AS LONG) AS DWORD LOCAL iCount AS LONG LOCAL iTotal AS LONG LOCAL iError AS LONG LOCAL iErrIndex AS LONG LOCAL iErrStatus AS LONG LOCAL sTextOut AS STRING LOCAL ASNOID AS AsnObjectIdentifier LOCAL snmpVarList AS SnmpVarBindList LOCAL snmpBuff AS STRING LOCAL sNewMIB AS STRING LOCAL sValue AS STRING LOCAL zOID AS ASCIIZ * 255 LISTBOX RESET ghDlg, %IDC_LBRESULTS 'CLEAR just in case we dont get anything zOID = sMIB IF ISFALSE SnmpMgrStrToOid(zOID, ASNOID) THEN MyMsg "Failed to convert MIB string to OID structure" iError = %TRUE ELSE snmpBuff = STRING$(SIZEOF(SnmpVarBind),CHR$(0)) snmpVarList.list = STRPTR(snmpBuff) snmpVarList.len = 1 'snmpVarList.list = SnmpUtilMemReAlloc(byval varptr(snmpVar), sizeof(SnmpVarBind) * snmpVarList.len) snmpBuff = snmpBuff & STRING$( (SIZEOF(SnmpVarBind) * snmpVarList.len) - LEN(snmpBuff), CHR$(0)) 'add any needed space IF snmpVarList.list = 0 THEN MyMsg "Something failed in the SnmpUtilMemReAlloc call. Aborting..." iError = %TRUE GOTO TerminateAll END IF 'Assigning OID to variable bindings list SnmpUtilOidCpy([email protected], ASNOID) [email protected] = %ASN_NULL DO 'loop to find each new one IF ghSession = 0 THEN EXIT LOOP 'we disconnected so exit ' initiates the GET request IF ISFALSE SnmpMgrRequest(hSession, %SNMP_PDU_GETNEXT, snmpVarList, iErrStatus, iErrIndex) THEN MyMsg "Snmp Request Failed. " & STR$(iErrIndex) & " Error:" & SnmpErrMsg(iErrStatus) iError = %TRUE EXIT DO END IF IF (iErrStatus = %SNMP_ERRORSTATUS_NOSUCHNAME) THEN EXIT DO IF (iErrStatus > 0) THEN MyMsg "Snmp Request status. " & STR$(iErrIndex) & " Error:" & SnmpErrMsg(iErrStatus) iError = %TRUE EXIT DO END IF sNewMIB = "." FOR iCount = 0 TO [email protected] - 1 IF [email protected] <> 0 THEN sNewMIB = sNewMIB & FORMAT$([email protected]@ids[iCount]) END IF IF (iCount < [email protected]) THEN sNewMIB = sNewMIB & "." NEXT iCount IF iStopAtSub AND (LEFT$(sNewMIB, LEN(sMIB)) <> sMIB) THEN EXIT DO 'no longer under this branch so exit IF iTotal > 10 AND sNewMIB = sMIB THEN EXIT DO 'we have looped all the way back so exit sValue = SNMP_AnyToStr([email protected]) AddMyList sNewMIB & " = " & sValue DIALOG DOEVENTS 1 iTotal = iTotal + 1 IF (iTotal MOD 500) = 0 THEN IF MSGBOX("I have collected 500 or more items, do you want me to conitunue?",%MB_YESNO,"Conitnue?") <> %IDYES THEN EXIT DO END IF LOOP IF iError <> %TRUE THEN MyMsg "Done. " & STR$(iTotal) & " items." 'only show Done msg if it worked, otherwise leave the err msg END IF TerminateAll: SnmpUtilVarBindListFree(snmpVarList) SnmpUtilOidFree(ASNOID) END FUNCTION '================================================== ' ** CallBacks ** '================================================== CALLBACK FUNCTION DIALOG1Proc() LOCAL hRet AS DWORD LOCAL iRet AS LONG LOCAL sIP AS STRING LOCAL sCommunity AS STRING LOCAL sMIB AS STRING LOCAL sFile AS STRING SELECT CASE AS LONG CB.MSG CASE %WM_INITDIALOG ' Initialization handler CASE %WM_CLOSE IF ghSession <> 0 THEN 'close session before exiting SNMP_Disconnect ghSession END IF CASE %WM_NCACTIVATE STATIC hWndSaveFocus AS DWORD IF ISFALSE CBWPARAM THEN ' Save control focus hWndSaveFocus = GetFocus() ELSEIF hWndSaveFocus THEN ' Restore control focus SetFocus(hWndSaveFocus) hWndSaveFocus = 0 END IF CASE %WM_USER + 101 CONTROL GET TEXT CB.HNDL, %IDC_CBMIB TO sMIB MyMsg SNMP_GetOidText(ghSession, sMIB) CASE %WM_COMMAND ' Process control notifications SELECT CASE AS LONG CB.CTL CASE %IDC_BUTCONNECT IF CB.CTLMSG = %BN_CLICKED OR CB.CTLMSG = 1 THEN IF ghSession = 0 THEN 'no session CONTROL GET TEXT CB.HNDL, %IDC_TXTIPADDRESS TO sIP CONTROL GET TEXT CB.HNDL, %IDC_TXTCOMMUNITY TO sCommunity ghSession = SNMP_Connect(sIP, sCommunity) IF ghSession THEN CONTROL SET TEXT CBHNDL, %IDC_BUTCONNECT, "&Disconnect" ELSE 'we have an existing session so close it SNMP_Disconnect ghSession ghSession = 0 CONTROL SET TEXT CB.HNDL, %IDC_BUTCONNECT, "&Connect" END IF END IF CASE %IDC_LBRESULTS IF CB.CTLMSG = %LBN_SELCHANGE THEN LISTBOX GET TEXT CB.HNDL, %IDC_LBRESULTS TO sMIB iRet = INSTR(1, sMIB, " = ") IF iRet THEN 'only show items from the listbox if they show a MIB sMIB = LEFT$(sMIB, iRet - 1) ELSE CONTROL GET TEXT CB.HNDL, %IDC_CBMIB TO sMIB END IF MyMsg SNMP_GetOidText(ghSession, sMIB) END IF CASE %IDC_CBMIB IF CB.CTLMSG = %CBN_EDITCHANGE OR CB.CTLMSG = %CBN_SELCHANGE THEN PostMessage CB.HNDL, %WM_USER + 101, 101, 101 END IF CASE %IDC_BUTGET IF CB.CTLMSG = %BN_CLICKED OR CB.CTLMSG = 1 THEN IF ghSession <> 0 THEN 'we have a session open MyMsg "Please wait..." CONTROL GET TEXT CB.HNDL, %IDC_CBMIB TO sMIB CALL SNMP_Getrequest(ghSession, sMIB) ELSE MyMsg "You must Connect first" END IF END IF CASE %IDC_BUTGETALL IF CB.CTLMSG = %BN_CLICKED OR CB.CTLMSG = 1 THEN IF ghSession <> 0 THEN 'we have a session open MyMsg "Please wait..." CONTROL GET TEXT CBHNDL, %IDC_CBMIB TO sMIB CALL SNMP_GetAll(ghSession, sMIB, %FALSE) ELSE MyMsg "You must Connect first" END IF END IF CASE %IDC_BUTGETSUB IF CB.CTLMSG = %BN_CLICKED OR CB.CTLMSG = 1 THEN IF ghSession <> 0 THEN 'we have a session open MyMsg "Please wait..." CONTROL GET TEXT CBHNDL, %IDC_CBMIB TO sMIB CALL SNMP_GetAll(ghSession, sMIB, %TRUE) ELSE MyMsg "You must Connect first" END IF END IF CASE %IDC_BUTSAVELIST IF CB.CTLMSG = %BN_CLICKED OR CB.CTLMSG = 1 THEN CONTROL GET TEXT CBHNDL, %IDC_CBMIB TO sMIB DISPLAY SAVEFILE CBHNDL,,,"Name of file to save report.","",CHR$("Text file",0,"*.TXT",0,"All Files",0,"*.*",0),"SNMP_" & sMIB & ".TXT","TXT",%OFN_CREATEPROMPT TO sFile IF LEN(sFile) AND sFile <> "SNMP_" & sMIB & ".TXT" THEN Save_List(CBHNDL, %IDC_LBRESULTS, sFile) END IF END SELECT END SELECT END FUNCTION '================================================== '================================================== FUNCTION PBMAIN() as LONG LOCAL iRet AS LONG LOCAL hDlg AS DWORD DIALOG NEW 0, "SNMP data retrieval", 150, 97, 334, 259, %WS_POPUP OR %WS_BORDER _ OR %WS_DLGFRAME OR %WS_THICKFRAME OR %WS_CAPTION OR %WS_SYSMENU OR %WS_MINIMIZEBOX _ OR %WS_MAXIMIZEBOX OR %WS_CLIPSIBLINGS OR %WS_VISIBLE OR %DS_MODALFRAME OR %DS_3DLOOK _ OR %DS_NOFAILCREATE OR %DS_SETFONT, %WS_EX_WINDOWEDGE OR %WS_EX_CONTROLPARENT _ OR %WS_EX_LEFT OR %WS_EX_LTRREADING, TO hDlg CONTROL ADD TEXTBOX, hDlg, %IDC_TXTIPADDRESS, "127.0.0.1", 15, 15, 140, 10 CONTROL ADD TEXTBOX, hDlg, %IDC_TXTCOMMUNITY, "public", 10, 40, 150, 15 CONTROL ADD BUTTON, hDlg, %IDC_BUTCONNECT, "&Connect", 170, 10, 65, 15 CONTROL ADD BUTTON, hDlg, %IDC_BUTSAVELIST, "Save List", 280, 90, 50, 15 CONTROL ADD FRAME, hDlg, %IDC_FRAME2, "MIB to retrieve", 5, 60, 260, 40 CONTROL ADD COMBOBOX, hDlg, %IDC_CBMIB, , 10, 72, 250, 250, %CBS_DROPDOWN OR %CBS_HASSTRINGS _ OR %WS_TABSTOP OR %WS_CHILD OR %WS_VISIBLE OR %WS_VSCROLL, %WS_EX_LEFT OR %WS_EX_LTRREADING OR %WS_EX_RIGHTSCROLLBAR CONTROL ADD BUTTON, hDlg, %IDC_BUTGET, "Get Item", 10, 87, 55, 10 CONTROL ADD BUTTON, hDlg, %IDC_BUTGETSUB, "Get Sub items", 70, 87, 55, 10 CONTROL ADD BUTTON, hDlg, %IDC_BUTGETALL, "Get All", 130, 87, 55, 10 CONTROL ADD LISTBOX, hDlg, %IDC_LBRESULTS, , 10, 115, 315, 110, %WS_CHILD OR %WS_VISIBLE OR %LBS_NOTIFY OR %LBS_NOINTEGRALHEIGHT OR %WS_VSCROLL _ OR %WS_HSCROLL OR %LBS_USETABSTOPS OR %LBS_DISABLENOSCROLL OR %WS_TABSTOP, %WS_EX_CLIENTEDGE CONTROL ADD LABEL, hDlg, %IDC_LABMSG, "Type in IP address and click " + _ "Connect. Then Get Item.", 5, 235, 325, 20, %WS_CHILD OR _ %WS_VISIBLE OR %SS_CENTER OR %SS_SUNKEN, %WS_EX_LEFT OR _ %WS_EX_LTRREADING CONTROL ADD FRAME, hDlg, %IDC_FRAME1, "Connect to", 5, 5, 160, 25 CONTROL ADD FRAME, hDlg, %IDC_FRAME4, "Community", 5, 30, 160, 30 CONTROL ADD FRAME, hDlg, %IDC_FRAME3, "Results", 5, 105, 325, 125 ghDlg = hDlg 'assign to the global COMBOBOX ADD hDlg, %IDC_CBMIB, ".1.3.6.1.2.1.1.5.0" 'device name COMBOBOX ADD hDlg, %IDC_CBMIB, ".1.3.6.1.2.1.25.3.2.1.3.1" 'generic device description COMBOBOX ADD hDlg, %IDC_CBMIB, ".1.3.6.1.4.1.11.2.3.9.4.2.1.1.3.2.0" 'prt model (on HPs anyway) COMBOBOX ADD hDlg, %IDC_CBMIB, ".1.3.6.1.4.1.11.2.3.9.4.2.1.1.3.3.0" 'printer serial number (on HPs anyway) COMBOBOX ADD hDlg, %IDC_CBMIB, ".1.3.6.1.4.1.641.2.1.2.1.2.1" 'prt model IBM COMBOBOX ADD hDlg, %IDC_CBMIB, ".1.3.6.1.4.1.641.2.1.2.1.6.1" 'IBM SN COMBOBOX ADD hDlg, %IDC_CBMIB, ".1.3.6.1.4.1.11.2.3.9.1.1.7.0" 'jetdirect prt models COMBOBOX ADD hDlg, %IDC_CBMIB, ".1.3.6.1.4.1.11.2.4.3.1.10.0" 'jetdirect model COMBOBOX ADD hDlg, %IDC_CBMIB, ".1.3.6.1.4.1.11.2.3.9.1.1.18.0" 'Lexmark prt models COMBOBOX ADD hDlg, %IDC_CBMIB, ".1.3.6.1.4.1.641.2.1.2.1.6.1" 'IBM SN COMBOBOX ADD hDlg, %IDC_CBMIB, ".1.3.6.1.2.1.43.5.1.1.17.1" 'HP SN COMBOBOX ADD hDlg, %IDC_CBMIB, ".1.3.6.1.2.1.43.8.2.1.14.1.1" 'Printer MIB input vendor name COMBOBOX ADD hDlg, %IDC_CBMIB, ".1.3.6.1.4.1" 'private enterprise (USE GETALL) COMBOBOX ADD hDlg, %IDC_CBMIB, ".1.3.6.1.2.1.43" 'printer MIB (USE GETALL) COMBOBOX ADD hDlg, %IDC_CBMIB, ".1.3.6" 'maint tree (USE GETALL) COMBOBOX SELECT hDlg, %IDC_CBMIB, 1 DIALOG SHOW MODAL hDlg, CALL DIALOG1Proc TO iRet FUNCTION = iRet END FUNCTION '==================================================
Comment