Synchronous SNMP (v1arch)¶
This chapter illustrates various uses of the synchronous high-level programming interface to client-side SNMP entities along the lines of RFC1905.
Note
The following examples involve creating Python iterator, the next() call is used to invoke iterator just once.
In most examples approximate analogues of well known Net-SNMP snmp* tools command line options are shown. That may help those readers who, by chance are familiar with Net-SNMP tools, better understanding what the example code does.
Here’s a quick example on a simple SNMP GET by high-level API:
- with SNMPv1, community ‘public’
- over IPv4/UDP
- to an Agent at demo.snmplabs.com:161
- for an instance of SNMPv2-MIB::sysDescr.0 MIB object
- having MIB lookup feature enabled
from pysnmp.hlapi.v1arch import *
for response in getCmd(SnmpDispatcher(),
CommunityData('public', mpModel=0),
UdpTransportTarget(('demo.snmplabs.com', 161)),
ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysDescr', 0)),
lookupMib=True):
errorIndication, errorStatus, errorIndex, varBinds = response
if errorIndication:
print(errorIndication)
elif errorStatus:
print('%s at %s' % (errorStatus.prettyPrint(),
errorIndex and varBinds[int(errorIndex) - 1][0] or '?'))
else:
for varBind in varBinds:
print(' = '.join([x.prettyPrint() for x in varBind]))
Note
If MIB lookup is required (e.g. when ObjectIdentity
,
ObjectType
or NotificationType
objects being used), the lookupMib=True should also be passed.
The following code performs a series of SNMP GETNEXT operations fetching a table of SNMP variables from SNMP Agent:
- with SNMPv2c, community ‘public’
- over IPv4/UDP
- to an Agent at demo.snmplabs.com:161
- for all OIDs in IF-MIB
- with MIB lookup enabled
from pysnmp.hlapi.v1arch import *
for (errorIndication,
errorStatus,
errorIndex,
varBinds) in nextCmd(SnmpDispatcher(),
CommunityData('public'),
UdpTransportTarget(('demo.snmplabs.com', 161)),
ObjectType(ObjectIdentity('IF-MIB'))):
if errorIndication:
print(errorIndication)
break
elif errorStatus:
print('%s at %s' % (errorStatus.prettyPrint(),
errorIndex and varBinds[int(errorIndex) - 1][0] or '?'))
break
else:
for varBind in varBinds:
print(' = '.join([x.prettyPrint() for x in varBind]))
More examples on Command Generator API usage follow.
The following code sends SNMP TRAP:
- SNMPv2c
- with community name ‘public’
- over IPv4/UDP
- send TRAP notification
- with Uptime 12345
- with Generic Trap #1 (warmStart) and Specific Trap 0
- include managed object information ‘1.3.6.1.2.1.1.1.0’ = ‘my system’
from pysnmp.hlapi.v1arch import *
errorIndication, errorStatus, errorIndex, varBinds = next(
sendNotification(
SnmpDispatcher(),
CommunityData('public'),
UdpTransportTarget(('demo.snmplabs.com', 162)),
'trap',
# SNMPv2-MIB::sysUpTime.0 = 12345
('1.3.6.1.2.1.1.3.0', TimeTicks(12345)),
# SNMPv2-SMI::snmpTrapOID.0 = SNMPv2-MIB::warmStart
('1.3.6.1.6.3.1.1.4.1.0', ObjectIdentifier('1.3.6.1.6.3.1.1.5.2')),
# SNMPv2-MIB::sysName.0
('1.3.6.1.2.1.1.1.0', OctetString('my system'))
)
)
if errorIndication:
print(errorIndication)
More examples on Notification Originator API usage follow.
More specific SNMP operations can still be performed with PySNMP via its Native API to Standard SNMP Applications.