简体   繁体   English

如何在python中将SNMP通知发送到专用子网?

[英]How to send SNMP notification to private subnet in python?

I'm trying to write simple python app to send SNMP traps. 我正在尝试编写简单的python应用程序以发送SNMP陷阱。 I've already wrote MIB table and sending traps to localhost works fine. 我已经写了MIB表并将陷阱发送到localhost可以正常工作。

from pysnmp.entity.rfc3413.oneliner import ntforg

ntfOrg = ntforg.NotificationOriginator()

errorIndication = ntfOrg.sendNotification(
    ntforg.CommunityData('public'),
    ntforg.UdpTransportTarget(('localhost', 162)),
    'trap',
    ntforg.MibVariable('MY-MIB', 'my_trap'),
    ( ntforg.MibVariable('MY-MIB', 'my_trap_var'), 0xAABBCCDD )
)

if errorIndication:
    print('Notification not sent: %s' % errorIndocation)

Now I need to modify code to send traps to private subnetwork. 现在,我需要修改代码以将陷阱发送到专用子网。

I have its IP address, subnet mask and gateway IP. 我有它的IP地址,子网掩码和网关IP。 Let assume: 假设:

IP: 20.40.34.14 IP:20.40.34.14

Subnet Mask: 255.255.255.224 子网掩码:255.255.255.224

Gateway: 20.40.34.10 网关:20.40.34.10

Is there any way to solve this through proper ntforg.UdpTransportTarget(...) arguments? 有没有办法通过适当的ntforg.UdpTransportTarget(...)参数来解决此问题? I looked up source code for this class ( target.py ) and internally it uses: 我查找了此类的源代码( target.py ),并且在内部使用了:

socket.getaddrinfo(transportAddr[0], # localhost in example
                   transportAddr[1], # 162 in example
                   socket.AF_INET,
                   socket.SOCK_DGRAM,
                   socket.IPPROTO_UDP)[0][4][:2]

This is not SNMP specific. 这不是特定于SNMP的。 Probably more relevant to IP routing. 可能与IP路由更相关。

As you get IP connectivity between hosts, just put 20.40.34.14 into UdpTransportTarget. 当您获得主机之间的IP连接时,只需将20.40.34.14放入UdpTransportTarget。 That should work alright for as long as UDP/162 packets could reach your Notification Receiver host. 只要UDP / 162数据包可以到达Notification Receiver主机,就可以正常工作。

There's also an option of sending broadcast TRAPs. 还可以选择发送广播的TRAP。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM