PySNMP: load error: Bad OctetString initializer - python

I have the following example from the pysnmp page:
# GET Command Generator
from pysnmp.entity.rfc3413.oneliner import cmdgen
errorIndication, errorStatus, \
errorIndex, varBinds = cmdgen.CommandGenerator().getCmd(
# SNMP v1
# cmdgen.CommunityData('test-agent', 'public', 0),
# SNMP v2
cmdgen.CommunityData('test-agent', 'public'),
# SNMP v3
# cmdgen.UsmUserData('test-user', 'authkey1', 'privkey1'),
cmdgen.UdpTransportTarget(('localhost', 161)),
# Plain OID
(1,3,6,1,2,1,1,1,0),
# ((mib-name, mib-symbol), instance-id)
(('SNMPv2-MIB', 'sysDescr'), 0)
)
if errorIndication:
print errorIndication
else:
if errorStatus:
print '%s at %s\n' % (
errorStatus.prettyPrint(),
errorIndex and varBinds[int(errorIndex)-1] or '?'
)
else:
for name, val in varBinds:
print '%s = %s' % (name.prettyPrint(), val.prettyPrint())
I made sure that SNMP is running on my machine. I checked in the console with the following command:
snmpget -v2c -Cf -c public localhost 1.3.6.1.2.1.1.1.0
Which worked fine.
If i execute the python code above I get the following error:
SmiError: MIB module "pysnmp/smi/mibs/SNMP-COMMUNITY-MIB.py" load error: MIB module "pysnmp/smi/mibs/SNMP-FRAMEWORK-MIB.py" load error: Bad OctetString initializer '[128, 0, 79, 184, 5, 192, 168, 1, 50, 371, 210, 26, 162, 157]'
The numbers at the end change with each execution (seems to a timestamp or somehting like that).
I'm using python 2.7 and the most recent version of pySNMP (4.2.1).
Does anyone know what's the problem with this example code?

That is a bug in pysnmp 4.2.1 which has been fixed to the latest pysnmp release candidates (it looks like this bug more frequently occurs on Macs for some reason).
Here's a link to currently most recent rc:
http://sourceforge.net/projects/pysnmp/files/pysnmp/4.2.2/pysnmp-4.2.2rc5.tar.gz/download
-ilya

Related

How to get SNMP OID Values in Cisco Router with Authentication

Am writing a Sample code below for fetching Cisco Switch Information through SNMP for the python pysnmp module.
after executing below code am getting 'No SNMP response received before timeout'.
from pysnmp.entity.rfc3413.oneliner import cmdgen
import time
#SNMP agent address
SNMP_AGENT_HOST = 'IPADDRESS' # IP adderess
#SNMP default port
SNMP_PORT = 161
#Add SNMP agent community here
SNMP_COMMUNITY = 'public'
cmdGen = cmdgen.CommandGenerator()
errorIndication, errorStatus, errorIndex, varBinds = cmdGen.getCmd(
cmdgen.CommunityData(SNMP_COMMUNITY),
cmdgen.UdpTransportTarget((SNMP_AGENT_HOST, SNMP_PORT)),
'1.3.6.1.4.1.9.2.1.56.0', # Cisco Switch OID
'1.3.6.1.4.1.9.2.1.57.0' #
)
# Check for errors and print out results
if errorIndication:
print(errorIndication)
else:
if errorStatus:
print('%s at %s' % (
errorStatus.prettyPrint(),
errorIndex and varBinds[int(errorIndex)-1] or '?'
)
)
else:
for name, val in varBinds:
print('%s = %s' % (name.prettyPrint(), val.prettyPrint()))
I getting the result
No SNMP response received before timeout
I want to be fetch all OID related Information through the GET call.
Your quickest way of diagnosing this is to use an EXISTING utility that you know works, and try it against that IPADDRESS with the authentication that you think should work.
If that utility, eg. NET-SNMP snmpwalk or snmpgetnext, does not work, then your problem is likely not in your code, but your understanding of how to access the SNMP agent. Eg. are you certain the community string "public" works?
It is ALWAYS better to work from success, than to interpolate from failure.

Using PySNMP as Trap Receiver with own/vendor MIB

I try to use PySNMP to receive SNMPv3 Traps. I found this example code:
#!/usr/bin/env /usr/bin/python3
from pysnmp.entity import engine, config
from pysnmp.carrier.asyncore.dgram import udp
from pysnmp.entity.rfc3413 import ntfrcv
from pysnmp.proto.api import v2c
from pysnmp.smi.rfc1902 import ObjectIdentity
snmpEngine = engine.SnmpEngine()
# Transport setup
# UDP over IPv4
config.addTransport(
snmpEngine,
udp.domainName,
udp.UdpTransport().openServerMode(('0.0.0.0', 162)),
)
# SNMPv3/USM setup
config.addV3User(
snmpEngine, '<username>',
config.usmHMACMD5AuthProtocol, '<password>',
config.usmAesCfb128Protocol, '<password>',
securityEngineId=v2c.OctetString(hexValue='<engineid>')
)
def cbFun(snmpEngine, stateReference, contextEngineId, contextName,
varBinds, cbCtx):
print('Notification from ContextEngineId "%s", ContextName "%s"' (contextEngineId.prettyPrint(), contextName.prettyPrint()))
for name, val in varBinds:
print('%s = %s' % (name.prettyPrint(), val.prettyPrint()))
# Register SNMP Application at the SNMP engine
ntfrcv.NotificationReceiver(snmpEngine, cbFun)
snmpEngine.transportDispatcher.jobStarted(1) # this job would never finish
# Run I/O dispatcher which would receive queries and send confirmations
try:
snmpEngine.transportDispatcher.runDispatcher()
except:
snmpEngine.transportDispatcher.closeDispatcher()
raise
This code works for me, but i get the raw Traps. I have an vendor specific MIB File I want to use. But I can't find any documentation how to bind the mib to the snmpEngine. The examples using MIBs from the PySNMP documentation show only the usage for SNMP GET operations and are not applicable here.
Has someone tried this before and can help me?
Thanks!
If your goal is to resolve raw variable-bindings you receive into human-friendly form, then you need to process those variable-bindings through the MIB browser object.
You are right, that's exactly the same operation that command generator frequently performs in the examples.
from pysnmp.smi import builder, view, compiler, rfc1902
# Assemble MIB browser
mibBuilder = builder.MibBuilder()
mibViewController = view.MibViewController(mibBuilder)
compiler.addMibCompiler(
mibBuilder, sources=['file:///usr/share/snmp/mibs',
'http://mibs.snmplabs.com/asn1/#mib#'])
# Pre-load MIB modules that define objects we receive in TRAPs
mibBuilder.loadModules('SNMPv2-MIB', 'SNMP-COMMUNITY-MIB')
# This is what we would get in a TRAP PDU
varBinds = [
('1.3.6.1.2.1.1.3.0', 12345),
('1.3.6.1.6.3.1.1.4.1.0', '1.3.6.1.6.3.1.1.5.2'),
('1.3.6.1.6.3.18.1.3.0', '0.0.0.0'),
('1.3.6.1.6.3.18.1.4.0', ''),
('1.3.6.1.6.3.1.1.4.3.0', '1.3.6.1.4.1.20408.4.1.1.2'),
('1.3.6.1.2.1.1.1.0', 'my system')
]
# Pass raw var-binds through MIB browser
varBinds = [
rfc1902.ObjectType(rfc1902.ObjectIdentity(x[0]), x[1]).resolveWithMib(mibViewController)
for x in varBinds
]
for varBind in varBinds:
print(varBind.prettyPrint())

pysnmp error on specific query

I have been trying to implement code that loads my device MIBS and walks through all the OIDS. In this one case when I try to load the OID for snmp 1.3.6.1.2.1.11 smi throws an exception when trying to load a specific OID. The previous OID works successfully: '.1.3.6.1.2.1.11.29.0' but this one generates the error message '.1.3.6.1.2.1.11.30.0'
The exception is:
File "/opt/anaconda/lib/python2.7/site-packages/pysnmp/smi/rfc1902.py", line 859, in resolveWithMib
raise SmiError('MIB object %r having type %r failed to cast value %r: %s' % (self.args[0].prettyPrint(), self.__args[0].getMibNode().getSyntax().__class.name, self.__args[1], sys.exc_info()[1]))
;SmiError: MIB object 'SNMPv2-MIB::snmpEnableAuthenTraps.0' having type 'Integer32' failed to cast value Integer32(808466736): ConstraintsIntersection(ConstraintsIntersection(ConstraintsIntersection(), ValueRangeConstraint(-2147483648, 2147483647)), SingleValueConstraint(1, 2)) failed at: "SingleValueConstraint(1, 2) failed at: "808466736"" at Integer32
Here is sample code that demonstrates the error. You will need to modify the DEVICE_IP. It is assuming that you are running SNMP v1 and against community 'public. It is running pysnmp version 4.3.2
from pysnmp.entity.rfc3413.oneliner import cmdgen
from pysnmp.smi.rfc1902 import ObjectIdentity
DEVICE_IP = 'localhost'
def get_oid(oid):
"""
Requires a valid oid as input and retrieves the given OID
"""
snmp_target = (DEVICE_IP, 161)
cmdGen = cmdgen.CommandGenerator()
result = None
errorIndication, errorStatus, errorIndex, varBindTable = cmdGen.nextCmd(
cmdgen.CommunityData('public', mpModel=0),
cmdgen.UdpTransportTarget(snmp_target),
ObjectIdentity(oid, last=True),
lexicographicMode=False
)
if errorIndication:
print(errorIndication)
else:
for varBindTableRow in varBindTable:
for name, val in varBindTableRow:
try:
result = str(val)
except:
raise
return result
# Does not Throw Error
print get_oid('.1.3.6.1.2.1.11.29.0')
# Throws Error
print get_oid('.1.3.6.1.2.1.11.30.0')
Your SNMP agent responded with 1.3.6.1.2.1.11.30.0=808466736 while OID 1.3.6.1.2.1.11.30.0 identifies MIB object snmpEnableAuthenTraps of type INTEGER with only two values permitted: 1 and 2.
Here is formal definition from SNMPv2-MIB:
snmpEnableAuthenTraps OBJECT-TYPE
SYNTAX INTEGER { enabled(1), disabled(2) }
...
So this time pysnmp seems to do the right thing - it shields you from the value that makes no sense. Root cause of this problem is the SNMP agent that sends malformed values for MIB objects.

What is 'my-creds', 'my-area', and 'my-router' in these Python PySNMP codes?

I am new in using PySNMP module in Python. According to this user manual and this manual, the following python scripts perform similar things like the net-snmp command:
net-snmp v1 command:
snmpget -v1 -c public -ObentU 195.218.195.228 1.3.6.1.2.1.1.1.0
Python v1 script:
from twisted.internet import reactor, defer
from pysnmp.entity import engine, config
from pysnmp.entity.rfc3413.twisted import cmdgen
from pysnmp.carrier.twisted import dispatch
from pysnmp.carrier.twisted.dgram import udp
# Create SNMP engine instance
snmpEngine = engine.SnmpEngine()
# Instantiate and register Twisted dispatcher at SNMP engine
snmpEngine.registerTransportDispatcher(dispatch.TwistedDispatcher())
#
# SNMPv1 setup
#
# SecurityName <-> CommunityName mapping
config.addV1System(snmpEngine, 'my-area', 'public')
# Specify security settings per SecurityName (SNMPv1 - 0, SNMPv2c - 1)
config.addTargetParams(snmpEngine, 'my-creds', 'my-area', 'noAuthNoPriv', 0)
#
# Setup transport endpoint and bind it with security settings yielding
# a target name
#
# UDP/IPv4
config.addSocketTransport(
snmpEngine,
udp.domainName,
udp.UdpTwistedTransport().openClientMode()
)
config.addTargetAddr(
snmpEngine, 'my-router',
udp.domainName, ('195.218.195.228', 161),
'my-creds'
)
# Error/response receiver
def cbFun(cbCtx):
(errorIndication, errorStatus, errorIndex, varBinds) = cbCtx
if errorIndication:
print(errorIndication)
# SNMPv1 response may contain noSuchName error *and* SNMPv2c exception,
# so we ignore noSuchName error here
elif errorStatus and errorStatus != 2:
print('%s at %s' % (
errorStatus.prettyPrint(),
errorIndex and varBinds[int(errorIndex)-1][0] or '?'
)
)
else:
for oid, val in varBinds:
print('%s = %s' % (oid.prettyPrint(), val.prettyPrint()))
reactor.stop()
# Prepare request to be sent yielding Twisted deferred object
df = cmdgen.GetCommandGenerator().sendReq(
snmpEngine,
'my-router',
( ('1.3.6.1.2.1.1.1.0', None), ('1.3.6.1.2.1.1.2.0', None) ),
)
# Register error/response receiver function at deferred
df.addCallback(cbFun)
# Run Twisted main loop
reactor.run()
net-snmp v3 command:
snmpget -v3 -l authPriv -u usr-sha-aes -a SHA -A authkey1 -x AES -X privkey1 -ObentU 195.218.195.228:161 1.3.6.1.2.1.1.1.0
Python v3 script:
from twisted.internet import reactor, defer
from pysnmp.entity import engine, config
from pysnmp.entity.rfc3413.twisted import cmdgen
from pysnmp.carrier.twisted import dispatch
from pysnmp.carrier.twisted.dgram import udp
# Create SNMP engine instance
snmpEngine = engine.SnmpEngine()
# Instantiate and register Twisted dispatcher at SNMP engine
snmpEngine.registerTransportDispatcher(dispatch.TwistedDispatcher())
#
# SNMPv3/USM setup
#
# user: usr-sha-aes, auth: SHA, priv AES
config.addV3User(
snmpEngine, 'usr-sha-aes',
config.usmHMACSHAAuthProtocol, 'authkey1',
config.usmAesCfb128Protocol, 'privkey1'
)
config.addTargetParams(snmpEngine, 'my-creds', 'usr-sha-aes', 'authPriv')
#
# Setup transport endpoint and bind it with security settings yielding
# a target name
#
# UDP/IPv4
config.addSocketTransport(
snmpEngine,
udp.domainName,
udp.UdpTwistedTransport().openClientMode()
)
config.addTargetAddr(
snmpEngine, 'my-router',
udp.domainName, ('195.218.195.228', 161),
'my-creds'
)
# Error/response receiver
def cbFun(cbCtx):
(errorIndication, errorStatus, errorIndex, varBinds) = cbCtx
if errorIndication:
print(errorIndication)
elif errorStatus:
print('%s at %s' % (
errorStatus.prettyPrint(),
errorIndex and varBinds[int(errorIndex)-1][0] or '?'
)
)
else:
for oid, val in varBinds:
print('%s = %s' % (oid.prettyPrint(), val.prettyPrint()))
reactor.stop()
# Prepare request to be sent yielding Twisted deferred object
df = cmdgen.GetCommandGenerator().sendReq(
snmpEngine,
'my-router',
( ('1.3.6.1.2.1.1.1.0', None), ),
)
# Register error/response receiver function at deferred
df.addCallback(cbFun)
# Run Twisted main loop
reactor.run()
In the above net-snmp commands, you can see that there is no 'my-creds', 'my-area', and 'my-router' parameters. However, these parameters are used in the Python scripts.
May I know what is 'my-creds', 'my-area', and 'my-router' in these Python PySNMP scripts?
In SNMP (not just pysnmp), system configuration is scattered across several 'SNMP tables'. They are defined in their respective MIBs and their rows can be logically linked one with the other by columnar names.
In the end you can refer to all SNMP configuration details for a particular SNMP peer via a single ID. Also, you can re-use common parts of SNMP configuration for multiple distinct peers.
In most pysnmp scripts you can spot the following tables and their relations:
securityName + securityLevel + snmpMessageProcessingModel (3) -> snmpTargetParameters (see SNMP-TARGET-MIB::snmpTargetParams)
securityName + communityName + snmpMessageVersion (1|2c) -> snmpTargetParameters (see SNMP-COMMUNITY-MIB::snmpCommunity)
snmpTargetParameters + targetAddress + timeout + retries -> snmpTarget (see SNMP-TARGET-MIB::snmpTargetAddr)
So snmpTarget (e.g. 'my-router') is the top-level ID that can be referred to when requesting SNMP application (like Command Generator) to send SNMP request to specific peer via specific SNMP version and with specific credentials.
One of the features of this configuration model (for at least for SNMP Agent situation) is that it can be managed remotely through SNMP.

Error Running Gearman for Windows 7

Hi I installed perl's gearman package in windows 7 and written simple client and worker just to connect to gearman server as follows :
Client.pl
use Gearman::Client;
my $client = Gearman::Client->new;
$client->job_servers('127.0.0.1:7003');
and Worker.pl
use Gearman::Worker;
my $worker = Gearman::Worker->new;
$worker->job_servers('127.0.0.1:7003');
$worker->work while 1;
but when i runs worker.pl, command prompt for gearmand gives error as
Error accepting incoming connection: Bad file descriptor
I have also tried with python worker and clients ( which works correctly under cygwin and with gearman 0.14) for reversing string but it shows the same error.
My Python Modules are :
Client.py
import gearman
def check_request_status(job_request):
if job_request.complete:
print "Job %s finished! Result: %s - %s" % (job_request.job.unique, job_request.state, job_request.result)
elif job_request.timed_out:
print "Job %s timed out!" % job_request.unique
elif job_request.state == JOB_UNKNOWN:
print "Job %s connection failed!" % job_request.unique
gm_client = gearman.GearmanClient(['127.0.0.1:7003'])
completed_job_request = gm_client.submit_job("reverse", "Hello World!")
check_request_status(completed_job_request)
and worker.py
import gearman
gm_worker = gearman.GearmanWorker(['127.0.0.1:7003'])
def task_listener_reverse(gearman_worker, gearman_job):
print 'Reversing string: ' + gearman_job.data
return gearman_job.data[::-1]
# gm_worker.set_client_id is optional
gm_worker.set_client_id('python-worker')
gm_worker.register_task('reverse', task_listener_reverse)
# Enter our work loop and call gm_worker.after_poll() after each time we timeout/see socket activity
gm_worker.work()
When i run Client.pl gearmand command prompt gives error as follows:
Error: Can't locate object method "CMD_" via package "Gearman::Server::Client" at C:/Perl/site/lib/Gearman/Server/Client.pm line 505.
Can anybody please resolve this issue?

Categories

Resources