Announcement

Collapse
No announcement yet.

Example of reading SNMP values

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Example of reading SNMP values

    I just posted an example of how to read SNMP values from a remote network device.

    http://www.powerbasic.com/support/pb...309#post297309


    In order to compile this program you will need PB9 and the new include files from Jose Roca's site. (the link is in the source code posting) I highly recommend his include files since they cover much more of the windows API than the includes that came with PB.

    SNMP can be kinda hard to find the correct OID/MIB for the information you are looking for. I gave you some examples in the combobox for printer info which is what I was after. And to make it easier I also included a couple functions that can "walk" through the OID trees. Just give it the first part of the OID and click Get All and it will list the items found on that device by getting the "next" OID. Some other functions that may not be obivous is that you can click on the item in the results listbox and it will display the text version of the OID string at the bottom and you can use the text version instead when listing the OID to read from. SNMP can still be a challenge to find what you are after, (since much of the data is in the private section which can vary from vendor to vendor) so I recommend researching the ones you want on the internet at sites like:
    http://www.oid-info.com/get/1.3

    Let me know if you have any questions,
    "I haven't lost my mind... its backed up on tape... I think??" :D

  • #2
    i just looked at this source but was unable to locate the required headers on jose's site. the link was broken and a search didn't turn up the headers. does anyone have them?

    tia
    don
    Don Dickinson
    www.greatwebdivide.com

    Comment


    • #3
      The headers are here: http://www.jose.it-berater.org/smffo...hp?board=344.0

      They are not isolated include files, but a whole package like the Windows SDK Headers.
      Forum: http://www.jose.it-berater.org/smfforum/index.php

      Comment


      • #4
        Hi,

        I see a very good reading software for snmp, (but smnp is soo simple! not for me!), I have to change only one value (Writing), someone have a example how to do that with this nice software ???

        Sorry for my snmp limited knowledge ...

        Thank you for your help

        Guy

        Comment


        • #5
          Here is a function that I added to that tool to write SNMP string values. Add this function to that code and add a button to write. Then have the button prompt for the value (and grab the mib to update) and then call this function. Just keep in mind you must connect using a "password" that has write access. And not all values can be overwritten on all devices. (some can be static)


          Code:
          '==================================================
          ' SNMP_WriteValue will update a SNMP value
          '==================================================
          FUNCTION SNMP_WriteValue(BYVAL hSession AS dword, BYVAL sMIB AS STRING, byval sNewValue 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
          
             if hSession = 0 then exit function
          
             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_OCTETSTRING
          
                [email protected] = %TRUE
                [email protected] = len(sNewValue)
                [email protected] = strptr(sNewValue)
          
                ' initiates the SET request
                if isfalse SnmpMgrRequest(hSession, %SNMP_PDU_SET, snmpVarList, iErrStatus, iErrIndex) then
                   MyMsg "Snmp Write Request Failed.  " & str$(iErrIndex) & " Error:" & SnmpErrMsg(iErrStatus)
                   goto Terminate
                else
                end if
          
                if (iErrStatus > 0) then   '%SNMP_ERRORSTATUS_NOSUCHNAME
                   MyMsg "Snmp Write Request status.  " & str$(iErrIndex) & " Error:" & SnmpErrMsg(iErrStatus)
                   goto Terminate
                end if
          
          
                AddMyList "Value updated for " & sMib & " to: " & sNewValue
                MyMsg "Done"
          
             end if
          
             Terminate:
          
             SnmpUtilVarBindListFree(snmpVarList)
             SnmpUtilOidFree(ASNOID)
          
          
          end function
          "I haven't lost my mind... its backed up on tape... I think??" :D

          Comment


          • #6
            Hi William,

            First Thank You for your help with your software, everything is ok for writing without any password, I'm searching now how to insert the password with the connection / writing. Could you tell what function do that with snmp ??

            I do a check in msdn for "SnmpMgrOpen" , "SnmpMgrRequest", I can't put the password ... I look to microsoft but I dont see something helpfull.

            http://msdn.microsoft.com/en-us/libr...(v=VS.85).aspx

            Do you think I can have hint?, (It's cold or hot ... for a green snmp Guy)

            Thank you

            Guy

            Comment


            • #7
              Sorry, for the confusion. The SNMP password is also known as a community string. So on my function to connect, you provide it the community string(password).

              Code:
              SNMP_Connect(BYVAL sIP AS STRING, BYVAL sCommunity AS STRING)
              Most devices will use a default string of "public" for reading access unless someone has changed it to something else.
              "I haven't lost my mind... its backed up on tape... I think??" :D

              Comment


              • #8
                Hi William,

                I think I'm confused with snmp v3 and secure access, but I test with my switch and working fine! :beer:

                For snmpV3 it's another story ! for the moment I will play with my snmp toy !

                Thank you very much for your software and your help

                I will receive my book for snmp soon ... I hope I will able to learn this (simple)
                snmp !

                Have a good evening

                Guy

                Comment


                • #9
                  Hi William,

                  I discover the SNMP_WriteValue working fine with string (%ASN_OCTETSTRING), but for the integer the value is the asci code, is it possible to use the same routine with %ASN_INTEGER ???

                  When I put the %ASN_INTEGER and the value 0 or 1, wireshark give me the good code but wrong value => value(integer32): 8661316

                  snmp soo simple

                  Thank you


                  '==================================================
                  ' SNMP_AnyToStr converts a AsnAny object to a string
                  ' replacement for SnmpMgrOidToStr()
                  '==================================================

                  typedef struct {
                  BYTE asnType;
                  union {
                  AsnInteger32 number; <----------------
                  AsnUnsigned32 unsigned32;
                  AsnCounter64 counter64;
                  AsnOctetString string; <---------------
                  AsnBits bits;
                  AsnObjectIdentifier object;
                  AsnSequence sequence;
                  AsnIPAddress address;
                  AsnCounter32 counter;
                  AsnGauge32 gauge;
                  AsnTimeticks ticks;
                  AsnOpaque arbitrary;
                  } asnValue;
                  } AsnAny;

                  Comment


                  • #10
                    To write a number, you are correct that you have to change %ASN_OCTETSTRING to %ASN_INTEGER. But you also have to change the code that assigns the value since it is assigning a string value right now. I do not have anything I can use to test this code, but I think this would be the correct way to code it:

                    Code:
                    '==================================================
                    ' SNMP_WriteIntVal will update a SNMP integer value
                    '==================================================
                    FUNCTION SNMP_WriteIntVal(BYVAL hSession AS dword, BYVAL sMIB AS STRING, byval iNewValue as long) 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
                    
                       if hSession = 0 then exit function
                    
                       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_INTEGER32
                    
                          [email protected] = iNewValue
                    
                          ' initiates the SET request
                          if isfalse SnmpMgrRequest(hSession, %SNMP_PDU_SET, snmpVarList, iErrStatus, iErrIndex) then
                             MyMsg "Snmp Write Request Failed.  " & str$(iErrIndex) & " Error:" & SnmpErrMsg(iErrStatus)
                             goto Terminate
                          else
                          end if
                    
                          if (iErrStatus > 0) then   '%SNMP_ERRORSTATUS_NOSUCHNAME
                             MyMsg "Snmp Write Request status.  " & str$(iErrIndex) & " Error:" & SnmpErrMsg(iErrStatus)
                             goto Terminate
                          end if
                    
                    
                          AddMyList "Value updated for " & sMib & " to: " & sNewValue
                          MyMsg "Done"
                    
                       end if
                    
                       Terminate:
                    
                       SnmpUtilVarBindListFree(snmpVarList)
                       SnmpUtilOidFree(ASNOID)
                    
                    
                    end function
                    "I haven't lost my mind... its backed up on tape... I think??" :D

                    Comment


                    • #11
                      Hi William,

                      I will test it now with my printer, and I will able to tell you if everything is ok :-), I will check with wireshark too.

                      Before I dont like snmp, but now ... I will like and it's usefull, waiting for my book.

                      Thank you very much

                      PS: I will post your complete code to the other thread (source code), if it's ok for you.

                      Comment


                      • #12
                        Hi William,

                        http://www.powerbasic.com/support/pb...966#post389966

                        I post your new code, I add buttons and I test it with wireshark, at this time everthing is ok, I will do more tests with other devices.

                        SNMP It's hard, but usefull ...

                        Thank you

                        Guy

                        Comment

                        Working...
                        X