SNMP トラップを送信するための単純な python アプリを作成しようとしています。既に 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)
次に、トラップをプライベート サブネットワークに送信するようにコードを変更する必要があります。
その IP アドレス、サブネット マスク、およびゲートウェイ IP があります。仮定しましょう:
IP: 20.40.34.14
サブネットマスク: 255.255.255.224
ゲートウェイ: 20.40.34.10
ntforg.UdpTransportTarget(...)
適切な議論を通じてこれを解決する方法はありますか?このクラス ( 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]