Skip to content

Commit

Permalink
Reformat example scripts for easier reading (etingof#241)
Browse files Browse the repository at this point in the history
  • Loading branch information
etingof authored Feb 12, 2019
1 parent c5f7842 commit b9a7b9c
Show file tree
Hide file tree
Showing 157 changed files with 1,371 additions and 823 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,26 @@

# List of targets in the following format:
# ((authData, transportTarget, varNames), ...)
targets = (
TARGETS = (
# 1-st target (SNMPv1 over IPv4/UDP)
(CommunityData('public', mpModel=0),
UdpTransportTarget(('demo.snmplabs.com', 161)),
(ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysDescr', 0)),
ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysLocation', 0)))),

# 2-nd target (SNMPv2c over IPv4/UDP)
(CommunityData('public'),
UdpTransportTarget(('demo.snmplabs.com', 161)),
(ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysDescr', 0)),
ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysLocation', 0)))),
# 3-nd target (SNMPv2c over IPv4/UDP) - same community and

# 3-nd target (SNMPv2c over IPv4/UDP) - same community and
# different transport address.
(CommunityData('public'),
Udp6TransportTarget(('::1', 161)),
(ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysContact', 0)),
ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysName', 0)))),

# N-th target
# ...
)
Expand All @@ -53,10 +56,11 @@ def cbFun(errorIndication, errorStatus, errorIndex, varBinds, **context):
for varBind in varBinds:
print(' = '.join([x.prettyPrint() for x in varBind]))


snmpDispatcher = SnmpDispatcher()

# Submit a bunch of initial GET requests
for authData, transportTarget, varBinds in targets:
for authData, transportTarget, varBinds in TARGETS:
getCmd(snmpDispatcher, authData, transportTarget, *varBinds,
cbFun=cbFun, lookupMib=True)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,23 @@

# List of targets in the following format:
# ((authData, transportTarget, varNames), ...)
targets = (
TARGETS = (
# 1-st target (SNMPv1 over IPv4/UDP)
(CommunityData('public', mpModel=0),
UdpTransportTarget(('demo.snmplabs.com', 161)),
(ObjectType(ObjectIdentity('1.3.6.1.2.1')),
ObjectType(ObjectIdentity('1.3.6.1.3.1')))),

# 2-nd target (SNMPv2c over IPv4/UDP)
(CommunityData('public'),
UdpTransportTarget(('demo.snmplabs.com', 161)),
(ObjectType(ObjectIdentity('1.3.6.1.4.1')),)),

# 3-th target (SNMPv3 over IPv6/UDP)
(CommunityData('public'),
Udp6TransportTarget(('::1', 161)),
(ObjectType(ObjectIdentity('IF-MIB', 'ifTable')),))

# N-th target
# ...
)
Expand All @@ -53,11 +56,12 @@ def cbFun(errorIndication, errorStatus, errorIndex, varBindTable, **context):

return context.get('nextVarBinds')


snmpDispatcher = SnmpDispatcher()

# Submit a bunch of initial GETNEXT requests
for authData, transportTarget, varBinds in targets:
for authData, transportTarget, varBinds in TARGETS:
nextCmd(snmpDispatcher, authData, transportTarget, *varBinds,
cbFun=cbFun, lookupMib=True)
cbFun=cbFun, lookupMib=True)

snmpDispatcher.transportDispatcher.runDispatcher()
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def cbFun(errorIndication, errorStatus, errorIndex, varBindTable, **context):

return context.get('nextVarBinds')


snmpDispatcher = SnmpDispatcher()

# Submit initial GETBULK request
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def cbFun(errorIndication, errorStatus, errorIndex, varBindTable, **context):

return context.get('nextVarBinds')


snmpDispatcher = SnmpDispatcher()

stateHandle = nextCmd(
Expand Down
38 changes: 19 additions & 19 deletions examples/hlapi/v1arch/asyncore/sync/agent/ntforg/generic-v1-trap.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,26 @@
"""#
from pysnmp.hlapi.v1arch import *

errorIndication, errorStatus, errorIndex, varBinds = next(
sendNotification(
SnmpDispatcher(),
CommunityData('public', mpModel=0),
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')),
# SNMP-COMMUNITY-MIB::snmpTrapAddress.0 = 127.0.0.1
('1.3.6.1.6.3.18.1.3.0', IpAddress('127.0.0.1')),
# SNMP-COMMUNITY-MIB::snmpTrapCommunity.0 = public
('1.3.6.1.6.3.18.1.4.0', OctetString('public')),
# SNMP-COMMUNITY-MIB::snmpTrapEnterprise.0 = 1.3.6.1.4.1.20408.4.1.1.2
('1.3.6.1.6.3.1.1.4.3.0', ObjectIdentifier('1.3.6.1.4.1.20408.4.1.1.2')),
# SNMPv2-MIB::sysName.0
('1.3.6.1.2.1.1.1.0', OctetString('my system'))
)
iterator = sendNotification(
SnmpDispatcher(),
CommunityData('public', mpModel=0),
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')),
# SNMP-COMMUNITY-MIB::snmpTrapAddress.0 = 127.0.0.1
('1.3.6.1.6.3.18.1.3.0', IpAddress('127.0.0.1')),
# SNMP-COMMUNITY-MIB::snmpTrapCommunity.0 = public
('1.3.6.1.6.3.18.1.4.0', OctetString('public')),
# SNMP-COMMUNITY-MIB::snmpTrapEnterprise.0 = 1.3.6.1.4.1.20408.4.1.1.2
('1.3.6.1.6.3.1.1.4.3.0', ObjectIdentifier('1.3.6.1.4.1.20408.4.1.1.2')),
# SNMPv2-MIB::sysName.0
('1.3.6.1.2.1.1.1.0', OctetString('my system'))
)

errorIndication, errorStatus, errorIndex, varBinds = next(iterator)

if errorIndication:
print(errorIndication)
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,20 @@
"""#
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'))
)
iterator = 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'))
)

errorIndication, errorStatus, errorIndex, varBinds = next(iterator)

if errorIndication:
print(errorIndication)
26 changes: 13 additions & 13 deletions examples/hlapi/v1arch/asyncore/sync/agent/ntforg/v2c-inform.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,21 @@
"""#
from pysnmp.hlapi.v1arch import *

errorIndication, errorStatus, errorIndex, varBinds = next(
sendNotification(
SnmpDispatcher(),
CommunityData('public'),
UdpTransportTarget(('demo.snmplabs.com', 162)),
'inform',
# 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'))
)
iterator = sendNotification(
SnmpDispatcher(),
CommunityData('public'),
UdpTransportTarget(('demo.snmplabs.com', 162)),
'inform',
# 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'))
)

errorIndication, errorStatus, errorIndex, varBinds = next(iterator)

if errorIndication:
print(errorIndication)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,21 @@
"""#
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
NotificationType(ObjectIdentity('SNMPv2-MIB', 'warmStart')),
# SNMPv2-MIB::sysName.0
('1.3.6.1.2.1.1.1.0', OctetString('my system')),
lookupMib=True
)
iterator = 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
NotificationType(ObjectIdentity('SNMPv2-MIB', 'warmStart')),
# SNMPv2-MIB::sysName.0
('1.3.6.1.2.1.1.1.0', OctetString('my system')),
lookupMib=True
)

errorIndication, errorStatus, errorIndex, varBinds = next(iterator)

if errorIndication:
print(errorIndication)
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,16 @@
"""#
from pysnmp.hlapi.v1arch import *

errorIndication, errorStatus, errorIndex, varBinds = next(
setCmd(SnmpDispatcher(),
CommunityData('public'),
UdpTransportTarget(('demo.snmplabs.com', 161)),
ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysORDescr', 1),
'new description'),
lookupMib=True)
iterator = setCmd(
SnmpDispatcher(),
CommunityData('public'),
UdpTransportTarget(('demo.snmplabs.com', 161)),
ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysORDescr', 1), 'new description'),
lookupMib=True
)

errorIndication, errorStatus, errorIndex, varBinds = next(iterator)

if errorIndication:
print(errorIndication)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,19 @@
"""#
from pysnmp.hlapi.v1arch import *

errorIndication, errorStatus, errorIndex, varBinds = next(
getCmd(SnmpDispatcher(),
CommunityData('public'),
UdpTransportTarget(('demo.snmplabs.com', 161)),
ObjectType(ObjectIdentity('IF-MIB', 'ifInOctets', 1).addAsn1MibSource(
'file:///usr/share/snmp',
'http://mibs.snmplabs.com/asn1/@mib@')),
lookupMib=True)
iterator = getCmd(
SnmpDispatcher(),
CommunityData('public'),
UdpTransportTarget(('demo.snmplabs.com', 161)),
ObjectType(ObjectIdentity('IF-MIB', 'ifInOctets', 1).addAsn1MibSource(
'file:///usr/share/snmp',
'http://mibs.snmplabs.com/asn1/@mib@')
),
lookupMib=True
)

errorIndication, errorStatus, errorIndex, varBinds = next(iterator)

if errorIndication:
print(errorIndication)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,21 @@
"""#
from pysnmp.hlapi.v1arch import *

for (errorIndication,
errorStatus,
errorIndex,
varBinds) in bulkCmd(SnmpDispatcher(),
CommunityData('public'),
UdpTransportTarget(('demo.snmplabs.com', 161)),
0, 50,
ObjectType(
ObjectIdentity('TCP-MIB', 'tcpConnTable').addMibSource(
'/opt/mibs/pysnmp').addMibSource(
'python_packaged_mibs')
),
lookupMib=True,
lexicographicMode=False):
iterator = bulkCmd(
SnmpDispatcher(),
CommunityData('public'),
UdpTransportTarget(('demo.snmplabs.com', 161)),
0, 50,
ObjectType(
ObjectIdentity('TCP-MIB', 'tcpConnTable').addMibSource(
'/opt/mibs/pysnmp').addMibSource(
'python_packaged_mibs')
),
lookupMib=True,
lexicographicMode=False
)

for errorIndication, errorStatus, errorIndex, varBinds in iterator:

if errorIndication:
print(errorIndication)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,17 @@
"""#
from pysnmp.hlapi.v1arch import *

errorIndication, errorStatus, errorIndex, varBinds = next(
getCmd(SnmpDispatcher(),
CommunityData('public'),
UdpTransportTarget(
('demo.snmplabs.com', 161), timeout=2.0, retries=0
),
(('1.3.6.1.2.1.1.1.0', None)))
iterator = getCmd(
SnmpDispatcher(),
CommunityData('public'),
UdpTransportTarget(
('demo.snmplabs.com', 161), timeout=2.0, retries=0
),
('1.3.6.1.2.1.1.1.0', None)
)

errorIndication, errorStatus, errorIndex, varBinds = next(iterator)

if errorIndication:
print(errorIndication)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@
"""#
from pysnmp.hlapi.v1arch import *

for (errorIndication,
errorStatus,
errorIndex,
varBinds) in nextCmd(SnmpDispatcher(),
CommunityData('public'),
UdpTransportTarget(('demo.snmplabs.com', 161)),
ObjectType(ObjectIdentity('1.3.6.1.2.1.1')),
lookupMib=True):
iterator = nextCmd(
SnmpDispatcher(),
CommunityData('public'),
UdpTransportTarget(('demo.snmplabs.com', 161)),
ObjectType(ObjectIdentity('1.3.6.1.2.1.1')),
lookupMib=True
)

for errorIndication, errorStatus, errorIndex, varBinds in iterator:

if errorIndication:
print(errorIndication)
Expand Down
Loading

0 comments on commit b9a7b9c

Please sign in to comment.