Python Netsnmp and snmpwalk

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
2
down vote

favorite












I am trying to use snmpwalk to gain some info and stats on some interfaces. I use this:



import netsnmp

serv = "172.16.1.1"
snmp_pass = "private"

oid = netsnmp.VarList('IF-MIB::ifName','IF-MIB::ifDescr')
snmp_res = netsnmp.snmpwalk(oid, Version=2, DestHost=serv, Community=snmp_pass)
for x in snmp_res:
print "snmp_res:: ", x


All I get as answer is:



snmp_res:: lo
snmp_res:: EtherNet Adapter XYZ


The answer is correct but I want more info. When I do same thing from linux command with snmpwalk I get more info such as:



IF-MIB::ifDescr.1 = STRING: lo
IF-MIB::ifDescr.2 = STRING: EtherNet Adapter XYZ


The ID is 2 for "EtherNet Adapter XYZ" and I need that value as well to reference for other stats on interface. How do I get that/them with python and snmp?










share|improve this question















migrated from unix.stackexchange.com Dec 4 at 14:02


This question came from our site for users of Linux, FreeBSD and other Un*x-like operating systems.














  • Have you examined the returned objects to see if more information isn't being returned? All you're seeing is a string representation
    – TheIncorrigible1
    Dec 4 at 14:11











  • I have tried with little luck. I imagine I look at "oid"? It appears to be a "<class 'netsnmp.client.VarList'>". I am really needing ID matched to interface name.
    – Matt
    Dec 4 at 15:52















up vote
2
down vote

favorite












I am trying to use snmpwalk to gain some info and stats on some interfaces. I use this:



import netsnmp

serv = "172.16.1.1"
snmp_pass = "private"

oid = netsnmp.VarList('IF-MIB::ifName','IF-MIB::ifDescr')
snmp_res = netsnmp.snmpwalk(oid, Version=2, DestHost=serv, Community=snmp_pass)
for x in snmp_res:
print "snmp_res:: ", x


All I get as answer is:



snmp_res:: lo
snmp_res:: EtherNet Adapter XYZ


The answer is correct but I want more info. When I do same thing from linux command with snmpwalk I get more info such as:



IF-MIB::ifDescr.1 = STRING: lo
IF-MIB::ifDescr.2 = STRING: EtherNet Adapter XYZ


The ID is 2 for "EtherNet Adapter XYZ" and I need that value as well to reference for other stats on interface. How do I get that/them with python and snmp?










share|improve this question















migrated from unix.stackexchange.com Dec 4 at 14:02


This question came from our site for users of Linux, FreeBSD and other Un*x-like operating systems.














  • Have you examined the returned objects to see if more information isn't being returned? All you're seeing is a string representation
    – TheIncorrigible1
    Dec 4 at 14:11











  • I have tried with little luck. I imagine I look at "oid"? It appears to be a "<class 'netsnmp.client.VarList'>". I am really needing ID matched to interface name.
    – Matt
    Dec 4 at 15:52













up vote
2
down vote

favorite









up vote
2
down vote

favorite











I am trying to use snmpwalk to gain some info and stats on some interfaces. I use this:



import netsnmp

serv = "172.16.1.1"
snmp_pass = "private"

oid = netsnmp.VarList('IF-MIB::ifName','IF-MIB::ifDescr')
snmp_res = netsnmp.snmpwalk(oid, Version=2, DestHost=serv, Community=snmp_pass)
for x in snmp_res:
print "snmp_res:: ", x


All I get as answer is:



snmp_res:: lo
snmp_res:: EtherNet Adapter XYZ


The answer is correct but I want more info. When I do same thing from linux command with snmpwalk I get more info such as:



IF-MIB::ifDescr.1 = STRING: lo
IF-MIB::ifDescr.2 = STRING: EtherNet Adapter XYZ


The ID is 2 for "EtherNet Adapter XYZ" and I need that value as well to reference for other stats on interface. How do I get that/them with python and snmp?










share|improve this question















I am trying to use snmpwalk to gain some info and stats on some interfaces. I use this:



import netsnmp

serv = "172.16.1.1"
snmp_pass = "private"

oid = netsnmp.VarList('IF-MIB::ifName','IF-MIB::ifDescr')
snmp_res = netsnmp.snmpwalk(oid, Version=2, DestHost=serv, Community=snmp_pass)
for x in snmp_res:
print "snmp_res:: ", x


All I get as answer is:



snmp_res:: lo
snmp_res:: EtherNet Adapter XYZ


The answer is correct but I want more info. When I do same thing from linux command with snmpwalk I get more info such as:



IF-MIB::ifDescr.1 = STRING: lo
IF-MIB::ifDescr.2 = STRING: EtherNet Adapter XYZ


The ID is 2 for "EtherNet Adapter XYZ" and I need that value as well to reference for other stats on interface. How do I get that/them with python and snmp?







python linux snmp net-snmp






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 4 at 19:19









Benyamin Jafari

2,56531833




2,56531833










asked Dec 3 at 23:01









Matt

266




266




migrated from unix.stackexchange.com Dec 4 at 14:02


This question came from our site for users of Linux, FreeBSD and other Un*x-like operating systems.






migrated from unix.stackexchange.com Dec 4 at 14:02


This question came from our site for users of Linux, FreeBSD and other Un*x-like operating systems.













  • Have you examined the returned objects to see if more information isn't being returned? All you're seeing is a string representation
    – TheIncorrigible1
    Dec 4 at 14:11











  • I have tried with little luck. I imagine I look at "oid"? It appears to be a "<class 'netsnmp.client.VarList'>". I am really needing ID matched to interface name.
    – Matt
    Dec 4 at 15:52

















  • Have you examined the returned objects to see if more information isn't being returned? All you're seeing is a string representation
    – TheIncorrigible1
    Dec 4 at 14:11











  • I have tried with little luck. I imagine I look at "oid"? It appears to be a "<class 'netsnmp.client.VarList'>". I am really needing ID matched to interface name.
    – Matt
    Dec 4 at 15:52
















Have you examined the returned objects to see if more information isn't being returned? All you're seeing is a string representation
– TheIncorrigible1
Dec 4 at 14:11





Have you examined the returned objects to see if more information isn't being returned? All you're seeing is a string representation
– TheIncorrigible1
Dec 4 at 14:11













I have tried with little luck. I imagine I look at "oid"? It appears to be a "<class 'netsnmp.client.VarList'>". I am really needing ID matched to interface name.
– Matt
Dec 4 at 15:52





I have tried with little luck. I imagine I look at "oid"? It appears to be a "<class 'netsnmp.client.VarList'>". I am really needing ID matched to interface name.
– Matt
Dec 4 at 15:52













1 Answer
1






active

oldest

votes

















up vote
1
down vote













Straight out of the documentation:




snmpwalk(<Varbind/VarList>, <Session args>))



Takes args of netsnmp.Session preceded by a Varbind or
VarList from which the 'walk' operation will start.
Returns a tuple of values retrieved from the MIB below
the Varbind passed in. If a VarList is passed in it
will be updated to contain a complete set of VarBinds
created for the results of the walk.
It is not
recommended to pass in just a Varbind since you loose
the ability to examine the returned OIDs. But, if only
a Varbind is passed in it will be returned unaltered.



Note that only one varbind should be contained in the
VarList passed in. The code is structured to maybe
handle this is the the future, but right now walking
multiple trees at once is not yet supported and will
produce insufficient results.




You're already passing a VarList, so you already have what you need. You just need to examine the results properly.



The tests have an example:



vars = netsnmp.VarList(netsnmp.Varbind('system'))

vals = sess.walk(vars)
print "v1 sess.walk result: ", vals, "n"

for var in vars:
print " ",var.tag, var.iid, "=", var.val, '(',var.type,')'


The key is that the input variable is modified to give you what you need. The return value is not of much value (lol) to you.



Putting this all together it looks like you want the following:



import netsnmp

serv = "172.16.1.1"
snmp_pass = "private"

oid = netsnmp.VarList('IF-MIB::ifName','IF-MIB::ifDescr')
snmp_res = netsnmp.snmpwalk(oid, Version=2, DestHost=serv, Community=snmp_pass)
for x in oid:
print "snmp_res:: ", x.iid, " = ", x.val


(Disclaimer: can't test; adapt as needed)



There's enough information about VarBind and VarList in that documentation to figure out the best stuff to get out of x.



x.iid is the instance identifier, though, so that should give you the 1 and 2 that you're after. Don't forget to examine x.tag as well, though, which will be either IF-MIB::ifName or IF-MIB::ifDescr (or something equivalent; you'd have to experiment).






share|improve this answer






















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53614670%2fpython-netsnmp-and-snmpwalk%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote













    Straight out of the documentation:




    snmpwalk(<Varbind/VarList>, <Session args>))



    Takes args of netsnmp.Session preceded by a Varbind or
    VarList from which the 'walk' operation will start.
    Returns a tuple of values retrieved from the MIB below
    the Varbind passed in. If a VarList is passed in it
    will be updated to contain a complete set of VarBinds
    created for the results of the walk.
    It is not
    recommended to pass in just a Varbind since you loose
    the ability to examine the returned OIDs. But, if only
    a Varbind is passed in it will be returned unaltered.



    Note that only one varbind should be contained in the
    VarList passed in. The code is structured to maybe
    handle this is the the future, but right now walking
    multiple trees at once is not yet supported and will
    produce insufficient results.




    You're already passing a VarList, so you already have what you need. You just need to examine the results properly.



    The tests have an example:



    vars = netsnmp.VarList(netsnmp.Varbind('system'))

    vals = sess.walk(vars)
    print "v1 sess.walk result: ", vals, "n"

    for var in vars:
    print " ",var.tag, var.iid, "=", var.val, '(',var.type,')'


    The key is that the input variable is modified to give you what you need. The return value is not of much value (lol) to you.



    Putting this all together it looks like you want the following:



    import netsnmp

    serv = "172.16.1.1"
    snmp_pass = "private"

    oid = netsnmp.VarList('IF-MIB::ifName','IF-MIB::ifDescr')
    snmp_res = netsnmp.snmpwalk(oid, Version=2, DestHost=serv, Community=snmp_pass)
    for x in oid:
    print "snmp_res:: ", x.iid, " = ", x.val


    (Disclaimer: can't test; adapt as needed)



    There's enough information about VarBind and VarList in that documentation to figure out the best stuff to get out of x.



    x.iid is the instance identifier, though, so that should give you the 1 and 2 that you're after. Don't forget to examine x.tag as well, though, which will be either IF-MIB::ifName or IF-MIB::ifDescr (or something equivalent; you'd have to experiment).






    share|improve this answer


























      up vote
      1
      down vote













      Straight out of the documentation:




      snmpwalk(<Varbind/VarList>, <Session args>))



      Takes args of netsnmp.Session preceded by a Varbind or
      VarList from which the 'walk' operation will start.
      Returns a tuple of values retrieved from the MIB below
      the Varbind passed in. If a VarList is passed in it
      will be updated to contain a complete set of VarBinds
      created for the results of the walk.
      It is not
      recommended to pass in just a Varbind since you loose
      the ability to examine the returned OIDs. But, if only
      a Varbind is passed in it will be returned unaltered.



      Note that only one varbind should be contained in the
      VarList passed in. The code is structured to maybe
      handle this is the the future, but right now walking
      multiple trees at once is not yet supported and will
      produce insufficient results.




      You're already passing a VarList, so you already have what you need. You just need to examine the results properly.



      The tests have an example:



      vars = netsnmp.VarList(netsnmp.Varbind('system'))

      vals = sess.walk(vars)
      print "v1 sess.walk result: ", vals, "n"

      for var in vars:
      print " ",var.tag, var.iid, "=", var.val, '(',var.type,')'


      The key is that the input variable is modified to give you what you need. The return value is not of much value (lol) to you.



      Putting this all together it looks like you want the following:



      import netsnmp

      serv = "172.16.1.1"
      snmp_pass = "private"

      oid = netsnmp.VarList('IF-MIB::ifName','IF-MIB::ifDescr')
      snmp_res = netsnmp.snmpwalk(oid, Version=2, DestHost=serv, Community=snmp_pass)
      for x in oid:
      print "snmp_res:: ", x.iid, " = ", x.val


      (Disclaimer: can't test; adapt as needed)



      There's enough information about VarBind and VarList in that documentation to figure out the best stuff to get out of x.



      x.iid is the instance identifier, though, so that should give you the 1 and 2 that you're after. Don't forget to examine x.tag as well, though, which will be either IF-MIB::ifName or IF-MIB::ifDescr (or something equivalent; you'd have to experiment).






      share|improve this answer
























        up vote
        1
        down vote










        up vote
        1
        down vote









        Straight out of the documentation:




        snmpwalk(<Varbind/VarList>, <Session args>))



        Takes args of netsnmp.Session preceded by a Varbind or
        VarList from which the 'walk' operation will start.
        Returns a tuple of values retrieved from the MIB below
        the Varbind passed in. If a VarList is passed in it
        will be updated to contain a complete set of VarBinds
        created for the results of the walk.
        It is not
        recommended to pass in just a Varbind since you loose
        the ability to examine the returned OIDs. But, if only
        a Varbind is passed in it will be returned unaltered.



        Note that only one varbind should be contained in the
        VarList passed in. The code is structured to maybe
        handle this is the the future, but right now walking
        multiple trees at once is not yet supported and will
        produce insufficient results.




        You're already passing a VarList, so you already have what you need. You just need to examine the results properly.



        The tests have an example:



        vars = netsnmp.VarList(netsnmp.Varbind('system'))

        vals = sess.walk(vars)
        print "v1 sess.walk result: ", vals, "n"

        for var in vars:
        print " ",var.tag, var.iid, "=", var.val, '(',var.type,')'


        The key is that the input variable is modified to give you what you need. The return value is not of much value (lol) to you.



        Putting this all together it looks like you want the following:



        import netsnmp

        serv = "172.16.1.1"
        snmp_pass = "private"

        oid = netsnmp.VarList('IF-MIB::ifName','IF-MIB::ifDescr')
        snmp_res = netsnmp.snmpwalk(oid, Version=2, DestHost=serv, Community=snmp_pass)
        for x in oid:
        print "snmp_res:: ", x.iid, " = ", x.val


        (Disclaimer: can't test; adapt as needed)



        There's enough information about VarBind and VarList in that documentation to figure out the best stuff to get out of x.



        x.iid is the instance identifier, though, so that should give you the 1 and 2 that you're after. Don't forget to examine x.tag as well, though, which will be either IF-MIB::ifName or IF-MIB::ifDescr (or something equivalent; you'd have to experiment).






        share|improve this answer














        Straight out of the documentation:




        snmpwalk(<Varbind/VarList>, <Session args>))



        Takes args of netsnmp.Session preceded by a Varbind or
        VarList from which the 'walk' operation will start.
        Returns a tuple of values retrieved from the MIB below
        the Varbind passed in. If a VarList is passed in it
        will be updated to contain a complete set of VarBinds
        created for the results of the walk.
        It is not
        recommended to pass in just a Varbind since you loose
        the ability to examine the returned OIDs. But, if only
        a Varbind is passed in it will be returned unaltered.



        Note that only one varbind should be contained in the
        VarList passed in. The code is structured to maybe
        handle this is the the future, but right now walking
        multiple trees at once is not yet supported and will
        produce insufficient results.




        You're already passing a VarList, so you already have what you need. You just need to examine the results properly.



        The tests have an example:



        vars = netsnmp.VarList(netsnmp.Varbind('system'))

        vals = sess.walk(vars)
        print "v1 sess.walk result: ", vals, "n"

        for var in vars:
        print " ",var.tag, var.iid, "=", var.val, '(',var.type,')'


        The key is that the input variable is modified to give you what you need. The return value is not of much value (lol) to you.



        Putting this all together it looks like you want the following:



        import netsnmp

        serv = "172.16.1.1"
        snmp_pass = "private"

        oid = netsnmp.VarList('IF-MIB::ifName','IF-MIB::ifDescr')
        snmp_res = netsnmp.snmpwalk(oid, Version=2, DestHost=serv, Community=snmp_pass)
        for x in oid:
        print "snmp_res:: ", x.iid, " = ", x.val


        (Disclaimer: can't test; adapt as needed)



        There's enough information about VarBind and VarList in that documentation to figure out the best stuff to get out of x.



        x.iid is the instance identifier, though, so that should give you the 1 and 2 that you're after. Don't forget to examine x.tag as well, though, which will be either IF-MIB::ifName or IF-MIB::ifDescr (or something equivalent; you'd have to experiment).







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Dec 6 at 10:58

























        answered Dec 6 at 10:52









        Lightness Races in Orbit

        281k51453769




        281k51453769



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53614670%2fpython-netsnmp-and-snmpwalk%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown






            Popular posts from this blog

            Peggy Mitchell

            Palaiologos

            The Forum (Inglewood, California)