pySNMP: SNMPget example

Discussion in 'Python' started by WIWA, Jul 10, 2003.

  1. WIWA

    WIWA Guest

    I have recently installed pySNMP 3.3.2 and use Python 2.2.2. Thanks to
    Peter Hansen, I succeeded to install pySNMP properly. I'm not
    completely new to SNMP (I know the basics), but I'm new to Python and
    pysnmp. While experimenting, I find some strange things. When using
    pySNMP:

    1) I can type: 'import pysnmp' or 'from pysnmp import *'

    2) when using: 'from pysnmp import role' (found on
    http://pysnmp.sourceforge
    ..net/examples/2.x/snmpget.html), I get the message 'ImportError:
    cannot
    import name role'. The same applies for 'from pysnmp import session'
    or 'from pysnmp import v1' , but 'from pysnmp import proto' seems to
    work.

    3) A general question: how can I get a list of what I can type after
    the 'from
    pysnmp import ...'

    4) How can I use: 'from snmpget import snmpget'. It does not accept
    this.

    5) Anyone has a simple example for the following application: I have a
    cable
    modem (which has an SNMP agent inside). I want to make a script where
    I can
    do SNMPgets (and later SNMPSet and SNMPwalk).

    6) Where can I find a simple description on how to use 'snmpget'. I
    cannot find
    anything. I would like to write sth like snmpget(IP, OID)

    7) What is the difference between snmpget and getrequest in pysnmp?

    Any input is appreciated...

    Thanks in advance,

    Wim
    WIWA, Jul 10, 2003
    #1
    1. Advertising

  2. WIWA

    Ilya Etingof Guest

    > 2) when using: 'from pysnmp import role' (found on
    > http://pysnmp.sourceforge
    > .net/examples/2.x/snmpget.html), I get the message 'ImportError:


    You seems to use pysnmp 2.x API which differs from the latest 3.x branch
    (though, a compatibility layer exists in 3.x distribution). That's why
    I suggest you looking at the 3.x docs and examples at:

    http://pysnmp.sourceforge.net/docs/3.x/index.html

    > 3) A general question: how can I get a list of what I can type after
    > the 'from
    > pysnmp import ...'


    dir(<module>) may help but in this case I'd better see an example.

    > 4) How can I use: 'from snmpget import snmpget'. It does not accept
    > this.


    There is no such module as snmpget in pysnmp.

    > 5) Anyone has a simple example for the following application: I have a
    > cable
    > modem (which has an SNMP agent inside). I want to make a script where
    > I can
    > do SNMPgets (and later SNMPSet and SNMPwalk).


    Python 1.5.2 (#3, Aug 25 1999, 19:14:24) [GCC 2.8.1] on sunos5
    Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
    >>> from pysnmp.proto import v1
    >>> from pysnmp.proto.api import generic
    >>> from pysnmp.mapping.udp import role
    >>> req = v1.GetRequest()
    >>> req.apiGetPdu().apiSetVarBind([('1.3.6.1.2.1.1.1.0', None)])
    >>> tr = role.manager(('router-1.glas.net', 161))
    >>> (answer, src) = tr.send_and_receive(req.encode())
    >>> rsp = v1.GetResponse()
    >>> rsp.decode(answer)
    >>> vars = rsp.apiGetPdu().apiGetVarBind()
    >>> print vars

    [('.1.3.6.1.2.1.1.1.0', OctetString('Cisco Internetwork Operating System
    Software\015\012IOS (tm) 5400 Software(C5400-JS-M), Version 12.2(11.8b),
    MAINTENANCE INTERIM SOFTWARE\015\012 Copyright (c) 1986-2002 by cisco
    Systems, Inc.\015\012 Compiled Tue 30-Jul-02 19:02 by pwade'))]
    >>>


    > 7) What is the difference between snmpget and getrequest in pysnmp?


    The only difference is the SNMP request object (GetRequest vs GetNextRequest)
    you create when building SNMP message.

    -ilya
    Ilya Etingof, Jul 14, 2003
    #2
    1. Advertising

  3. WIWA

    Ilya Etingof Guest

    I'd suggest you to refer to "high-level" API documentation at
    http://pysnmp.sourceforge.net/docs/3.x/ for getting used to
    basic operations on SNMP objects (such as apiGetPdu(), apiSetVarBind()).

    Also, note, that pysnmp s/w (the third branch) has been closely aligned
    with the APIs introduced by SNMP RFCs, so reading these RFCs may be
    helpful too.

    If you got more specific questions, please, let me know.

    -ilya

    WIWA <> wrote:
    > Thanks Ilya,


    > This has been very helpful. I'm able to get data out of my 'device
    > under test'.


    > I must be honnest and say that I understand the sample code, but could
    > not write or produce it myself.


    > How do you know e.g that
    > req.apiGetPdu().apiSetVarBind([('1.3.6.1.2.1.1.1.0', None)]) can be
    > written? I've read through the documentation and could not find
    > anything similar. Of course, I could overlook it.


    > Isn't there a tutorial out there that summarizes pysnmpv3 and gives
    > examples of snmpget, snmpset, snmpwalk, etc...


    > Thanks in advance for helping me out.


    > Regards,


    > Wim






    > Ilya Etingof <> wrote in message news:<betnfr$dqj$>...
    >> > 2) when using: 'from pysnmp import role' (found on
    >> > http://pysnmp.sourceforge
    >> > .net/examples/2.x/snmpget.html), I get the message 'ImportError:

    >>
    >> You seems to use pysnmp 2.x API which differs from the latest 3.x branch
    >> (though, a compatibility layer exists in 3.x distribution). That's why
    >> I suggest you looking at the 3.x docs and examples at:
    >>
    >> http://pysnmp.sourceforge.net/docs/3.x/index.html
    >>
    >> > 3) A general question: how can I get a list of what I can type after
    >> > the 'from
    >> > pysnmp import ...'

    >>
    >> dir(<module>) may help but in this case I'd better see an example.
    >>
    >> > 4) How can I use: 'from snmpget import snmpget'. It does not accept
    >> > this.

    >>
    >> There is no such module as snmpget in pysnmp.
    >>
    >> > 5) Anyone has a simple example for the following application: I have a
    >> > cable
    >> > modem (which has an SNMP agent inside). I want to make a script where
    >> > I can
    >> > do SNMPgets (and later SNMPSet and SNMPwalk).

    >>
    >> Python 1.5.2 (#3, Aug 25 1999, 19:14:24) [GCC 2.8.1] on sunos5
    >> Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
    >> >>> from pysnmp.proto import v1
    >> >>> from pysnmp.proto.api import generic
    >> >>> from pysnmp.mapping.udp import role
    >> >>> req = v1.GetRequest()
    >> >>> req.apiGetPdu().apiSetVarBind([('1.3.6.1.2.1.1.1.0', None)])
    >> >>> tr = role.manager(('router-1.glas.net', 161))
    >> >>> (answer, src) = tr.send_and_receive(req.encode())
    >> >>> rsp = v1.GetResponse()
    >> >>> rsp.decode(answer)
    >> >>> vars = rsp.apiGetPdu().apiGetVarBind()
    >> >>> print vars

    >> [('.1.3.6.1.2.1.1.1.0', OctetString('Cisco Internetwork Operating System
    >> Software\015\012IOS (tm) 5400 Software(C5400-JS-M), Version 12.2(11.8b),
    >> MAINTENANCE INTERIM SOFTWARE\015\012 Copyright (c) 1986-2002 by cisco
    >> Systems, Inc.\015\012 Compiled Tue 30-Jul-02 19:02 by pwade'))]
    >> >>>

    >>
    >> > 7) What is the difference between snmpget and getrequest in pysnmp?

    >>
    >> The only difference is the SNMP request object (GetRequest vs GetNextRequest)
    >> you create when building SNMP message.
    >>
    >> -ilya
    Ilya Etingof, Jul 15, 2003
    #3
  4. WIWA

    Ilya Etingof Guest

    > 1) Instead of writing '1.3.6.1.2.1.69.1.3.1', I would also like to be
    > able to write 'docsDevSwServer'. Any idea how I can do that?


    The right way is to use a MIB parser (for labels-to-oids translation) which
    is not a part of pysnmp. Try looking at Python backend to libsmi. If you
    expect to query just a small and definite set of OIDs, an alternative would be
    to hardcode labels-to-oid mapping somewhere in your script.

    -ilya
    Ilya Etingof, Jul 16, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. WIWA

    pySNMP: SNMP walk

    WIWA, Aug 21, 2003, in forum: Python
    Replies:
    0
    Views:
    2,078
  2. Yomanium Yoth Taripoät II

    [pysnmp]

    Yomanium Yoth Taripoät II, Dec 2, 2003, in forum: Python
    Replies:
    0
    Views:
    391
    Yomanium Yoth Taripoät II
    Dec 2, 2003
  3. Axel Scheepers

    pysnmp/shell

    Axel Scheepers, Jun 22, 2004, in forum: Python
    Replies:
    2
    Views:
    550
    Ilya Etingof
    Jun 25, 2004
  4. Axel Scheepers

    pysnmp/shell

    Axel Scheepers, Jun 22, 2004, in forum: Python
    Replies:
    0
    Views:
    372
    Axel Scheepers
    Jun 22, 2004
  5. Evan Platt

    can't execute @SNMPGET@ on os/x box?

    Evan Platt, Aug 17, 2006, in forum: Perl Misc
    Replies:
    1
    Views:
    103
    Sherm Pendley
    Aug 17, 2006
Loading...

Share This Page