pySNMP: SNMPget example

W

WIWA

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
 
I

Ilya Etingof

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( said:
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
 
I

Ilya Etingof

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 said:
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.





Ilya Etingof said:
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( said:
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
 
I

Ilya Etingof

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top