Reputation:
I am new to python, so maybe a similar question has been asked and I didn't understand the answer as it pertains to my situation, and sorry if this is a dumb question.
I am passing this snmp function, either get or bulk, a list of ip addresses to loop through and then print the MIB information i'm requesting.
I've attempted to do this so many ways; different loops, list comprehension, using .join and .split to make them a comma separated string, using enumerate and looping over the indexes, using netaddr variables, etc. and usually end up with some sort of type error. I'm trying to pass each ip address as a string to the function, which is asking for a string. If I try to loop over the list directly with a 'for a in a:' loop, I end up with a similar error.
When I print the type for each string, it says it is a string. Why would this not work and what is the recommended method?
in this specific situation i'm getting the following errors:
if snmp bulk:
TYPEError: int () argument must be a string, a bytes-like object or a number, not 'ObjectType'
if snmp get:
TYPEError: 'int' object is not subscriptable
Code and results listed below:
from pysnmp.hlapi import *
from nmapPoller import a
def snmpquery(hostip):
snmp_iter = bulkCmd(SnmpEngine(),
CommunityData('public'),
UdpTransportTarget((hostip, 161)),
ContextData(),
1,
255,
ObjectType(ObjectIdentity('IF-MIB','ifOperStatus')),
lexicographicMode=True)
for errorIndication, errorStatus, errorIndex, varBinds in snmp_iter:
# Check for errors and print out results
if errorIndication:
print(errorIndication)
elif errorStatus:
print('%s at %s' % (errorStatus.prettyPrint(),
errorIndex and varBinds[int(errorIndex) - 1][0] or '?'))
else:
for varBind in varBinds:
print(' = '.join([x.prettyPrint() for x in varBind]))
print(a)
result: ['10.200.100.1', '10.200.100.8']
print(type(a))
result: <class 'list'>
for i, x in enumerate(a):
host = a[i]
str(host) this can be excluded I believe
print(host)
result: 10.200.100.1
print(type(host))
result: <class 'str'>
#snmpquery(host) <-- Error results from running function
User9876 fixed my issue by adding maxrepetitions, added to above.
Upvotes: 1
Views: 547
Reputation: 11102
According to these docs, the function you're calling is defined as:
pysnmp.hlapi.bulkCmd(snmpEngine, authData, transportTarget, contextData, nonRepeaters, maxRepetitions, *varBinds, **options)
maxRepetitions (int)
But for the maxRepetitions
parameter, you're passing an ObjectType()
instead of an int. I think the ObjectType()
should be one of the *varBinds
, I think you missed the maxRepetitions
parameter.
In your code:
snmp_iter = bulkCmd(SnmpEngine(), CommunityData('public'), UdpTransportTarget((hostip, 161)), ContextData(), 1,
I think you're missing the maxRepetitions
parameter that should go here...
ObjectType(ObjectIdentity('IF-MIB','ifOperStatus')), lexicographicMode=True)
Upvotes: 2