我正在尝试通过snmpv3发送陷阱,这是我第一次尝试:
#!/usr/bin/python
from pysnmp.hlapi import *
TARGET="localhost"
TARGET_PORT=162
COMMUNITY_STR="PASSWORD"
IDENTIFIER="1.3.6.1.2.1.xxx"
USER="trapadm"
KEY="PASSWORD"
# OID NODE : MESSAGE
values = { ".100.5": "LOL",
".100.6": "ROFL",
}
def notification(
NODE,
MESSAGE,
TARGET=TARGET,
TARGET_PORT=TARGET_PORT,
#COMMUNITY_STR=COMMUNITY_STR,
IDENTIFIER=IDENTIFIER
):
errorIndication, errorStatus, errorIndex, varBinds = next(
sendNotification(
SnmpEngine(),
UsmUserData(userName=USER, privKey=KEY, authKey=KEY
#authProtocol=usmHMACMD5AuthProtocol,
#privProtocol=usmDESPrivProtocol
#authProtocol=(1, 3, 6, 1, 6, 3, 10, 1, 1, 2),
#privProtocol= (1, 3, 6, 1, 6, 3, 10, 1, 2, 2)
),
#CommunityData(COMMUNITY_STR, mpModel=0),
UdpTransportTarget((TARGET, TARGET_PORT)),
ContextData(),
'trap',
NotificationType(
ObjectIdentity(IDENTIFIER)
).addVarBinds(
(IDENTIFIER+NODE, OctetString(MESSAGE)))
)
)
if errorIndication:
print(errorIndication)
CASE = True
def main():
for key in values:
if CASE is True:
notification(key, values[key])
if __name__ == '__main__':
main()我已经用下面的命令测试了我的陷阱接收器的功能(立即生效)
snmptrap -Ci -v 3 -a MD5 -A PASSWORD -x DES -X PASSWORD -l authPriv -u trapadm localhost 0 linkUp.0现在,使用上面的python脚本,我可以通过tcpdump看到它已被发送,但它没有出现在trapd日志文件中。我怀疑它以某种方式依赖于auth-/privProtocol。顺便提一下注释行(auth-/privProtocol)。也进行了测试。
这里有什么好主意吗?
发布于 2019-12-03 13:43:08
您的代码在我看来很好(here is工作示例)。
重要的一点是,使用SNMPv3陷阱时,您必须手动将陷阱发射器的SNMP ID值传递给陷阱接收器。这是由于陷阱的单向性质,这使得自动同步过程无法发生(例如,与SNMP命令不同)。
我猜想在snmptrap中发生这种情况是因为snmptrap和snmpd从一开始就具有相同的SNMP引擎ID值。
解决方案可以是将-e <snmp-engine-id>参数添加到snmptrap,并在snmpd端将其配置为context-snmp-engine-id to trapadm用户条目。
https://stackoverflow.com/questions/59138814
复制相似问题