Reputation: 9
I can send an snmptrap (net-snmp-utils) command from the shell, equivalent to -
snmptrap -v2c -c public myhostname.com '' .1.3.6.1.4.1.1234.7.0.1 .1.3.6.1.4.1.1234.7.1 s desktop 1.3.6.1.4.1.1234.7.2 s "TESTING" .1.3.6.1.4.1.1234.7.3 s CODE
I am trying to do the same via python3. The trap messages sent from the script are received and processed by the server/agent/manager. However the ones from python (code and output pasted below) are are not successfully reaching the server.
So far I've been using pysnmp
from pysnmp import debug
debug.setLogger(debug.Debug('msgproc'))
from pysnmp.hlapi.asyncore import *
next(sendNotification(
SnmpEngine(),
CommunityData('public', mpModel=1),
UdpTransportTarget(('myhostname.com', 162)),
ContextData(),
'trap',
NotificationType(
ObjectIdentity('1.3.6.1.6.3.1.1.5.2')
).addVarBinds(
( '1.3.6.1.6.3.1.1.4.3.0', '.1.3.6.1.4.1.1234.7.0.1'),
('.1.3.6.1.4.1.1234.7.1', OctetString('desktop')),
('1.3.6.1.4.1.1234.7.2', OctetString('TESTING')),
('.1.3.6.1.4.1.1234.7.3', OctetString('CODE'))
)
))
The output is
2019-05-03 12:16:02,881 pysnmp: StatusInformation: {'errorIndication': AccessAllowed()} 2019-05-03 12:16:02,884 pysnmp: StatusInformation: {'errorIndication': AccessAllowed()} 2019-05-03 12:16:02,886 pysnmp: StatusInformation: {'errorIndication': AccessAllowed()} 2019-05-03 12:16:02,891 pysnmp: StatusInformation: {'errorIndication': AccessAllowed()} 2019-05-03 12:16:02,899 pysnmp: prepareOutgoingMessage: using contextEngineId subtypeSpec , > encoding iso-885 9-1 payload [0x80004fb805c3c8cf48]> contextName b'' 2019-05-03 12:16:02,904 pysnmp: generateRequestMsg: Message:
version=1
community=public
data=PDUs:
snmpV2-trap=SNMPv2TrapPDU:
request-id=10163951
error-status=noError
error-index=0
variable-bindings=VarBindList:
VarBind:
name=1.3.6.1.2.1.1.3.0
=_BindValue:
value=ObjectSyntax:
application-wide=ApplicationSyntax:
timeticks-value=0
VarBind:
name=1.3.6.1.6.3.1.1.4.1.0
=_BindValue:
value=ObjectSyntax:
simple=SimpleSyntax:
objectID-value=1.3.6.1.6.3.1.1.5.2
VarBind:
name=1.3.6.1.6.3.1.1.4.3.0
=_BindValue:
value=ObjectSyntax:
simple=SimpleSyntax:
objectID-value=1.3.6.1.4.1.1234.7.0.1
VarBind:
name=1.3.6.1.4.1.1234.7.1
=_BindValue:
value=ObjectSyntax:
simple=SimpleSyntax:
string-value=desktop
VarBind:
name=1.3.6.1.4.1.1234.7.2
=_BindValue:
value=ObjectSyntax:
simple=SimpleSyntax:
string-value=TESTING
VarBind:
name=1.3.6.1.4.1.1234.7.3
=_BindValue:
value=ObjectSyntax:
simple=SimpleSyntax:
string-value=CODE
On adding 'io' to the debugger I get this at the end - is it a standard message or does it mean that the message was queued and did not leave?
2019-05-06 04:55:42,914 pysnmp: sendMessage: outgoingMessage queued (164 octets) 00000: 30 81 A1 02 01 01 04 06 70 75 62 6C 69 63 A7 81
Upvotes: 0
Views: 925
Reputation: 5555
Your message has not been sent because the main loop has not been started.
You should probably import pysnmp.hlapi
, as opposed to pysnmp.hlapi.asyncore
, because the former eventually defaults onto pysnmp.hlapi.asyncore.sync
, which runs main loop internally for just one query.
The pysnmp.hlapi.asyncore
module is asynchronous. That means that besides submitting a message you also need to run the main loop (snmpEngine.transportDispatcher.runDispatcher()
). You can find an example on asynchronous use, but it seems you need just sync version for now.
Upvotes: 0
Reputation: 71
I am not an expert on pysnmp but problem seems like coming from snmp settings. Did you make the arrangements for agent? please check http://www.net-snmp.org/docs/man/snmpd.conf.html for settings like version, rwusers, groups etc. If you want alternatives you can check: https://github.com/pief/python-netsnmpagent and https://github.com/hosthvo/pyagentx
netsnmpagent cant sent traps but you can add that spec by yourself.
I recommand pyagentx. Using the main agent is a bit difficult however you can create your agentx and extend it. But do not forget to make settings over snmpd.conf.
Upvotes: 0