我试图获取 snmp 陷阱,但出现 OSError: [WinError 10049] 错误,任何人都可以帮我解决这个问题,这是我的完整代码:
from pysnmp.entity import engine, config
from pysnmp.carrier.asyncore.dgram import udp
from pysnmp.entity.rfc3413 import ntfrcv
snmpEngine = engine.SnmpEngine()
TrapAgentAddress='10.17.92.2' #Trap listerner address
Port= 163 #trap listerner port
print("Agent is listening SNMP Trap on "+TrapAgentAddress+" , Port : " +str(Port))
print('--------------------------------------------------------------------------')
config.addTransport(
snmpEngine,
udp.domainName + (1,),
udp.UdpTransport().openServerMode((TrapAgentAddress, Port))
)
#Configure community here
config.addV1System(snmpEngine, 'my-area', 'public')
def cbFun(snmpEngine, stateReference, contextEngineId, contextName,
varBinds, cbCtx):
print("Received new Trap message")
for name, val in varBinds:
print('%s = %s' % (name.prettyPrint(), val.prettyPrint()))
ntfrcv.NotificationReceiver(snmpEngine, cbFun)
snmpEngine.transportDispatcher.jobStarted(1)
try:
snmpEngine.transportDispatcher.runDispatcher()
except:
snmpEngine.transportDispatcher.closeDispatcher()
raise
这是回溯错误:
Traceback (most recent call last):
File "C:\Users\oussama.belhadje.stg\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pysnmp\carrier\asyncore\dgram\base.py", line 55, in openServerMode
self.socket.bind(iface)
OSError: [WinError 10049] L’adresse demandée n’est pas valide dans son contexte
在处理上述异常的过程中,又发生了一个异常:
Traceback (most recent call last):
File "c:/Users/oussama.belhadje.stg/Desktop/pysnmp/snmp_trap_receiver.py", line 15, in <module>
udp.UdpTransport().openServerMode((TrapAgentAddress, Port))
File "C:\Users\oussama.belhadje.stg\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pysnmp\carrier\asyncore\dgram\base.py", line 57, in openServerMode
raise error.CarrierError('bind() for %s failed: %s' % (iface, sys.exc_info()[1],))
pysnmp.carrier.error.CarrierError: bind() for ('10.17.92.2', 163) failed: [WinError 10049] L’adresse demandée n’est pas valide dans
son contextecaused by <class 'OSError'>: [WinError 10049] L’adresse demandée n’est pas valide dans son contexte
通常小于1024的端口需要管理员权限才能监听。
因此,您必须在 Windows 上以管理员身份运行此 Python 代码。
顺便说一句,要监听 TRAP 和 NOTIFICATION,您应该绑定到端口 162,而不是 163。