Home > other >  Pysnmp can receive to trap, locally deployed to the server could not received, what's the matte
Pysnmp can receive to trap, locally deployed to the server could not received, what's the matte

Time:09-26

Code 1: in the local test can receive, don't get the cloud host
` ` `
The from pysnmp. Entity import engine, config
The from pysnmp. Carrier. Asyncio. Dgram import udp
The from pysnmp. Entity. Rfc3413 import NTFRCV
The import asyncio
# requires Python 3.4 and higher version!
# Get the event loop for this thread
Loop=asyncio. Get_event_loop ()

# the Create SNMP engine with autogenernated engineID and pre - bound
# to socket transport dispatcher
SnmpEngine=engine. SnmpEngine ()

# Transport setup

# UDP over IPv4, first listening interface/port
The config. AddTransport (
SnmpEngine,
Udp. DomainName + (1),
Udp. UdpTransport (.) openServerMode ((162) '192.168.6.1')
)

# UDP over IPv4, second listening interface/port
# config. AddTransport (
# snmpEngine,
# udp domainName + (2),
# udp. UdpTransport (.) openServerMode ((2162) '127.0.0.1')
#)
The config. AddTransport (
SnmpEngine,
Udp domainName + (2),
Udp. UdpTransport (.) openServerMode ((2162) 'localhost')
)
The config. AddTransport (
SnmpEngine,
Udp domainName + (3),
Udp. UdpTransport (.) openServerMode ((162) '106.13.174.184')
)

2 c # SNMPv1/setup

# SecurityName & lt; -> CommunityName mapping
Config. AddV1System (snmpEngine, 'my - area', 'public')


# the Callback function for identifiers notifications
# noinspection PyUnusedLocal
Def cbFun (snmpEngine,
StateReference,
ContextEngineId contextName,
VarBinds,
CbCtx) :
TransportDomain, transportAddress=snmpEngine. MsgAndPduDsp. GetTransportInfo (stateReference)
SNMP Engine print (' Notification from % s, % s, the Context % s' % (transportAddress,
ContextEngineId. PrettyPrint (),
ContextName. PrettyPrint ()))
For the name, val in varBinds:
Print (' % s'=% s % (name) prettyPrint (), val. PrettyPrint ()))


SNMP Application # Register at the SNMP engine
NTFRCV. NotificationReceiver (snmpEngine cbFun)

# Run asyncio main loop
Loop. Run_forever ()
` ` `
Code 2: local, deployment to the cloud host can receive the trap, how without password, not afraid of being attacked, strange
` ` `
The from pysnmp. Carrier. The asyncore. Dispatch the import AsyncoreDispatcher
The from pysnmp. Carrier. The asyncore. Dgram import udp, udp6, Unix
The from pyasn1. Codec. Ber import decoder
The from pysnmp. Proto import API

# can only receive local?
#
# noinspection PyUnusedLocal
Def cbFun (transportDispatcher, transportDomain transportAddress, wholeMsg) :
While wholeMsg:
MsgVer=int (API. DecodeMessageVersion (wholeMsg))
If msgVer in API. ProtoModules:
PMod=API. ProtoModules [msgVer]
The else:
Print (' Unsupported SNMP version % s' % msgVer)
Return
ReqMsg, wholeMsg=decoder. Decode (
WholeMsg asn1Spec=pMod), Message (),
)
Print (' Notification message from % s: % s: '% (
TransportDomain, transportAddress
)
)
ReqPDU=pMod. ApiMessage. GetPDU (reqMsg)
If reqPDU. IsSameTypeWith (pMod TrapPDU ()) :
If msgVer==API. ProtoVersion1:
Print (' Enterprise: % s' % (pMod. ApiTrapPDU. GetEnterprise (reqPDU). PrettyPrint ()))
Print (' Agent Address: % s' % (pMod. ApiTrapPDU. GetAgentAddr (reqPDU). PrettyPrint ()))
Print (' Generic Trap: % s' % (pMod. ApiTrapPDU. GetGenericTrap (reqPDU). PrettyPrint ()))
Print (' Specific Trap: % s' % (pMod. ApiTrapPDU. GetSpecificTrap (reqPDU). PrettyPrint ()))
Uptime: print (' % s' % (pMod. ApiTrapPDU. GetTimeStamp (reqPDU). PrettyPrint ()))
VarBinds=pMod. ApiTrapPDU. GetVarBinds (reqPDU)
The else:
VarBinds=pMod. ApiPDU. GetVarBinds (reqPDU)
Print (' Var - binds:)
For the oid, val in varBinds:
Print (' % s'=% s % (oid) prettyPrint (), val. PrettyPrint ()))
Return wholeMsg


TransportDispatcher=AsyncoreDispatcher ()

TransportDispatcher. RegisterRecvCbFun (cbFun)

# UDP/IPv4
TransportDispatcher. RegisterTransport (
Udp domainName, udp. UdpSocketTransport () openServerMode ((162) '192.168.6.1')
)

# UDP/IPv6
# transportDispatcher. RegisterTransport (
# udp6 domainName, udp6. Udp6SocketTransport () openServerMode ((162) ': : 1')
#)

# # Local domain socket
# transportDispatcher. RegisterTransport (
# Unix domainName, Unix. UnixSocketTransport () openServerMode ('/TMP/SNMP - manager ')
#)

TransportDispatcher. JobStarted (1)

Try:
# the Dispatcher will never finish the as job# 1 never reaches zero
TransportDispatcher. RunDispatcher ()
Except:
TransportDispatcher. CloseDispatcher ()
Raise
` ` `