lost and found ( for me ? )

Python : SNMP library PySNMP

[ PySNMP ]

SNMP library for Python
http://pysnmp.sourceforge.net/index.html

[ install pysnmp library ]

# python --version
Python 2.7.3

# apt-get install python-pysnmp-common


[ sample usage ]

just referred to http://pysnmp.sourceforge.net/examples/current/index.html

- snmp get
# snmpget -v2c -c public -ObentU 127.0.0.1 1.3.6.1.2.1.1.1.0 1.3.6.1.2.1.1.6.0
.1.3.6.1.2.1.1.1.0 = STRING: "Linux mint-1 3.5.0-23-generic #35-Ubuntu SMP Thu Jan 24 13:15:40 UTC 2013 x86_64"
.1.3.6.1.2.1.1.6.0 = STRING: "Sitting on the Dock of the Bay"


python script
# cat -n snmp-get.py
    1  #!/usr/bin/env python
    2
    3  from pysnmp.entity.rfc3413.oneliner import cmdgen
    4
    5  cmdGen = cmdgen.CommandGenerator()
    6
    7  # snmp get
    8  errorIndication, errorStatus, errorIndex, varBinds = cmdGen.getCmd(
    9      cmdgen.CommunityData('public'),
   10      cmdgen.UdpTransportTarget(('localhost', 161)),
   11      '1.3.6.1.2.1.1.1.0',
   12      '1.3.6.1.2.1.1.6.0'
   13  )
   14
   15  # Check for errors
   16  if errorIndication:
   17      print(errorIndication)
   18  else:
   19      if int(errorStatus) != 0:
   20          print('%s at %s' % (
   21              errorStatus.prettyPrint(),
   22              errorIndex and varBinds[int(errorIndex)-1] or '?'
   23              )
   24          )
   25  # success
   26      if int(errorStatus) == 0:
   27          for name, val in varBinds:
   28              print('%s = %s' % (name.prettyPrint(), val.prettyPrint()))

# ./snmp-get.py
1.3.6.1.2.1.1.1.0 = Linux mint-1 3.5.0-23-generic #35-Ubuntu SMP Thu Jan 24 13:15:40 UTC 2013 x86_64
1.3.6.1.2.1.1.6.0 = Sitting on the Dock of the Bay


- snmp bulkwalk
# snmpbulkwalk -c public -v2c localhost .1.3.6.1 -On -Cn0 -Cr25 | head -2
.1.3.6.1.2.1.1.1.0 = STRING: "Linux mint-1 3.5.0-23-generic #35-Ubuntu SMP Thu Jan 24 13:15:40 UTC 2013 x86_64"
.1.3.6.1.2.1.1.2.0 = OID: .1.3.6.1.4.1.8072.3.2.10


about -Cn , -Cr arguments
      -Cn<NUM>
              Set the non-repeaters field in the GETBULK PDUs.   This  speci‐
              fies  the number of supplied variables that should not be iter‐
              ated over.  The default is 0.


python script
# cat -n snmp-bulkwalk.py
    1  #!/usr/bin/env python
    2
    3  from pysnmp.entity.rfc3413.oneliner import cmdgen
    4
    5  cmdGen = cmdgen.CommandGenerator()
    6
    7  # snmp bulkwalk
    8  errorIndication, errorStatus, errorIndex, varBindTable = cmdGen.bulkCmd(
    9      cmdgen.CommunityData('public'),
   10      cmdgen.UdpTransportTarget(('localhost', 161)),
   11  # snmpbulkwalk -Cn0 -Cr25
   12      0, 25,
   13  # OID
   14      '1.3.6.1',
   15  )
   16
   17  # Check for errors
   18  if errorIndication:
   19      print(errorIndication)
   20  else:
   21      if int(errorStatus) != 0:
   22          print('%s at %s' % (
   23              errorStatus.prettyPrint(),
   24              errorIndex and varBindTable[-1][int(errorIndex)-1] or '?'
   25              )
   26          )
   27
   28  # success
   29      if int(errorStatus) == 0:
   30          for varBindTableRow in varBindTable:
   31                  for name, val in varBindTableRow:
   32                      print('%s = %s' % (name.prettyPrint(), val.prettyPrint()))


# ./snmp-bulkwalk.py  | head -2
1.3.6.1.2.1.1.1.0 = Linux mint-1 3.5.0-23-generic #35-Ubuntu SMP Thu Jan 24 13:15:40 UTC 2013 x86_64
1.3.6.1.2.1.1.2.0 = 1.3.6.1.4.1.8072.3.2.10

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.