diff options
Diffstat (limited to 'examples/hlapi')
90 files changed, 1043 insertions, 695 deletions
diff --git a/examples/hlapi/v1arch/asyncore/manager/cmdgen/multiple-concurrent-queries-over-ipv4-and-ipv6.py b/examples/hlapi/v1arch/asyncore/manager/cmdgen/multiple-concurrent-queries-over-ipv4-and-ipv6.py index beeb1785..b40c5c09 100644 --- a/examples/hlapi/v1arch/asyncore/manager/cmdgen/multiple-concurrent-queries-over-ipv4-and-ipv6.py +++ b/examples/hlapi/v1arch/asyncore/manager/cmdgen/multiple-concurrent-queries-over-ipv4-and-ipv6.py @@ -19,23 +19,26 @@ from pysnmp.hlapi.v1arch.asyncore import * # 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 # ... ) @@ -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) diff --git a/examples/hlapi/v1arch/asyncore/manager/cmdgen/pull-mibs-from-multiple-agents-at-once-over-ipv4-and-ipv6.py b/examples/hlapi/v1arch/asyncore/manager/cmdgen/pull-mibs-from-multiple-agents-at-once-over-ipv4-and-ipv6.py index b3af60a4..32c92ff8 100644 --- a/examples/hlapi/v1arch/asyncore/manager/cmdgen/pull-mibs-from-multiple-agents-at-once-over-ipv4-and-ipv6.py +++ b/examples/hlapi/v1arch/asyncore/manager/cmdgen/pull-mibs-from-multiple-agents-at-once-over-ipv4-and-ipv6.py @@ -19,20 +19,23 @@ from pysnmp.hlapi.v1arch.asyncore import * # 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 # ... ) @@ -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() diff --git a/examples/hlapi/v1arch/asyncore/manager/cmdgen/pull-whole-mib.py b/examples/hlapi/v1arch/asyncore/manager/cmdgen/pull-whole-mib.py index 09113fd1..4af0138f 100644 --- a/examples/hlapi/v1arch/asyncore/manager/cmdgen/pull-whole-mib.py +++ b/examples/hlapi/v1arch/asyncore/manager/cmdgen/pull-whole-mib.py @@ -31,6 +31,7 @@ def cbFun(errorIndication, errorStatus, errorIndex, varBindTable, **context): return context.get('nextVarBinds') + snmpDispatcher = SnmpDispatcher() # Submit initial GETBULK request diff --git a/examples/hlapi/v1arch/asyncore/manager/cmdgen/v1-getnext.py b/examples/hlapi/v1arch/asyncore/manager/cmdgen/v1-getnext.py index 385e869c..d59f1ba0 100644 --- a/examples/hlapi/v1arch/asyncore/manager/cmdgen/v1-getnext.py +++ b/examples/hlapi/v1arch/asyncore/manager/cmdgen/v1-getnext.py @@ -35,6 +35,7 @@ def cbFun(errorIndication, errorStatus, errorIndex, varBindTable, **context): return context.get('nextVarBinds') + snmpDispatcher = SnmpDispatcher() stateHandle = nextCmd( diff --git a/examples/hlapi/v1arch/asyncore/sync/agent/ntforg/generic-v1-trap.py b/examples/hlapi/v1arch/asyncore/sync/agent/ntforg/generic-v1-trap.py index 2f1fa0fd..2d83da5f 100644 --- a/examples/hlapi/v1arch/asyncore/sync/agent/ntforg/generic-v1-trap.py +++ b/examples/hlapi/v1arch/asyncore/sync/agent/ntforg/generic-v1-trap.py @@ -21,26 +21,26 @@ Functionally similar to: """# 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) diff --git a/examples/hlapi/v1arch/asyncore/sync/agent/ntforg/generic-v2c-trap.py b/examples/hlapi/v1arch/asyncore/sync/agent/ntforg/generic-v2c-trap.py index 10a04ed4..639b7b6b 100644 --- a/examples/hlapi/v1arch/asyncore/sync/agent/ntforg/generic-v2c-trap.py +++ b/examples/hlapi/v1arch/asyncore/sync/agent/ntforg/generic-v2c-trap.py @@ -19,20 +19,20 @@ Functionally similar to: """# 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) diff --git a/examples/hlapi/v1arch/asyncore/sync/agent/ntforg/v2c-inform.py b/examples/hlapi/v1arch/asyncore/sync/agent/ntforg/v2c-inform.py index c1799993..9210d45a 100644 --- a/examples/hlapi/v1arch/asyncore/sync/agent/ntforg/v2c-inform.py +++ b/examples/hlapi/v1arch/asyncore/sync/agent/ntforg/v2c-inform.py @@ -18,21 +18,21 @@ Functionally similar to: """# 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) diff --git a/examples/hlapi/v1arch/asyncore/sync/agent/ntforg/v2c-trap-with-notification-objects.py b/examples/hlapi/v1arch/asyncore/sync/agent/ntforg/v2c-trap-with-notification-objects.py index 052946bc..0bf6c354 100644 --- a/examples/hlapi/v1arch/asyncore/sync/agent/ntforg/v2c-trap-with-notification-objects.py +++ b/examples/hlapi/v1arch/asyncore/sync/agent/ntforg/v2c-trap-with-notification-objects.py @@ -19,21 +19,21 @@ Functionally similar to: """# 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) diff --git a/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/coerce-set-value-to-mib-spec.py b/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/coerce-set-value-to-mib-spec.py index a6789f7f..a22e6e93 100644 --- a/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/coerce-set-value-to-mib-spec.py +++ b/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/coerce-set-value-to-mib-spec.py @@ -17,15 +17,16 @@ Functionally similar to: """# 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) diff --git a/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/custom-asn1-mib-search-path.py b/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/custom-asn1-mib-search-path.py index 759171c1..20767556 100644 --- a/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/custom-asn1-mib-search-path.py +++ b/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/custom-asn1-mib-search-path.py @@ -18,16 +18,19 @@ Functionally similar to: """# 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) diff --git a/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/custom-pysnmp-mibs-search-path.py b/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/custom-pysnmp-mibs-search-path.py index a3686d69..7f1f8d2c 100644 --- a/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/custom-pysnmp-mibs-search-path.py +++ b/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/custom-pysnmp-mibs-search-path.py @@ -20,20 +20,21 @@ Functionally similar to: """# 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) diff --git a/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/custom-timeout-and-retries.py b/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/custom-timeout-and-retries.py index 7a4253cf..f56d6511 100644 --- a/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/custom-timeout-and-retries.py +++ b/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/custom-timeout-and-retries.py @@ -30,15 +30,17 @@ Functionally similar to: """# 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) diff --git a/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/enable-mib-lookup.py b/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/enable-mib-lookup.py index 2f4f8edb..1072c71c 100644 --- a/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/enable-mib-lookup.py +++ b/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/enable-mib-lookup.py @@ -20,14 +20,15 @@ Functionally similar to: """# 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) diff --git a/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/fetch-variables-over-ipv6.py b/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/fetch-variables-over-ipv6.py index bd8d9ca2..dc9db8a5 100644 --- a/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/fetch-variables-over-ipv6.py +++ b/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/fetch-variables-over-ipv6.py @@ -15,15 +15,17 @@ Functionally similar to: """# from pysnmp.hlapi.v1arch import * -errorIndication, errorStatus, errorIndex, varBinds = next( - getCmd(SnmpDispatcher(), - CommunityData('public'), - Udp6TransportTarget(('::1', 161)), - ('1.3.6.1.2.1.1.1.0', None), - ('1.3.6.1.2.1.1.2.0', None), - ('1.3.6.1.2.1.1.3.0', None)) +iterator = getCmd( + SnmpDispatcher(), + CommunityData('public'), + Udp6TransportTarget(('::1', 161)), + ('1.3.6.1.2.1.1.1.0', None), + ('1.3.6.1.2.1.1.2.0', None), + ('1.3.6.1.2.1.1.3.0', None) ) +errorIndication, errorStatus, errorIndex, varBinds = next(iterator) + if errorIndication: print(errorIndication) diff --git a/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/get-table-object-by-index.py b/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/get-table-object-by-index.py index b7fccdcb..38d248c2 100644 --- a/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/get-table-object-by-index.py +++ b/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/get-table-object-by-index.py @@ -17,15 +17,17 @@ Functionally similar to: """# 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)), - ObjectType(ObjectIdentity('IF-MIB', 'ifOutOctets', 1)), - lookupMib=True) +iterator = getCmd( + SnmpDispatcher(), + CommunityData('public'), + UdpTransportTarget(('demo.snmplabs.com', 161)), + ObjectType(ObjectIdentity('IF-MIB', 'ifInOctets', 1)), + ObjectType(ObjectIdentity('IF-MIB', 'ifOutOctets', 1)), + lookupMib=True ) +errorIndication, errorStatus, errorIndex, varBinds = next(iterator) + if errorIndication: print(errorIndication) diff --git a/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/get-table-object-by-multiple-indices.py b/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/get-table-object-by-multiple-indices.py index cec5ad25..ae888ae3 100644 --- a/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/get-table-object-by-multiple-indices.py +++ b/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/get-table-object-by-multiple-indices.py @@ -17,17 +17,24 @@ Functionally similar to: """# from pysnmp.hlapi.v1arch import * -errorIndication, errorStatus, errorIndex, varBinds = next( - getCmd(SnmpDispatcher(), - CommunityData('public'), - UdpTransportTarget(('demo.snmplabs.com', 161)), - ObjectType(ObjectIdentity('TCP-MIB', - 'tcpConnLocalAddress', - '0.0.0.0', 22, - '0.0.0.0', 0)), - lookupMib=True) +iterator = getCmd( + SnmpDispatcher(), + CommunityData('public'), + UdpTransportTarget(('demo.snmplabs.com', 161)), + ObjectType( + ObjectIdentity( + 'TCP-MIB', + 'tcpConnLocalAddress', + '0.0.0.0', 22, + '0.0.0.0', 0 + ) + ), + lookupMib=True ) + +errorIndication, errorStatus, errorIndex, varBinds = next(iterator) + if errorIndication: print(errorIndication) diff --git a/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/getbulk-fetch-scalar-and-table-variables-over-ipv6.py b/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/getbulk-fetch-scalar-and-table-variables-over-ipv6.py index 49257a1c..1bdcf87a 100644 --- a/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/getbulk-fetch-scalar-and-table-variables-over-ipv6.py +++ b/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/getbulk-fetch-scalar-and-table-variables-over-ipv6.py @@ -19,17 +19,18 @@ Functionally similar to: """# from pysnmp.hlapi.v1arch import * -for (errorIndication, - errorStatus, - errorIndex, - varBinds) in bulkCmd(SnmpDispatcher(), - CommunityData('public'), - UdpTransportTarget(('demo.snmplabs.com', 161)), - 1, 25, - ObjectType(ObjectIdentity('IP-MIB', 'ipAdEntAddr')), - ObjectType(ObjectIdentity('IP-MIB', 'ipAddrEntry')), - lookupMib=True, - lexicographicMode=False): +iterator = bulkCmd( + SnmpDispatcher(), + CommunityData('public'), + UdpTransportTarget(('demo.snmplabs.com', 161)), + 1, 25, + ObjectType(ObjectIdentity('IP-MIB', 'ipAdEntAddr')), + ObjectType(ObjectIdentity('IP-MIB', 'ipAddrEntry')), + lookupMib=True, + lexicographicMode=False +) + +for errorIndication, errorStatus, errorIndex, varBinds in iterator: if errorIndication: print(errorIndication) diff --git a/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/getbulk-limit-number-of-packets.py b/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/getbulk-limit-number-of-packets.py index 22d52589..519c1bf2 100644 --- a/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/getbulk-limit-number-of-packets.py +++ b/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/getbulk-limit-number-of-packets.py @@ -19,16 +19,17 @@ Functionally similar to: """# from pysnmp.hlapi.v1arch import * -for (errorIndication, - errorStatus, - errorIndex, - varBinds) in bulkCmd(SnmpDispatcher(), - CommunityData('public'), - UdpTransportTarget(('demo.snmplabs.com', 161)), - 0, 50, - ObjectType(ObjectIdentity('SNMPv2-MIB', 'system')), - lookupMib=True, - maxCalls=10): +iterator = bulkCmd( + SnmpDispatcher(), + CommunityData('public'), + UdpTransportTarget(('demo.snmplabs.com', 161)), + 0, 50, + ObjectType(ObjectIdentity('SNMPv2-MIB', 'system')), + lookupMib=True, + maxCalls=10 +) + +for errorIndication, errorStatus, errorIndex, varBinds in iterator: if errorIndication: print(errorIndication) diff --git a/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/multiple-get-calls.py b/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/multiple-get-calls.py index 57a1132b..e1478085 100644 --- a/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/multiple-get-calls.py +++ b/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/multiple-get-calls.py @@ -27,15 +27,18 @@ queue = [ [ObjectType(ObjectIdentity('IF-MIB', 'ifOutOctets', 1))] ] -iterator = getCmd(SnmpDispatcher(), - CommunityData('public'), - UdpTransportTarget(('demo.snmplabs.com', 161)), - lookupMib=True) +iterator = getCmd( + SnmpDispatcher(), + CommunityData('public'), + UdpTransportTarget(('demo.snmplabs.com', 161)), + lookupMib=True +) next(iterator) while queue: errorIndication, errorStatus, errorIndex, varBinds = iterator.send(queue.pop()) + if errorIndication: print(errorIndication) diff --git a/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/preload-pysnmp-mibs.py b/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/preload-pysnmp-mibs.py index 39d1e7dd..0df556c4 100644 --- a/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/preload-pysnmp-mibs.py +++ b/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/preload-pysnmp-mibs.py @@ -18,14 +18,15 @@ Functionally similar to: """# 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').loadMibs()), - lookupMib=True): +iterator = nextCmd( + SnmpDispatcher(), + CommunityData('public'), + UdpTransportTarget(('demo.snmplabs.com', 161)), + ObjectType(ObjectIdentity('1.3.6').loadMibs()), + lookupMib=True +) + +for errorIndication, errorStatus, errorIndex, varBinds in iterator: if errorIndication: print(errorIndication) diff --git a/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/pull-whole-mib.py b/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/pull-whole-mib.py index 69e943d9..71cf6a70 100644 --- a/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/pull-whole-mib.py +++ b/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/pull-whole-mib.py @@ -17,13 +17,14 @@ Functionally similar to: """# from pysnmp.hlapi.v1arch import * -for (errorIndication, - errorStatus, - errorIndex, - varBinds) in nextCmd(SnmpDispatcher(), - CommunityData('public'), - UdpTransportTarget(('demo.snmplabs.com', 161)), - ObjectType(ObjectIdentity('IF-MIB'))): +iterator = nextCmd( + SnmpDispatcher(), + CommunityData('public'), + UdpTransportTarget(('demo.snmplabs.com', 161)), + ObjectType(ObjectIdentity('IF-MIB')) +) + +for errorIndication, errorStatus, errorIndex, varBinds in interator: if errorIndication: print(errorIndication) diff --git a/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/pull-whole-snmp-table.py b/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/pull-whole-snmp-table.py index 8d81b1e5..d605c2da 100644 --- a/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/pull-whole-snmp-table.py +++ b/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/pull-whole-snmp-table.py @@ -18,20 +18,21 @@ Functionally similar to: """# from pysnmp.hlapi.v1arch import * -for (errorIndication, - errorStatus, - errorIndex, - varBinds) in nextCmd(SnmpDispatcher(), - CommunityData('public', mpModel=0), - UdpTransportTarget(('demo.snmplabs.com', 161)), - ObjectType(ObjectIdentity('IF-MIB', 'ifDescr')), - ObjectType(ObjectIdentity('IF-MIB', 'ifType')), - ObjectType(ObjectIdentity('IF-MIB', 'ifMtu')), - ObjectType(ObjectIdentity('IF-MIB', 'ifSpeed')), - ObjectType(ObjectIdentity('IF-MIB', 'ifPhysAddress')), - ObjectType(ObjectIdentity('IF-MIB', 'ifType')), - lookupMib=True, - lexicographicMode=False): +iterator = nextCmd( + SnmpDispatcher(), + CommunityData('public', mpModel=0), + UdpTransportTarget(('demo.snmplabs.com', 161)), + ObjectType(ObjectIdentity('IF-MIB', 'ifDescr')), + ObjectType(ObjectIdentity('IF-MIB', 'ifType')), + ObjectType(ObjectIdentity('IF-MIB', 'ifMtu')), + ObjectType(ObjectIdentity('IF-MIB', 'ifSpeed')), + ObjectType(ObjectIdentity('IF-MIB', 'ifPhysAddress')), + ObjectType(ObjectIdentity('IF-MIB', 'ifType')), + lookupMib=True, + lexicographicMode=False +) + +for errorIndication, errorStatus, errorIndex, varBinds in iterator: if errorIndication: print(errorIndication) diff --git a/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/query-agents-from-multuple-threads-over-ipv4-and-ipv6.py b/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/query-agents-from-multuple-threads-over-ipv4-and-ipv6.py index 29ec78b6..98e235f3 100644 --- a/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/query-agents-from-multuple-threads-over-ipv4-and-ipv6.py +++ b/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/query-agents-from-multuple-threads-over-ipv4-and-ipv6.py @@ -35,29 +35,33 @@ else: # 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(('localhost', 161)), (ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysContact', 0)), ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysName', 0)))), + # 4-th target (SNMPv2c over IPv4/UDP) - same community and # different transport port. (CommunityData('public'), UdpTransportTarget(('demo.snmplabs.com', 1161)), (ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysDescr', 0)), ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysLocation', 0)))), + # N-th target # ... ) @@ -112,7 +116,7 @@ class ThreadPool(object): pool = ThreadPool(3) # Submit GET requests -for authData, transportTarget, varBinds in targets: +for authData, transportTarget, varBinds in TARGETS: pool.addRequest(authData, transportTarget, varBinds) # Wait for responses or errors @@ -120,11 +124,14 @@ pool.waitCompletion() # Walk through responses for errorIndication, errorStatus, errorIndex, varBinds in pool.getResponses(): + 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])) diff --git a/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/set-multiple-scalar-values.py b/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/set-multiple-scalar-values.py index b5498003..39edb0cb 100644 --- a/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/set-multiple-scalar-values.py +++ b/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/set-multiple-scalar-values.py @@ -21,19 +21,28 @@ Functionally similar to: """# from pysnmp.hlapi.v1arch import * -errorIndication, errorStatus, errorIndex, varBinds = next( - setCmd(SnmpDispatcher(), - CommunityData('public', mpModel=0), - UdpTransportTarget(('demo.snmplabs.com', 161)), - ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysORID', 1), - ObjectIdentifier('1.3.6.1.4.1.20408.1.1')), - ObjectType(ObjectIdentity('1.3.6.1.2.1.1.9.1.2.1'), - '1.3.6.1.4.1.20408.1.1'), - ObjectType(ObjectIdentity('1.3.6.1.2.1.1.9.1.3.1'), - OctetString('new system name')), - lookupMib=True) +iterator = setCmd( + SnmpDispatcher(), + CommunityData('public', mpModel=0), + UdpTransportTarget(('demo.snmplabs.com', 161)), + ObjectType( + ObjectIdentity('SNMPv2-MIB', 'sysORID', 1), + ObjectIdentifier('1.3.6.1.4.1.20408.1.1') + ), + ObjectType( + ObjectIdentity('1.3.6.1.2.1.1.9.1.2.1'), + '1.3.6.1.4.1.20408.1.1' + ), + ObjectType( + ObjectIdentity('1.3.6.1.2.1.1.9.1.3.1'), + OctetString('new system name') + ), + lookupMib=True ) + +errorIndication, errorStatus, errorIndex, varBinds = next(iterator) + if errorIndication: print(errorIndication) diff --git a/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/v1-get.py b/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/v1-get.py index b54802af..a07208e7 100644 --- a/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/v1-get.py +++ b/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/v1-get.py @@ -17,11 +17,15 @@ Functionally similar to: """# 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): +iterator = getCmd( + SnmpDispatcher(), + CommunityData('public', mpModel=0), + UdpTransportTarget(('demo.snmplabs.com', 161)), + ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysDescr', 0)), + lookupMib=True +) + +for response in iterator: errorIndication, errorStatus, errorIndex, varBinds = response diff --git a/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/v2c-get.py b/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/v2c-get.py index e2ec5815..25e1c626 100644 --- a/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/v2c-get.py +++ b/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/v2c-get.py @@ -16,11 +16,15 @@ Functionally similar to: """# from pysnmp.hlapi.v1arch import * -for response in getCmd(SnmpDispatcher(), - CommunityData('public'), - UdpTransportTarget(('demo.snmplabs.com', 161)), - ('1.3.6.1.2.1.1.1.0', None), - ('1.3.6.1.2.1.1.6.0', None)): +iterator = getCmd( + SnmpDispatcher(), + CommunityData('public'), + UdpTransportTarget(('demo.snmplabs.com', 161)), + ('1.3.6.1.2.1.1.1.0', None), + ('1.3.6.1.2.1.1.6.0', None) +) + +for response in iterator: errorIndication, errorStatus, errorIndex, varBinds = response diff --git a/examples/hlapi/v3arch/asyncio/agent/ntforg/default-v1-trap.py b/examples/hlapi/v3arch/asyncio/agent/ntforg/default-v1-trap.py index 6a0fefda..41032e9b 100644 --- a/examples/hlapi/v3arch/asyncio/agent/ntforg/default-v1-trap.py +++ b/examples/hlapi/v3arch/asyncio/agent/ntforg/default-v1-trap.py @@ -26,8 +26,10 @@ from pysnmp.hlapi.v3arch.asyncio import * @asyncio.coroutine def run(): + snmpEngine = SnmpEngine() - errorIndication, errorStatus, errorIndex, varBinds = yield from sendNotification( + + iterator = sendNotification( snmpEngine, CommunityData('public', mpModel=0), UdpTransportTarget(('demo.snmplabs.com', 162)), @@ -43,6 +45,8 @@ def run(): ) ) + errorIndication, errorStatus, errorIndex, varBinds = yield from iterator + if errorIndication: print(errorIndication) diff --git a/examples/hlapi/v3arch/asyncio/agent/ntforg/multiple-notifications-at-once.py b/examples/hlapi/v3arch/asyncio/agent/ntforg/multiple-notifications-at-once.py index f1cae2b8..fbdb568c 100644 --- a/examples/hlapi/v3arch/asyncio/agent/ntforg/multiple-notifications-at-once.py +++ b/examples/hlapi/v3arch/asyncio/agent/ntforg/multiple-notifications-at-once.py @@ -29,10 +29,8 @@ from pysnmp.hlapi.v3arch.asyncio import * @asyncio.coroutine def sendone(snmpEngine, hostname, notifyType): - (errorIndication, - errorStatus, - errorIndex, - varBinds) = yield from sendNotification( + + iterator = sendNotification( snmpEngine, CommunityData('public', tag=hostname), UdpTransportTarget((hostname, 162), tagList=hostname), @@ -48,10 +46,14 @@ def sendone(snmpEngine, hostname, notifyType): ) ) + errorIndication, errorStatus, errorIndex, varBinds = yield from iterator + 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])) @@ -61,6 +63,8 @@ snmpEngine = SnmpEngine() loop = asyncio.get_event_loop() loop.run_until_complete( - asyncio.wait([sendone(snmpEngine, 'demo.snmplabs.com', 'trap'), - sendone(snmpEngine, 'demo.snmplabs.com', 'inform')]) + asyncio.wait( + [sendone(snmpEngine, 'demo.snmplabs.com', 'trap'), + sendone(snmpEngine, 'demo.snmplabs.com', 'inform')] + ) ) diff --git a/examples/hlapi/v3arch/asyncio/manager/cmdgen/getbulk-to-eom.py b/examples/hlapi/v3arch/asyncio/manager/cmdgen/getbulk-to-eom.py index c60d6e12..aa6bf441 100644 --- a/examples/hlapi/v3arch/asyncio/manager/cmdgen/getbulk-to-eom.py +++ b/examples/hlapi/v3arch/asyncio/manager/cmdgen/getbulk-to-eom.py @@ -23,22 +23,25 @@ from pysnmp.hlapi.v3arch.asyncio import * @asyncio.coroutine def run(varBinds): + snmpEngine = SnmpEngine() + while True: - (errorIndication, - errorStatus, - errorIndex, - varBindTable) = yield from bulkCmd( + iterator = bulkCmd( snmpEngine, UsmUserData('usr-none-none'), UdpTransportTarget(('demo.snmplabs.com', 161)), ContextData(), 0, 50, - *varBinds) + *varBinds + ) + + errorIndication, errorStatus, errorIndex, varBindTable = yield from iterator if errorIndication: print(errorIndication) break + elif errorStatus: print('%s at %s' % ( errorStatus.prettyPrint(), diff --git a/examples/hlapi/v3arch/asyncio/manager/cmdgen/multiple-concurrent-queries-over-ipv4-and-ipv6.py b/examples/hlapi/v3arch/asyncio/manager/cmdgen/multiple-concurrent-queries-over-ipv4-and-ipv6.py index a1b5d9a0..5f3c78e7 100644 --- a/examples/hlapi/v3arch/asyncio/manager/cmdgen/multiple-concurrent-queries-over-ipv4-and-ipv6.py +++ b/examples/hlapi/v3arch/asyncio/manager/cmdgen/multiple-concurrent-queries-over-ipv4-and-ipv6.py @@ -23,7 +23,8 @@ from pysnmp.hlapi.v3arch.asyncio import * @asyncio.coroutine def getone(snmpEngine, hostname): - errorIndication, errorStatus, errorIndex, varBinds = yield from getCmd( + + iterator = getCmd( snmpEngine, CommunityData('public'), UdpTransportTarget(hostname), @@ -31,8 +32,11 @@ def getone(snmpEngine, hostname): ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysDescr', 0)) ) + errorIndication, errorStatus, errorIndex, varBinds = yield from iterator + if errorIndication: print(errorIndication) + elif errorStatus: print('%s at %s' % ( errorStatus.prettyPrint(), @@ -48,7 +52,9 @@ snmpEngine = SnmpEngine() loop = asyncio.get_event_loop() loop.run_until_complete( - asyncio.wait([getone(snmpEngine, ('demo.snmplabs.com', 1161)), - getone(snmpEngine, ('demo.snmplabs.com', 2161)), - getone(snmpEngine, ('demo.snmplabs.com', 3161))]) + asyncio.wait( + [getone(snmpEngine, ('demo.snmplabs.com', 1161)), + getone(snmpEngine, ('demo.snmplabs.com', 2161)), + getone(snmpEngine, ('demo.snmplabs.com', 3161))] + ) ) diff --git a/examples/hlapi/v3arch/asyncio/manager/cmdgen/multiple-sequential-queries.py b/examples/hlapi/v3arch/asyncio/manager/cmdgen/multiple-sequential-queries.py index d546870d..626661d0 100644 --- a/examples/hlapi/v3arch/asyncio/manager/cmdgen/multiple-sequential-queries.py +++ b/examples/hlapi/v3arch/asyncio/manager/cmdgen/multiple-sequential-queries.py @@ -23,7 +23,8 @@ from pysnmp.hlapi.v3arch.asyncio import * @asyncio.coroutine def getone(snmpEngine, hostname): - errorIndication, errorStatus, errorIndex, varBinds = yield from getCmd( + + iterator = getCmd( snmpEngine, CommunityData('public'), UdpTransportTarget(hostname), @@ -31,8 +32,11 @@ def getone(snmpEngine, hostname): ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysDescr', 0)) ) + errorIndication, errorStatus, errorIndex, varBinds = yield from iterator + if errorIndication: print(errorIndication) + elif errorStatus: print('%s at %s' % ( errorStatus.prettyPrint(), @@ -53,6 +57,13 @@ def getall(snmpEngine, hostnames): snmpEngine = SnmpEngine() loop = asyncio.get_event_loop() -loop.run_until_complete(getall(snmpEngine, [('demo.snmplabs.com', 1161), - ('demo.snmplabs.com', 2161), - ('demo.snmplabs.com', 3161)])) + +loop.run_until_complete( + getall( + snmpEngine, [ + ('demo.snmplabs.com', 1161), + ('demo.snmplabs.com', 2161), + ('demo.snmplabs.com', 3161) + ] + ) +) diff --git a/examples/hlapi/v3arch/asyncio/manager/cmdgen/v1-get.py b/examples/hlapi/v3arch/asyncio/manager/cmdgen/v1-get.py index f14a9f63..a7bf585a 100644 --- a/examples/hlapi/v3arch/asyncio/manager/cmdgen/v1-get.py +++ b/examples/hlapi/v3arch/asyncio/manager/cmdgen/v1-get.py @@ -22,7 +22,8 @@ from pysnmp.hlapi.v3arch.asyncio import * @asyncio.coroutine def run(): snmpEngine = SnmpEngine() - errorIndication, errorStatus, errorIndex, varBinds = yield from getCmd( + + iterator = getCmd( snmpEngine, CommunityData('public', mpModel=0), UdpTransportTarget(('demo.snmplabs.com', 161)), @@ -30,8 +31,11 @@ def run(): ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysDescr', 0)) ) + errorIndication, errorStatus, errorIndex, varBinds = yield from iterator + if errorIndication: print(errorIndication) + elif errorStatus: print('%s at %s' % ( errorStatus.prettyPrint(), diff --git a/examples/hlapi/v3arch/asyncore/agent/ntforg/multiple-informs-at-once.py b/examples/hlapi/v3arch/asyncore/agent/ntforg/multiple-informs-at-once.py index 9690c2e4..a1c73886 100644 --- a/examples/hlapi/v3arch/asyncore/agent/ntforg/multiple-informs-at-once.py +++ b/examples/hlapi/v3arch/asyncore/agent/ntforg/multiple-informs-at-once.py @@ -20,11 +20,12 @@ from pysnmp.hlapi.v3arch.asyncore import * # List of targets in the followin format: # ( ( authData, transportTarget ), ... ) -targets = ( +TARGETS = ( # 1-st target (SNMPv2c over IPv4/UDP) (CommunityData('public'), UdpTransportTarget(('demo.snmplabs.com', 162)), ContextData()), + # 2-nd target (SNMPv3 over IPv4/UDP) (UsmUserData('usr-md5-des', 'authkey1', 'privkey1'), UdpTransportTarget(('demo.snmplabs.com', 162)), @@ -48,7 +49,7 @@ def cbFun(snmpEngine, sendRequestHandle, errorIndication, snmpEngine = SnmpEngine() -for authData, transportTarget, contextData in targets: +for authData, transportTarget, contextData in TARGETS: sendPduHandle = sendNotification( snmpEngine, authData, diff --git a/examples/hlapi/v3arch/asyncore/agent/ntforg/multiple-traps-at-once.py b/examples/hlapi/v3arch/asyncore/agent/ntforg/multiple-traps-at-once.py index ad873868..83d9cfc4 100644 --- a/examples/hlapi/v3arch/asyncore/agent/ntforg/multiple-traps-at-once.py +++ b/examples/hlapi/v3arch/asyncore/agent/ntforg/multiple-traps-at-once.py @@ -21,11 +21,12 @@ from pysnmp.hlapi.v3arch.asyncore import * # List of targets in the followin format: # ( ( authData, transportTarget ), ... ) -targets = ( +TARGETS = ( # 1-st target (SNMPv1 over IPv4/UDP) (CommunityData('public', mpModel=0), UdpTransportTarget(('demo.snmplabs.com', 162)), ContextData()), + # 2-nd target (SNMPv2c over IPv4/UDP) (CommunityData('public'), UdpTransportTarget(('demo.snmplabs.com', 162)), @@ -34,7 +35,7 @@ targets = ( snmpEngine = SnmpEngine() -for authData, transportTarget, contextData in targets: +for authData, transportTarget, contextData in TARGETS: sendNotification( snmpEngine, authData, diff --git a/examples/hlapi/v3arch/asyncore/agent/ntforg/running-multiple-snmp-engines-at-once.py b/examples/hlapi/v3arch/asyncore/agent/ntforg/running-multiple-snmp-engines-at-once.py index 10a96756..c60ea839 100644 --- a/examples/hlapi/v3arch/asyncore/agent/ntforg/running-multiple-snmp-engines-at-once.py +++ b/examples/hlapi/v3arch/asyncore/agent/ntforg/running-multiple-snmp-engines-at-once.py @@ -34,11 +34,12 @@ from pysnmp.carrier.asyncore.dispatch import AsyncoreDispatcher # List of targets in the following format: # ( ( authData, transportTarget ), ... ) -targets = ( +TARGETS = ( # 1-st target (SNMPv2c over IPv4/UDP) (CommunityData('public'), UdpTransportTarget(('demo.snmplabs.com', 1162)), ContextData()), + # 2-nd target (SNMPv3 over IPv4/UDP) (UsmUserData('usr-md5-des', 'authkey1', 'privkey1'), UdpTransportTarget(('demo.snmplabs.com', 162)), @@ -49,13 +50,22 @@ targets = ( # noinspection PyUnusedLocal def cbFun(snmpEngine, sendRequestHandle, errorIndication, errorStatus, errorIndex, varBinds, cbCtx): - snmpEngine = cbCtx + if errorIndication: - print('Notification %s for %s not sent: %s' % (sendRequestHandle, snmpEngine.snmpEngineID.prettyPrint(), errorIndication)) + print('Notification %s for %s not sent: %s' % ( + sendRequestHandle, snmpEngine.snmpEngineID.prettyPrint(), + errorIndication)) + elif errorStatus: - print('Notification Receiver returned error for request %s, SNMP Engine %s: %s @%s' % (sendRequestHandle, snmpEngine.snmpEngineID.prettyPrint(), errorStatus, errorIndex)) + print('Notification Receiver returned error for request %s, ' + 'SNMP Engine %s: %s @%s' % (sendRequestHandle, + snmpEngine.snmpEngineID.prettyPrint(), + errorStatus, errorIndex)) + else: - print('Notification %s for SNMP Engine %s delivered:' % (sendRequestHandle, snmpEngine.snmpEngineID.prettyPrint())) + print('Notification %s for SNMP Engine %s delivered: ' % ( + sendRequestHandle, snmpEngine.snmpEngineID.prettyPrint())) + for name, val in varBinds: print('%s = %s' % (name.prettyPrint(), val.prettyPrint())) @@ -74,9 +84,12 @@ snmpEngineA.registerTransportDispatcher(transportDispatcher, 'A') snmpEngineB = SnmpEngine() snmpEngineB.registerTransportDispatcher(transportDispatcher, 'B') -for authData, transportTarget, contextData in targets: +for authData, transportTarget, contextData in TARGETS: + + # Pick one of the two SNMP engines snmpEngine = (transportTarget.getTransportInfo()[1][1] % 3 and snmpEngineA or snmpEngineB) + sendPduHandle = sendNotification( snmpEngine, authData, @@ -86,7 +99,7 @@ for authData, transportTarget, contextData in targets: NotificationType( ObjectIdentity('SNMPv2-MIB', 'coldStart') ).addVarBinds(('1.3.6.1.2.1.1.1.0', 'my name')), - cbFun=cbFun, cbCtx=snmpEngine + cbFun=cbFun ) transportDispatcher.runDispatcher() diff --git a/examples/hlapi/v3arch/asyncore/manager/cmdgen/multiple-concurrent-queries-over-ipv4-and-ipv6.py b/examples/hlapi/v3arch/asyncore/manager/cmdgen/multiple-concurrent-queries-over-ipv4-and-ipv6.py index ba9ffed3..1422fe3e 100644 --- a/examples/hlapi/v3arch/asyncore/manager/cmdgen/multiple-concurrent-queries-over-ipv4-and-ipv6.py +++ b/examples/hlapi/v3arch/asyncore/manager/cmdgen/multiple-concurrent-queries-over-ipv4-and-ipv6.py @@ -20,28 +20,32 @@ from pysnmp.hlapi.v3arch.asyncore import * # List of targets in the followin 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'), UdpTransportTarget(('localhost', 161)), (ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysContact', 0)), ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysName', 0)))), + # 4-nd target (SNMPv3 over IPv4/UDP) (UsmUserData('usr-md5-des', 'authkey1', 'privkey1'), UdpTransportTarget(('demo.snmplabs.com', 161)), (ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysDescr', 0)), ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysLocation', 0)))), + # 5-th target (SNMPv3 over IPv6/UDP) (UsmUserData('usr-md5-none', 'authkey1'), Udp6TransportTarget(('::1', 161)), @@ -56,15 +60,20 @@ targets = ( # noinspection PyUnusedLocal,PyUnusedLocal def cbFun(snmpEngine, sendRequestHandle, errorIndication, errorStatus, errorIndex, varBinds, cbCtx): + authData, transportTarget = cbCtx + print('%s via %s' % (authData, transportTarget)) + if errorIndication: print(errorIndication) return True + elif errorStatus: print('%s at %s' % (errorStatus.prettyPrint(), errorIndex and varBinds[int(errorIndex) - 1][0] or '?')) return True + else: for varBind in varBinds: print(' = '.join([x.prettyPrint() for x in varBind])) @@ -73,7 +82,7 @@ def cbFun(snmpEngine, sendRequestHandle, errorIndication, snmpEngine = SnmpEngine() # Submit GET requests -for authData, transportTarget, varNames in targets: +for authData, transportTarget, varNames in TARGETS: getCmd(snmpEngine, authData, transportTarget, ContextData(), *varNames, cbFun=cbFun, cbCtx=(authData, transportTarget)) diff --git a/examples/hlapi/v3arch/asyncore/manager/cmdgen/pull-mibs-from-multiple-agents-at-once-over-ipv4-and-ipv6.py b/examples/hlapi/v3arch/asyncore/manager/cmdgen/pull-mibs-from-multiple-agents-at-once-over-ipv4-and-ipv6.py index 38e96f45..44247b9a 100644 --- a/examples/hlapi/v3arch/asyncore/manager/cmdgen/pull-mibs-from-multiple-agents-at-once-over-ipv4-and-ipv6.py +++ b/examples/hlapi/v3arch/asyncore/manager/cmdgen/pull-mibs-from-multiple-agents-at-once-over-ipv4-and-ipv6.py @@ -25,18 +25,22 @@ targets = ( 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-nd target (SNMPv3 over IPv4/UDP) (UsmUserData('usr-md5-des', 'authkey1', 'privkey1'), UdpTransportTarget(('demo.snmplabs.com', 161)), (ObjectType(ObjectIdentity('SNMPv2-MIB', 'system')),)), + # 4-th target (SNMPv3 over IPv6/UDP) (UsmUserData('usr-md5-none', 'authkey1'), Udp6TransportTarget(('::1', 161)), (ObjectType(ObjectIdentity('IF-MIB', 'ifTable')),)) + # N-th target # ... ) @@ -46,15 +50,20 @@ targets = ( # noinspection PyUnusedLocal,PyUnusedLocal def cbFun(snmpEngine, sendRequestHandle, errorIndication, errorStatus, errorIndex, varBindTable, cbCtx): - (authData, transportTarget) = cbCtx + + authData, transportTarget = cbCtx + print('%s via %s' % (authData, transportTarget)) + if errorIndication: print(errorIndication) return + elif errorStatus: print('%s at %s' % (errorStatus.prettyPrint(), errorIndex and varBindTable[-1][int(errorIndex) - 1][0] or '?')) return + else: for varBindRow in varBindTable: for varBind in varBindRow: diff --git a/examples/hlapi/v3arch/asyncore/manager/cmdgen/pull-whole-mib.py b/examples/hlapi/v3arch/asyncore/manager/cmdgen/pull-whole-mib.py index a0aa02b9..e6fe324a 100644 --- a/examples/hlapi/v3arch/asyncore/manager/cmdgen/pull-whole-mib.py +++ b/examples/hlapi/v3arch/asyncore/manager/cmdgen/pull-whole-mib.py @@ -21,13 +21,16 @@ from pysnmp.hlapi.v3arch.asyncore import * # noinspection PyUnusedLocal,PyUnusedLocal,PyUnusedLocal def cbFun(snmpEngine, sendRequestHandle, errorIndication, errorStatus, errorIndex, varBindTable, cbCtx): + if errorIndication: print(errorIndication) return + elif errorStatus: print('%s at %s' % (errorStatus.prettyPrint(), errorIndex and varBindTable[-1][int(errorIndex) - 1][0] or '?')) return + else: for varBindRow in varBindTable: for varBind in varBindRow: diff --git a/examples/hlapi/v3arch/asyncore/manager/cmdgen/query-multiple-snmp-engines-over-ipv4-and-ipv6.py b/examples/hlapi/v3arch/asyncore/manager/cmdgen/query-multiple-snmp-engines-over-ipv4-and-ipv6.py index ad6e838f..fc2143d4 100644 --- a/examples/hlapi/v3arch/asyncore/manager/cmdgen/query-multiple-snmp-engines-over-ipv4-and-ipv6.py +++ b/examples/hlapi/v3arch/asyncore/manager/cmdgen/query-multiple-snmp-engines-over-ipv4-and-ipv6.py @@ -33,22 +33,25 @@ from pysnmp.carrier.asyncore.dispatch import AsyncoreDispatcher # 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', 1161)), (ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysDescr', 0)), ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysLocation', 0)))), + # 3-nd target (SNMPv3 over IPv4/UDP) (UsmUserData('usr-md5-des', 'authkey1', 'privkey1'), UdpTransportTarget(('demo.snmplabs.com', 2161)), (ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysDescr', 0)), ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysLocation', 0)))) + # N-th target # ... ) @@ -58,15 +61,19 @@ targets = ( # noinspection PyUnusedLocal,PyUnusedLocal def cbFun(snmpEngine, sendRequestHandle, errorIndication, errorStatus, errorIndex, varBinds, cbCtx): - (snmpEngine, authData, transportTarget) = cbCtx + authData, transportTarget = cbCtx + print('snmpEngine %s: %s via %s' % (snmpEngine.snmpEngineID.prettyPrint(), authData, transportTarget)) + if errorIndication: print(errorIndication) return True + elif errorStatus: print('%s at %s' % (errorStatus.prettyPrint(), errorIndex and varBinds[int(errorIndex) - 1][0] or '?')) return True + else: for varBind in varBinds: print(' = '.join([x.prettyPrint() for x in varBind])) @@ -86,10 +93,11 @@ snmpEngineA.registerTransportDispatcher(transportDispatcher, 'A') snmpEngineB = SnmpEngine() snmpEngineB.registerTransportDispatcher(transportDispatcher, 'B') -for authData, transportTarget, varBinds in targets: - snmpEngine = transportTarget.getTransportInfo()[1][1] % 3 and \ - snmpEngineA or snmpEngineB +for authData, transportTarget, varBinds in TARGETS: + snmpEngine = (transportTarget.getTransportInfo()[1][1] % 3 and + snmpEngineA or snmpEngineB) + getCmd(snmpEngine, authData, transportTarget, ContextData(), *varBinds, - cbFun=cbFun, cbCtx=(snmpEngine, authData, transportTarget)) + cbFun=cbFun, cbCtx=(authData, transportTarget)) transportDispatcher.runDispatcher() diff --git a/examples/hlapi/v3arch/asyncore/manager/cmdgen/v2c-get.py b/examples/hlapi/v3arch/asyncore/manager/cmdgen/v2c-get.py index ae8f8262..da2e7b34 100644 --- a/examples/hlapi/v3arch/asyncore/manager/cmdgen/v2c-get.py +++ b/examples/hlapi/v3arch/asyncore/manager/cmdgen/v2c-get.py @@ -20,13 +20,16 @@ from pysnmp.hlapi.v3arch.asyncore import * # noinspection PyUnusedLocal,PyUnusedLocal,PyUnusedLocal def cbFun(snmpEngine, sendRequestHandle, errorIndication, errorStatus, errorIndex, varBinds, cbCtx): + if errorIndication: print(errorIndication) return + elif errorStatus: print('%s at %s' % (errorStatus.prettyPrint(), errorIndex and varBindTable[-1][int(errorIndex) - 1][0] or '?')) return + else: for varBind in varBinds: print(' = '.join([x.prettyPrint() for x in varBind])) diff --git a/examples/hlapi/v3arch/asyncore/sync/agent/ntforg/custom-contextengineid.py b/examples/hlapi/v3arch/asyncore/sync/agent/ntforg/custom-contextengineid.py index 1b9e12d5..18da255f 100644 --- a/examples/hlapi/v3arch/asyncore/sync/agent/ntforg/custom-contextengineid.py +++ b/examples/hlapi/v3arch/asyncore/sync/agent/ntforg/custom-contextengineid.py @@ -22,23 +22,26 @@ Functionally similar to: """# from pysnmp.hlapi import * -errorIndication, errorStatus, errorIndex, varBinds = next( - sendNotification(SnmpEngine(), - UsmUserData('usr-md5-none', 'authkey1'), - UdpTransportTarget(('demo.snmplabs.com', 162)), - ContextData(OctetString(hexValue='8000000004030201')), - 'inform', - NotificationType( - ObjectIdentity('1.3.6.1.6.3.1.1.5.2') - ).loadMibs('SNMPv2-MIB') - ) +iterator = sendNotification( + SnmpEngine(), + UsmUserData('usr-md5-none', 'authkey1'), + UdpTransportTarget(('demo.snmplabs.com', 162)), + ContextData(OctetString(hexValue='8000000004030201')), + 'inform', + NotificationType( + ObjectIdentity('1.3.6.1.6.3.1.1.5.2') + ).loadMibs('SNMPv2-MIB') ) +errorIndication, errorStatus, errorIndex, varBinds = next(iterator) + 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])) diff --git a/examples/hlapi/v3arch/asyncore/sync/agent/ntforg/custom-contextname.py b/examples/hlapi/v3arch/asyncore/sync/agent/ntforg/custom-contextname.py index 96abba48..2c232cb6 100644 --- a/examples/hlapi/v3arch/asyncore/sync/agent/ntforg/custom-contextname.py +++ b/examples/hlapi/v3arch/asyncore/sync/agent/ntforg/custom-contextname.py @@ -22,20 +22,24 @@ Functionally similar to: """# from pysnmp.hlapi import * -errorIndication, errorStatus, errorIndex, varBinds = next( - sendNotification(SnmpEngine(), - UsmUserData('usr-md5-none', 'authkey1'), - UdpTransportTarget(('demo.snmplabs.com', 162)), - ContextData(contextName='my-context'), - 'inform', - NotificationType(ObjectIdentity('1.3.6.1.6.3.1.1.5.2')).loadMibs('SNMPv2-MIB')) +iterator = sendNotification( + SnmpEngine(), + UsmUserData('usr-md5-none', 'authkey1'), + UdpTransportTarget(('demo.snmplabs.com', 162)), + ContextData(contextName='my-context'), + 'inform', + NotificationType(ObjectIdentity('1.3.6.1.6.3.1.1.5.2')).loadMibs('SNMPv2-MIB') ) +errorIndication, errorStatus, errorIndex, varBinds = next(iterator) + 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])) diff --git a/examples/hlapi/v3arch/asyncore/sync/agent/ntforg/custom-v1-trap.py b/examples/hlapi/v3arch/asyncore/sync/agent/ntforg/custom-v1-trap.py index c6d84303..017fc8b8 100644 --- a/examples/hlapi/v3arch/asyncore/sync/agent/ntforg/custom-v1-trap.py +++ b/examples/hlapi/v3arch/asyncore/sync/agent/ntforg/custom-v1-trap.py @@ -24,24 +24,25 @@ Functionally similar to: """# from pysnmp.hlapi import * -errorIndication, errorStatus, errorIndex, varBinds = next( - sendNotification( - SnmpEngine(), - CommunityData('public', mpModel=0), - UdpTransportTarget(('demo.snmplabs.com', 162)), - ContextData(), - 'trap', - NotificationType( - ObjectIdentity('1.3.6.1.4.1.20408.4.1.1.2.0.432'), - ).addVarBinds( - ('1.3.6.1.2.1.1.3.0', 12345), - ('1.3.6.1.6.3.18.1.3.0', '127.0.0.1'), - ('1.3.6.1.6.3.1.1.4.3.0', '1.3.6.1.4.1.20408.4.1.1.2'), - ('1.3.6.1.2.1.1.1.0', OctetString('my system')) - ).loadMibs( - 'SNMPv2-MIB', 'SNMP-COMMUNITY-MIB' - ) +iterator = sendNotification( + SnmpEngine(), + CommunityData('public', mpModel=0), + UdpTransportTarget(('demo.snmplabs.com', 162)), + ContextData(), + 'trap', + NotificationType( + ObjectIdentity('1.3.6.1.4.1.20408.4.1.1.2.0.432'), + ).addVarBinds( + ('1.3.6.1.2.1.1.3.0', 12345), + ('1.3.6.1.6.3.18.1.3.0', '127.0.0.1'), + ('1.3.6.1.6.3.1.1.4.3.0', '1.3.6.1.4.1.20408.4.1.1.2'), + ('1.3.6.1.2.1.1.1.0', OctetString('my system')) + ).loadMibs( + 'SNMPv2-MIB', 'SNMP-COMMUNITY-MIB' ) ) + +errorIndication, errorStatus, errorIndex, varBinds = next(iterator) + if errorIndication: print(errorIndication) diff --git a/examples/hlapi/v3arch/asyncore/sync/agent/ntforg/default-v1-trap.py b/examples/hlapi/v3arch/asyncore/sync/agent/ntforg/default-v1-trap.py index 552ad877..4846f9da 100644 --- a/examples/hlapi/v3arch/asyncore/sync/agent/ntforg/default-v1-trap.py +++ b/examples/hlapi/v3arch/asyncore/sync/agent/ntforg/default-v1-trap.py @@ -22,23 +22,23 @@ Functionally similar to: """# from pysnmp.hlapi import * -errorIndication, errorStatus, errorIndex, varBinds = next( - sendNotification( - SnmpEngine(), - CommunityData('public', mpModel=0), - UdpTransportTarget(('demo.snmplabs.com', 162)), - ContextData(), - 'trap', - NotificationType( - ObjectIdentity('1.3.6.1.6.3.1.1.5.2') - ).addVarBinds( - ('1.3.6.1.6.3.1.1.4.3.0', '1.3.6.1.4.1.20408.4.1.1.2'), - ('1.3.6.1.2.1.1.1.0', OctetString('my system')) - ).loadMibs( - 'SNMPv2-MIB' - ) +iterator = sendNotification( + SnmpEngine(), + CommunityData('public', mpModel=0), + UdpTransportTarget(('demo.snmplabs.com', 162)), + ContextData(), + 'trap', + NotificationType( + ObjectIdentity('1.3.6.1.6.3.1.1.5.2') + ).addVarBinds( + ('1.3.6.1.6.3.1.1.4.3.0', '1.3.6.1.4.1.20408.4.1.1.2'), + ('1.3.6.1.2.1.1.1.0', OctetString('my system')) + ).loadMibs( + 'SNMPv2-MIB' ) ) +errorIndication, errorStatus, errorIndex, varBinds = next(iterator) + if errorIndication: print(errorIndication) diff --git a/examples/hlapi/v3arch/asyncore/sync/agent/ntforg/send-notification-with-additional-varbinds.py b/examples/hlapi/v3arch/asyncore/sync/agent/ntforg/send-notification-with-additional-varbinds.py index 46f3c5c1..1fd44398 100644 --- a/examples/hlapi/v3arch/asyncore/sync/agent/ntforg/send-notification-with-additional-varbinds.py +++ b/examples/hlapi/v3arch/asyncore/sync/agent/ntforg/send-notification-with-additional-varbinds.py @@ -18,26 +18,28 @@ Functionally similar to: """# from pysnmp.hlapi import * -errorIndication, errorStatus, errorIndex, varBinds = next( - sendNotification( - SnmpEngine(), - CommunityData('public'), - UdpTransportTarget(('demo.snmplabs.com', 162)), - ContextData(), - 'inform', - NotificationType( - ObjectIdentity('SNMPv2-MIB', 'coldStart') - ).addVarBinds( - ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysName', 0), 'my system') - ) +iterator = sendNotification( + SnmpEngine(), + CommunityData('public'), + UdpTransportTarget(('demo.snmplabs.com', 162)), + ContextData(), + 'inform', + NotificationType( + ObjectIdentity('SNMPv2-MIB', 'coldStart') + ).addVarBinds( + ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysName', 0), 'my system') ) ) +errorIndication, errorStatus, errorIndex, varBinds = next(iterator) + 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])) diff --git a/examples/hlapi/v3arch/asyncore/sync/agent/ntforg/v2c-trap-with-notification-objects.py b/examples/hlapi/v3arch/asyncore/sync/agent/ntforg/v2c-trap-with-notification-objects.py index 5681d28c..c53f09e6 100644 --- a/examples/hlapi/v3arch/asyncore/sync/agent/ntforg/v2c-trap-with-notification-objects.py +++ b/examples/hlapi/v3arch/asyncore/sync/agent/ntforg/v2c-trap-with-notification-objects.py @@ -20,22 +20,24 @@ Functionally similar to: """# from pysnmp.hlapi import * -errorIndication, errorStatus, errorIndex, varBinds = next( - sendNotification( - SnmpEngine(), - CommunityData('public'), - UdpTransportTarget(('demo.snmplabs.com', 162)), - ContextData(), - 'trap', - NotificationType( - ObjectIdentity('IF-MIB', 'linkUp'), - instanceIndex=(123,), - objects={('IF-MIB', 'ifIndex'): 123, - ('IF-MIB', 'ifAdminStatus'): 'up', - ('IF-MIB', 'ifOperStatus'): 'up'} - ) +iterator = sendNotification( + SnmpEngine(), + CommunityData('public'), + UdpTransportTarget(('demo.snmplabs.com', 162)), + ContextData(), + 'trap', + NotificationType( + ObjectIdentity('IF-MIB', 'linkUp'), + instanceIndex=(123,), + objects={ + ('IF-MIB', 'ifIndex'): 123, + ('IF-MIB', 'ifAdminStatus'): 'up', + ('IF-MIB', 'ifOperStatus'): 'up' + } ) ) +errorIndication, errorStatus, errorIndex, varBinds = next(iterator) + if errorIndication: print(errorIndication) diff --git a/examples/hlapi/v3arch/asyncore/sync/agent/ntforg/v3-inform.py b/examples/hlapi/v3arch/asyncore/sync/agent/ntforg/v3-inform.py index 6f02024f..fb9df891 100644 --- a/examples/hlapi/v3arch/asyncore/sync/agent/ntforg/v3-inform.py +++ b/examples/hlapi/v3arch/asyncore/sync/agent/ntforg/v3-inform.py @@ -18,28 +18,30 @@ Functionally similar to: """# from pysnmp.hlapi import * -errorIndication, errorStatus, errorIndex, varBinds = next( - sendNotification( - SnmpEngine(), - UsmUserData('usr-md5-des', 'authkey1', 'privkey1'), - UdpTransportTarget(('demo.snmplabs.com', 162)), - ContextData(), - 'inform', - NotificationType( - ObjectIdentity('1.3.6.1.6.3.1.1.5.2') - ).addVarBinds( - ObjectType(ObjectIdentity('1.3.6.1.2.1.1.5.0'), 'system name') - ).loadMibs( - 'SNMPv2-MIB' - ) +iterator = sendNotification( + SnmpEngine(), + UsmUserData('usr-md5-des', 'authkey1', 'privkey1'), + UdpTransportTarget(('demo.snmplabs.com', 162)), + ContextData(), + 'inform', + NotificationType( + ObjectIdentity('1.3.6.1.6.3.1.1.5.2') + ).addVarBinds( + ObjectType(ObjectIdentity('1.3.6.1.2.1.1.5.0'), 'system name') + ).loadMibs( + 'SNMPv2-MIB' ) ) +errorIndication, errorStatus, errorIndex, varBinds = next(iterator) + 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])) diff --git a/examples/hlapi/v3arch/asyncore/sync/agent/ntforg/v3-trap.py b/examples/hlapi/v3arch/asyncore/sync/agent/ntforg/v3-trap.py index 14501fb9..87a53fd6 100644 --- a/examples/hlapi/v3arch/asyncore/sync/agent/ntforg/v3-trap.py +++ b/examples/hlapi/v3arch/asyncore/sync/agent/ntforg/v3-trap.py @@ -24,18 +24,18 @@ Functionally similar to: """# from pysnmp.hlapi import * -errorIndication, errorStatus, errorIndex, varBinds = next( - sendNotification( - SnmpEngine(OctetString(hexValue='8000000001020304')), - UsmUserData('usr-sha-aes128', 'authkey1', 'privkey1', - authProtocol=USM_AUTH_HMAC96_SHA, - privProtocol=USM_PRIV_CFB128_AES), - UdpTransportTarget(('demo.snmplabs.com', 162)), - ContextData(), - 'trap', - NotificationType(ObjectIdentity('SNMPv2-MIB', 'authenticationFailure')) - ) +iterator = sendNotification( + SnmpEngine(OctetString(hexValue='8000000001020304')), + UsmUserData('usr-sha-aes128', 'authkey1', 'privkey1', + authProtocol=USM_AUTH_HMAC96_SHA, + privProtocol=USM_PRIV_CFB128_AES), + UdpTransportTarget(('demo.snmplabs.com', 162)), + ContextData(), + 'trap', + NotificationType(ObjectIdentity('SNMPv2-MIB', 'authenticationFailure')) ) +errorIndication, errorStatus, errorIndex, varBinds = next(iterator) + if errorIndication: print(errorIndication) diff --git a/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/coerce-set-value-to-mib-spec.py b/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/coerce-set-value-to-mib-spec.py index b6fb879e..93ba8fff 100644 --- a/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/coerce-set-value-to-mib-spec.py +++ b/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/coerce-set-value-to-mib-spec.py @@ -16,20 +16,26 @@ Functionally similar to: """# from pysnmp.hlapi import * -errorIndication, errorStatus, errorIndex, varBinds = next( - setCmd(SnmpEngine(), - CommunityData('public'), - UdpTransportTarget(('demo.snmplabs.com', 161)), - ContextData(), - ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysORDescr', 1), - 'new system name')) +iterator = setCmd( + SnmpEngine(), + CommunityData('public'), + UdpTransportTarget(('demo.snmplabs.com', 161)), + ContextData(), + ObjectType( + ObjectIdentity('SNMPv2-MIB', 'sysORDescr', 1), + 'new system name' + ) ) +errorIndication, errorStatus, errorIndex, varBinds = next(iterator) + 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])) diff --git a/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/custom-asn1-mib-search-path.py b/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/custom-asn1-mib-search-path.py index f25fafe4..3c279437 100644 --- a/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/custom-asn1-mib-search-path.py +++ b/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/custom-asn1-mib-search-path.py @@ -17,20 +17,30 @@ Functionally similar to: """# from pysnmp.hlapi import * -errorIndication, errorStatus, errorIndex, varBinds = next( - getCmd(SnmpEngine(), - CommunityData('public'), - UdpTransportTarget(('demo.snmplabs.com', 161)), - ContextData(), - ObjectType(ObjectIdentity('IF-MIB', 'ifInOctets', 1).addAsn1MibSource('file:///usr/share/snmp', - 'http://mibs.snmplabs.com/asn1/@mib@'))) +iterator = getCmd( + SnmpEngine(), + CommunityData('public'), + UdpTransportTarget(('demo.snmplabs.com', 161)), + ContextData(), + ObjectType( + ObjectIdentity( + 'IF-MIB', 'ifInOctets', 1 + ).addAsn1MibSource( + 'file:///usr/share/snmp', + 'http://mibs.snmplabs.com/asn1/@mib@' + ) + ) ) +errorIndication, errorStatus, errorIndex, varBinds = next(iterator) + 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])) diff --git a/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/custom-contextengineid-and-contextname.py b/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/custom-contextengineid-and-contextname.py index 497af602..38871cd8 100644 --- a/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/custom-contextengineid-and-contextname.py +++ b/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/custom-contextengineid-and-contextname.py @@ -19,20 +19,26 @@ Functionally similar to: """# from pysnmp.hlapi import * -errorIndication, errorStatus, errorIndex, varBinds = next( - setCmd(SnmpEngine(), - UsmUserData('usr-md5-none', 'authkey1'), - UdpTransportTarget(('demo.snmplabs.com', 161)), - ContextData(contextEngineId=OctetString(hexValue='80004fb805636c6f75644dab22cc'), - contextName='da761cfc8c94d3aceef4f60f049105ba'), - ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysORDescr', 1), 'new system name')) +iterator = setCmd( + SnmpEngine(), + UsmUserData('usr-md5-none', 'authkey1'), + UdpTransportTarget(('demo.snmplabs.com', 161)), + ContextData( + contextEngineId=OctetString(hexValue='80004fb805636c6f75644dab22cc'), + contextName='da761cfc8c94d3aceef4f60f049105ba' + ), + ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysORDescr', 1), 'new system name') ) +errorIndication, errorStatus, errorIndex, varBinds = next(iterator) + 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])) diff --git a/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/custom-contextengineid.py b/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/custom-contextengineid.py index 6435a5d5..3f50481d 100644 --- a/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/custom-contextengineid.py +++ b/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/custom-contextengineid.py @@ -18,20 +18,24 @@ Functionally similar to: """# from pysnmp.hlapi import * -errorIndication, errorStatus, errorIndex, varBinds = next( - setCmd(SnmpEngine(), - UsmUserData('usr-md5-des', 'authkey1', 'privkey1', - securityEngineId=OctetString(hexValue='80004fb805636c6f75644dab22cc')), - UdpTransportTarget(('demo.snmplabs.com', 161)), - ContextData(), - ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysName', 0), 'new system name')) +iterator = setCmd( + SnmpEngine(), + UsmUserData('usr-md5-des', 'authkey1', 'privkey1', + securityEngineId=OctetString(hexValue='80004fb805636c6f75644dab22cc')), + UdpTransportTarget(('demo.snmplabs.com', 161)), + ContextData(), + ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysName', 0), 'new system name') ) +errorIndication, errorStatus, errorIndex, varBinds = next(iterator) + 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])) diff --git a/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/custom-pysnmp-mibs-search-path.py b/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/custom-pysnmp-mibs-search-path.py index 26b8954a..33d72f1f 100644 --- a/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/custom-pysnmp-mibs-search-path.py +++ b/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/custom-pysnmp-mibs-search-path.py @@ -19,26 +19,35 @@ Functionally similar to: """# from pysnmp.hlapi import * -for (errorIndication, - errorStatus, - errorIndex, - varBinds) in bulkCmd(SnmpEngine(), - UsmUserData('usr-none-none'), - UdpTransportTarget(('demo.snmplabs.com', 161)), - ContextData(), - 0, 50, - ObjectType( - ObjectIdentity('TCP-MIB', 'tcpConnTable').addMibSource('/opt/mibs/pysnmp').addMibSource('python_packaged_mibs') - ), - lexicographicMode=False): +iterator = bulkCmd( + SnmpEngine(), + UsmUserData('usr-none-none'), + UdpTransportTarget(('demo.snmplabs.com', 161)), + ContextData(), + 0, 50, + ObjectType( + ObjectIdentity( + 'TCP-MIB', 'tcpConnTable' + ).addMibSource( + '/opt/mibs/pysnmp' + ).addMibSource( + 'python_packaged_mibs' + ) + ), + lexicographicMode=False +) + +for errorIndication, errorStatus, errorIndex, varBinds in iterator: 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])) diff --git a/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/custom-timeout-and-retries.py b/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/custom-timeout-and-retries.py index d70a322f..9b99693a 100644 --- a/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/custom-timeout-and-retries.py +++ b/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/custom-timeout-and-retries.py @@ -30,21 +30,23 @@ Functionally similar to: """# from pysnmp.hlapi import * -errorIndication, errorStatus, errorIndex, varBinds = next( - getCmd(SnmpEngine(), - CommunityData('public'), - UdpTransportTarget( - ('demo.snmplabs.com', 161), timeout=2.0, retries=0 - ), - ContextData(), - ObjectType(ObjectIdentity('1.3.6.1.2.1.1.1.0'))) +iterator = getCmd( + SnmpEngine(), + CommunityData('public'), + UdpTransportTarget(('demo.snmplabs.com', 161), timeout=2.0, retries=0), + ContextData(), + ObjectType(ObjectIdentity('1.3.6.1.2.1.1.1.0')) ) +errorIndication, errorStatus, errorIndex, varBinds = next(iterator) + 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])) diff --git a/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/custom-v3-security-name.py b/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/custom-v3-security-name.py index 8e541f7b..cadc2c3c 100644 --- a/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/custom-v3-security-name.py +++ b/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/custom-v3-security-name.py @@ -17,19 +17,23 @@ usmUserTable, thus duplicate securityName parameters are possible. """# from pysnmp.hlapi import * -errorIndication, errorStatus, errorIndex, varBinds = next( - getCmd(SnmpEngine(), - UsmUserData('usr-md5-none', 'authkey1', securityName='myuser'), - UdpTransportTarget(('demo.snmplabs.com', 161)), - ContextData(), - ObjectType(ObjectIdentity('1.3.6.1.2.1.1.1.0'))) +iterator = getCmd( + SnmpEngine(), + UsmUserData('usr-md5-none', 'authkey1', securityName='myuser'), + UdpTransportTarget(('demo.snmplabs.com', 161)), + ContextData(), + ObjectType(ObjectIdentity('1.3.6.1.2.1.1.1.0')) ) +errorIndication, errorStatus, errorIndex, varBinds = next(iterator) + 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])) diff --git a/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/fetch-variables-over-ipv6.py b/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/fetch-variables-over-ipv6.py index 9bc76446..9b8f3382 100644 --- a/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/fetch-variables-over-ipv6.py +++ b/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/fetch-variables-over-ipv6.py @@ -15,21 +15,25 @@ Functionally similar to: """# from pysnmp.hlapi import * -errorIndication, errorStatus, errorIndex, varBinds = next( - getCmd(SnmpEngine(), - UsmUserData('usr-md5-des', 'authkey1', 'privkey1'), - Udp6TransportTarget(('::1', 161)), - ContextData(), - ObjectType(ObjectIdentity('1.3.6.1.2.1.1.1.0')), - ObjectType(ObjectIdentity('1.3.6.1.2.1.1.2.0')), - ObjectType(ObjectIdentity('1.3.6.1.2.1.1.3.0'))) +iterator = getCmd( + SnmpEngine(), + UsmUserData('usr-md5-des', 'authkey1', 'privkey1'), + Udp6TransportTarget(('::1', 161)), + ContextData(), + ObjectType(ObjectIdentity('1.3.6.1.2.1.1.1.0')), + ObjectType(ObjectIdentity('1.3.6.1.2.1.1.2.0')), + ObjectType(ObjectIdentity('1.3.6.1.2.1.1.3.0')) ) +errorIndication, errorStatus, errorIndex, varBinds = next(iterator) + 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])) diff --git a/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/get-table-object-by-index.py b/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/get-table-object-by-index.py index ed5d8351..253b9224 100644 --- a/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/get-table-object-by-index.py +++ b/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/get-table-object-by-index.py @@ -17,20 +17,24 @@ Functionally similar to: """# from pysnmp.hlapi import * -errorIndication, errorStatus, errorIndex, varBinds = next( - getCmd(SnmpEngine(), - UsmUserData('usr-none-none'), - UdpTransportTarget(('demo.snmplabs.com', 161)), - ContextData(), - ObjectType(ObjectIdentity('IF-MIB', 'ifInOctets', 1)), - ObjectType(ObjectIdentity('IF-MIB', 'ifOutOctets', 1))) +iterator = getCmd( + SnmpEngine(), + UsmUserData('usr-none-none'), + UdpTransportTarget(('demo.snmplabs.com', 161)), + ContextData(), + ObjectType(ObjectIdentity('IF-MIB', 'ifInOctets', 1)), + ObjectType(ObjectIdentity('IF-MIB', 'ifOutOctets', 1)) ) +errorIndication, errorStatus, errorIndex, varBinds = next(iterator) + 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])) diff --git a/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/get-table-object-by-multiple-indices.py b/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/get-table-object-by-multiple-indices.py index 06302525..733bd9cb 100644 --- a/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/get-table-object-by-multiple-indices.py +++ b/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/get-table-object-by-multiple-indices.py @@ -16,24 +16,34 @@ Functionally similar to: """# from pysnmp.hlapi import * -errorIndication, errorStatus, errorIndex, varBinds = next( - getCmd(SnmpEngine(), - UsmUserData('usr-sha-aes128', 'authkey1', 'privkey1', - authProtocol=USM_AUTH_HMAC96_SHA, - privProtocol=USM_PRIV_CFB128_AES), - UdpTransportTarget(('demo.snmplabs.com', 161)), - ContextData(), - ObjectType(ObjectIdentity('TCP-MIB', - 'tcpConnLocalAddress', - '0.0.0.0', 22, - '0.0.0.0', 0))) +iterator = getCmd( + SnmpEngine(), + UsmUserData( + 'usr-sha-aes128', 'authkey1', 'privkey1', + authProtocol=USM_AUTH_HMAC96_SHA, + privProtocol=USM_PRIV_CFB128_AES + ), + UdpTransportTarget(('demo.snmplabs.com', 161)), + ContextData(), + ObjectType( + ObjectIdentity( + 'TCP-MIB', + 'tcpConnLocalAddress', + '0.0.0.0', 22, + '0.0.0.0', 0 + ) + ) ) +errorIndication, errorStatus, errorIndex, varBinds = next(iterator) + 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])) diff --git a/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/getbulk-fetch-scalar-and-table-variables-over-ipv6.py b/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/getbulk-fetch-scalar-and-table-variables-over-ipv6.py index 499c9b02..261e46f9 100644 --- a/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/getbulk-fetch-scalar-and-table-variables-over-ipv6.py +++ b/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/getbulk-fetch-scalar-and-table-variables-over-ipv6.py @@ -18,25 +18,28 @@ Functionally similar to: """# from pysnmp.hlapi import * -for (errorIndication, - errorStatus, - errorIndex, - varBinds) in bulkCmd(SnmpEngine(), - UsmUserData('usr-md5-des', 'authkey1', 'privkey1'), - Udp6TransportTarget(('::1', 161)), - ContextData(), - 1, 25, - ObjectType(ObjectIdentity('IP-MIB', 'ipAdEntAddr')), - ObjectType(ObjectIdentity('IP-MIB', 'ipAddrEntry')), - lexicographicMode=False): +iterator = bulkCmd( + SnmpEngine(), + UsmUserData('usr-md5-des', 'authkey1', 'privkey1'), + Udp6TransportTarget(('::1', 161)), + ContextData(), + 1, 25, + ObjectType(ObjectIdentity('IP-MIB', 'ipAdEntAddr')), + ObjectType(ObjectIdentity('IP-MIB', 'ipAddrEntry')), + lexicographicMode=False +) + +for errorIndication, errorStatus, errorIndex, varBinds in iterator: 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])) diff --git a/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/getbulk-limit-number-of-packets.py b/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/getbulk-limit-number-of-packets.py index dd94650c..ce114002 100644 --- a/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/getbulk-limit-number-of-packets.py +++ b/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/getbulk-limit-number-of-packets.py @@ -18,24 +18,27 @@ Functionally similar to: """# from pysnmp.hlapi import * -for (errorIndication, - errorStatus, - errorIndex, - varBinds) in bulkCmd(SnmpEngine(), - UsmUserData('usr-none-none'), - UdpTransportTarget(('demo.snmplabs.com', 161)), - ContextData(), - 0, 50, - ObjectType(ObjectIdentity('SNMPv2-MIB', 'system')), - maxCalls=10): +iterator = bulkCmd( + SnmpEngine(), + UsmUserData('usr-none-none'), + UdpTransportTarget(('demo.snmplabs.com', 161)), + ContextData(), + 0, 50, + ObjectType(ObjectIdentity('SNMPv2-MIB', 'system')), + maxCalls=10 +) + +for errorIndication, errorStatus, errorIndex, varBinds in iterator: 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])) diff --git a/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/getbulk-limit-number-of-variables.py b/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/getbulk-limit-number-of-variables.py index fa39086b..c0c355c5 100644 --- a/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/getbulk-limit-number-of-variables.py +++ b/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/getbulk-limit-number-of-variables.py @@ -19,25 +19,28 @@ Functionally similar to: """# from pysnmp.hlapi import * -for (errorIndication, - errorStatus, - errorIndex, - varBinds) in bulkCmd(SnmpEngine(), - CommunityData('public'), - UdpTransportTarget(('demo.snmplabs.com', 161)), - ContextData(), - 0, 25, - ObjectType(ObjectIdentity('1.3.6.1.2.1.2.2')), - ObjectType(ObjectIdentity('1.3.6.1.2.1.2.3')), - lexicographicMode=False, maxRows=20): +iterator = bulkCmd( + SnmpEngine(), + CommunityData('public'), + UdpTransportTarget(('demo.snmplabs.com', 161)), + ContextData(), + 0, 25, + ObjectType(ObjectIdentity('1.3.6.1.2.1.2.2')), + ObjectType(ObjectIdentity('1.3.6.1.2.1.2.3')), + lexicographicMode=False, maxRows=20 +) + +for errorIndication, errorStatus, errorIndex, varBinds in iterator: 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 ])) diff --git a/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/getnext-limit-number-of-variables.py b/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/getnext-limit-number-of-variables.py index d8a2b935..950e9db6 100644 --- a/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/getnext-limit-number-of-variables.py +++ b/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/getnext-limit-number-of-variables.py @@ -18,25 +18,30 @@ Functionally similar to: """# from pysnmp.hlapi import * -for (errorIndication, - errorStatus, - errorIndex, - varBinds) in nextCmd(SnmpEngine(), - UsmUserData('usr-sha-aes128', 'authkey1', 'privkey1', - authProtocol=USM_AUTH_HMAC96_SHA, - privProtocol=USM_PRIV_CFB128_AES), - UdpTransportTarget(('demo.snmplabs.com', 161)), - ContextData(), - ObjectType(ObjectIdentity('SNMPv2-MIB')), - maxRows=100, ignoreNonIncreasingOid=True): +iterator = nextCmd( + SnmpEngine(), + UsmUserData( + 'usr-sha-aes128', 'authkey1', 'privkey1', + authProtocol=USM_AUTH_HMAC96_SHA, + privProtocol=USM_PRIV_CFB128_AES + ), + UdpTransportTarget(('demo.snmplabs.com', 161)), + ContextData(), + ObjectType(ObjectIdentity('SNMPv2-MIB')), + maxRows=100, ignoreNonIncreasingOid=True +) + +for errorIndication, errorStatus, errorIndex, varBinds in iterator: 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])) diff --git a/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/getnext-multiple-oids-to-eom.py b/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/getnext-multiple-oids-to-eom.py index 863ceb6f..0008f4bc 100644 --- a/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/getnext-multiple-oids-to-eom.py +++ b/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/getnext-multiple-oids-to-eom.py @@ -17,24 +17,27 @@ Functionally similar to: """# from pysnmp.hlapi import * -for (errorIndication, - errorStatus, - errorIndex, - varBinds) in nextCmd(SnmpEngine(), - CommunityData('public'), - UdpTransportTarget(('demo.snmplabs.com', 161)), - ContextData(), - ObjectType(ObjectIdentity('1.3.6.1.2.1.2.2.1.2')), - ObjectType(ObjectIdentity('1.3.6.1.2.1.2.2.1.3')), - lexicographicMode=False): +iterator = nextCmd( + SnmpEngine(), + CommunityData('public'), + UdpTransportTarget(('demo.snmplabs.com', 161)), + ContextData(), + ObjectType(ObjectIdentity('1.3.6.1.2.1.2.2.1.2')), + ObjectType(ObjectIdentity('1.3.6.1.2.1.2.2.1.3')), + lexicographicMode=False +) + +for errorIndication, errorStatus, errorIndex, varBinds in iterator: 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 ])) diff --git a/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/multiple-get-calls.py b/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/multiple-get-calls.py index d883d16a..b69f1c6b 100644 --- a/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/multiple-get-calls.py +++ b/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/multiple-get-calls.py @@ -25,20 +25,25 @@ from pysnmp.hlapi import * queue = [[ObjectType(ObjectIdentity('IF-MIB', 'ifInOctets', 1))], [ObjectType(ObjectIdentity('IF-MIB', 'ifOutOctets', 1))]] -iterator = getCmd(SnmpEngine(), - UsmUserData('usr-md5-none', 'authkey1'), - UdpTransportTarget(('demo.snmplabs.com', 161)), - ContextData()) +iterator = getCmd( + SnmpEngine(), + UsmUserData('usr-md5-none', 'authkey1'), + UdpTransportTarget(('demo.snmplabs.com', 161)), + ContextData() +) next(iterator) while queue: errorIndication, errorStatus, errorIndex, varBinds = iterator.send(queue.pop()) + 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])) diff --git a/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/preload-pysnmp-mibs.py b/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/preload-pysnmp-mibs.py index a9bb2bb3..ed752999 100644 --- a/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/preload-pysnmp-mibs.py +++ b/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/preload-pysnmp-mibs.py @@ -17,22 +17,25 @@ Functionally similar to: """# from pysnmp.hlapi import * -for (errorIndication, - errorStatus, - errorIndex, - varBinds) in nextCmd(SnmpEngine(), - UsmUserData('usr-md5-des', 'authkey1', 'privkey1'), - UdpTransportTarget(('demo.snmplabs.com', 161)), - ContextData(), - ObjectType(ObjectIdentity('1.3.6').loadMibs())): +iterator = nextCmd( + SnmpEngine(), + UsmUserData('usr-md5-des', 'authkey1', 'privkey1'), + UdpTransportTarget(('demo.snmplabs.com', 161)), + ContextData(), + ObjectType(ObjectIdentity('1.3.6').loadMibs()) +) + +for errorIndication, errorStatus, errorIndex, varBinds in iterator: 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])) diff --git a/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/pull-whole-mib.py b/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/pull-whole-mib.py index b9103de3..2ac2b318 100644 --- a/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/pull-whole-mib.py +++ b/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/pull-whole-mib.py @@ -16,22 +16,25 @@ Functionally similar to: """# from pysnmp.hlapi import * -for (errorIndication, - errorStatus, - errorIndex, - varBinds) in nextCmd(SnmpEngine(), - UsmUserData('usr-md5-none', 'authkey1'), - UdpTransportTarget(('demo.snmplabs.com', 161)), - ContextData(), - ObjectType(ObjectIdentity('IF-MIB'))): +iterator = nextCmd( + SnmpEngine(), + UsmUserData('usr-md5-none', 'authkey1'), + UdpTransportTarget(('demo.snmplabs.com', 161)), + ContextData(), + ObjectType(ObjectIdentity('IF-MIB')) +) + +for errorIndication, errorStatus, errorIndex, varBinds in iterator: 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])) diff --git a/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/pull-whole-snmp-table.py b/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/pull-whole-snmp-table.py index 91a71a61..260102a5 100644 --- a/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/pull-whole-snmp-table.py +++ b/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/pull-whole-snmp-table.py @@ -17,28 +17,31 @@ Functionally similar to: """# from pysnmp.hlapi import * -for (errorIndication, - errorStatus, - errorIndex, - varBinds) in nextCmd(SnmpEngine(), - CommunityData('public', mpModel=0), - UdpTransportTarget(('demo.snmplabs.com', 161)), - ContextData(), - ObjectType(ObjectIdentity('IF-MIB', 'ifDescr')), - ObjectType(ObjectIdentity('IF-MIB', 'ifType')), - ObjectType(ObjectIdentity('IF-MIB', 'ifMtu')), - ObjectType(ObjectIdentity('IF-MIB', 'ifSpeed')), - ObjectType(ObjectIdentity('IF-MIB', 'ifPhysAddress')), - ObjectType(ObjectIdentity('IF-MIB', 'ifType')), - lexicographicMode=False): +iterator = nextCmd( + SnmpEngine(), + CommunityData('public', mpModel=0), + UdpTransportTarget(('demo.snmplabs.com', 161)), + ContextData(), + ObjectType(ObjectIdentity('IF-MIB', 'ifDescr')), + ObjectType(ObjectIdentity('IF-MIB', 'ifType')), + ObjectType(ObjectIdentity('IF-MIB', 'ifMtu')), + ObjectType(ObjectIdentity('IF-MIB', 'ifSpeed')), + ObjectType(ObjectIdentity('IF-MIB', 'ifPhysAddress')), + ObjectType(ObjectIdentity('IF-MIB', 'ifType')), + lexicographicMode=False +) + +for errorIndication, errorStatus, errorIndex, varBinds in iterator: 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])) diff --git a/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/query-agents-from-multuple-threads-over-ipv4-and-ipv6.py b/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/query-agents-from-multuple-threads-over-ipv4-and-ipv6.py index 16995ed8..049e1be1 100644 --- a/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/query-agents-from-multuple-threads-over-ipv4-and-ipv6.py +++ b/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/query-agents-from-multuple-threads-over-ipv4-and-ipv6.py @@ -27,39 +27,45 @@ else: # 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'), UdpTransportTarget(('localhost', 161)), (ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysContact', 0)), ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysName', 0)))), + # 4-nd target (SNMPv3 over IPv4/UDP) (UsmUserData('usr-md5-des', 'authkey1', 'privkey1'), UdpTransportTarget(('demo.snmplabs.com', 161)), (ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysDescr', 0)), ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysLocation', 0)))), + # 5-th target (SNMPv3 over IPv6/UDP) (UsmUserData('usr-md5-none', 'authkey1'), Udp6TransportTarget(('::1', 161)), (ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysDescr', 0)), ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysLocation', 0)))), + # N-th target # ... ) class Worker(Thread): + def __init__(self, requests, responses): Thread.__init__(self) self.snmpEngine = SnmpEngine() @@ -71,15 +77,17 @@ class Worker(Thread): def run(self): while True: authData, transportTarget, varBinds = self.requests.get() + self.responses.append( next(getCmd(self.snmpEngine, authData, transportTarget, ContextData(), *varBinds)) ) - if hasattr(self.requests, 'task_done'): # 2.5+ - self.requests.task_done() + + self.requests.task_done() class ThreadPool(object): + def __init__(self, num_threads): self.requests = Queue(num_threads) self.responses = [] @@ -106,7 +114,7 @@ class ThreadPool(object): pool = ThreadPool(3) # Submit GET requests -for authData, transportTarget, varBinds in targets: +for authData, transportTarget, varBinds in TARGETS: pool.addRequest(authData, transportTarget, varBinds) # Wait for responses or errors @@ -114,11 +122,14 @@ pool.waitCompletion() # Walk through responses for errorIndication, errorStatus, errorIndex, varBinds in pool.getResponses(): + 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])) diff --git a/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/set-multiple-scalar-values.py b/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/set-multiple-scalar-values.py index be4ffeab..a5eeb1a3 100644 --- a/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/set-multiple-scalar-values.py +++ b/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/set-multiple-scalar-values.py @@ -20,24 +20,34 @@ Functionally similar to: """# from pysnmp.hlapi import * -errorIndication, errorStatus, errorIndex, varBinds = next( - setCmd(SnmpEngine(), - CommunityData('public', mpModel=0), - UdpTransportTarget(('demo.snmplabs.com', 161)), - ContextData(), - ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysORID', 1), - ObjectIdentifier('1.3.6.1.4.1.20408.1.1')), - ObjectType(ObjectIdentity('1.3.6.1.2.1.1.9.1.2.1'), - '1.3.6.1.4.1.20408.1.1'), - ObjectType(ObjectIdentity('1.3.6.1.2.1.1.9.1.3.1'), - OctetString('new system name'))) +iterator = setCmd( + SnmpEngine(), + CommunityData('public', mpModel=0), + UdpTransportTarget(('demo.snmplabs.com', 161)), + ContextData(), + ObjectType( + ObjectIdentity('SNMPv2-MIB', 'sysORID', 1), + ObjectIdentifier('1.3.6.1.4.1.20408.1.1') + ), + ObjectType( + ObjectIdentity('1.3.6.1.2.1.1.9.1.2.1'), + '1.3.6.1.4.1.20408.1.1' + ), + ObjectType( + ObjectIdentity('1.3.6.1.2.1.1.9.1.3.1'), + OctetString('new system name') + ) ) +errorIndication, errorStatus, errorIndex, varBinds = next(iterator) + 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])) diff --git a/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/specific-v3-engine-id.py b/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/specific-v3-engine-id.py index 94f591c4..36cfc050 100644 --- a/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/specific-v3-engine-id.py +++ b/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/specific-v3-engine-id.py @@ -65,19 +65,23 @@ print('Remote securityEngineId = %s' % securityEngineId.prettyPrint()) authData = UsmUserData('usr-md5-none', 'authkey1', securityEngineId=securityEngineId) -errorIndication, errorStatus, errorIndex, varBinds = next( - getCmd(snmpEngine, - authData, - transportTarget, - ContextData(), - ObjectType(ObjectIdentity('1.3.6.1.2.1.1.1.0'))) +iterator = getCmd( + snmpEngine, + authData, + transportTarget, + ContextData(), + ObjectType(ObjectIdentity('1.3.6.1.2.1.1.1.0')) ) +errorIndication, errorStatus, errorIndex, varBinds = next(iterator) + if errorIndication: print(errorIndication) + elif errorStatus: print('%s at %s' % (errorStatus.prettyPrint(), errorIndex and varBinds[int(errorIndex) - 1][0] or '?')) + else: for name, val in varBinds: print('%s = %s' % (name.prettyPrint(), val.prettyPrint())) diff --git a/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/usm-md5-des.py b/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/usm-md5-des.py index 27c0e7fb..6254418c 100644 --- a/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/usm-md5-des.py +++ b/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/usm-md5-des.py @@ -16,19 +16,23 @@ Functionally similar to: """# from pysnmp.hlapi import * -errorIndication, errorStatus, errorIndex, varBinds = next( - getCmd(SnmpEngine(), - UsmUserData('usr-md5-des', 'authkey1', 'privkey1'), - UdpTransportTarget(('demo.snmplabs.com', 161)), - ContextData(), - ObjectType(ObjectIdentity('IF-MIB', 'ifInOctets', 1))) +iterator = getCmd( + SnmpEngine(), + UsmUserData('usr-md5-des', 'authkey1', 'privkey1'), + UdpTransportTarget(('demo.snmplabs.com', 161)), + ContextData(), + ObjectType(ObjectIdentity('IF-MIB', 'ifInOctets', 1)) ) +errorIndication, errorStatus, errorIndex, varBinds = next(iterator) + 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])) diff --git a/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/usm-md5-none.py b/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/usm-md5-none.py index f06964e2..724910b5 100644 --- a/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/usm-md5-none.py +++ b/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/usm-md5-none.py @@ -16,19 +16,23 @@ Functionally similar to: """# from pysnmp.hlapi import * -errorIndication, errorStatus, errorIndex, varBinds = next( - getCmd(SnmpEngine(), - UsmUserData('usr-md5-none', 'authkey1'), - UdpTransportTarget(('demo.snmplabs.com', 161)), - ContextData(), - ObjectType(ObjectIdentity('IF-MIB', 'ifInOctets', 1))) +iterator = getCmd( + SnmpEngine(), + UsmUserData('usr-md5-none', 'authkey1'), + UdpTransportTarget(('demo.snmplabs.com', 161)), + ContextData(), + ObjectType(ObjectIdentity('IF-MIB', 'ifInOctets', 1)) ) +errorIndication, errorStatus, errorIndex, varBinds = next(iterator) + 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])) diff --git a/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/usm-none-none.py b/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/usm-none-none.py index 940d8817..8a558920 100644 --- a/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/usm-none-none.py +++ b/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/usm-none-none.py @@ -16,18 +16,22 @@ Functionally similar to: """# from pysnmp.hlapi import * -errorIndication, errorStatus, errorIndex, varBinds = next( - getCmd(SnmpEngine(), - UsmUserData('usr-none-none'), - UdpTransportTarget(('demo.snmplabs.com', 161)), - ContextData(), - ObjectType(ObjectIdentity('IF-MIB', 'ifInOctets', 1))) +iterator = getCmd( + SnmpEngine(), + UsmUserData('usr-none-none'), + UdpTransportTarget(('demo.snmplabs.com', 161)), + ContextData(), + ObjectType(ObjectIdentity('IF-MIB', 'ifInOctets', 1)) ) +errorIndication, errorStatus, errorIndex, varBinds = next(iterator) + 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])) diff --git a/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/usm-sha-aes128.py b/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/usm-sha-aes128.py index 69262086..e30e8239 100644 --- a/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/usm-sha-aes128.py +++ b/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/usm-sha-aes128.py @@ -35,21 +35,25 @@ Functionally similar to: """# from pysnmp.hlapi import * -errorIndication, errorStatus, errorIndex, varBinds = next( - getCmd(SnmpEngine(), - UsmUserData('usr-sha-aes', 'authkey1', 'privkey1', - authProtocol=USM_AUTH_HMAC96_SHA, - privProtocol=USM_PRIV_CFB128_AES), - UdpTransportTarget(('demo.snmplabs.com', 161)), - ContextData(), - ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysDescr', 0))) +iterator = getCmd( + SnmpEngine(), + UsmUserData('usr-sha-aes', 'authkey1', 'privkey1', + authProtocol=USM_AUTH_HMAC96_SHA, + privProtocol=USM_PRIV_CFB128_AES), + UdpTransportTarget(('demo.snmplabs.com', 161)), + ContextData(), + ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysDescr', 0)) ) +errorIndication, errorStatus, errorIndex, varBinds = next(iterator) + 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])) diff --git a/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/v1-get.py b/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/v1-get.py index 1b272967..55c49735 100644 --- a/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/v1-get.py +++ b/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/v1-get.py @@ -16,19 +16,23 @@ Functionally similar to: """# from pysnmp.hlapi import * -errorIndication, errorStatus, errorIndex, varBinds = next( - getCmd(SnmpEngine(), - CommunityData('public', mpModel=0), - UdpTransportTarget(('demo.snmplabs.com', 161)), - ContextData(), - ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysDescr', 0))) +iterator = getCmd( + SnmpEngine(), + CommunityData('public', mpModel=0), + UdpTransportTarget(('demo.snmplabs.com', 161)), + ContextData(), + ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysDescr', 0)) ) +errorIndication, errorStatus, errorIndex, varBinds = next(iterator) + 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])) diff --git a/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/v2c-get.py b/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/v2c-get.py index 606bab5b..d7676e32 100644 --- a/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/v2c-get.py +++ b/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/v2c-get.py @@ -16,20 +16,24 @@ Functionally similar to: """# from pysnmp.hlapi import * -errorIndication, errorStatus, errorIndex, varBinds = next( - getCmd(SnmpEngine(), - CommunityData('public'), - UdpTransportTarget(('demo.snmplabs.com', 161)), - ContextData(), - ObjectType(ObjectIdentity('1.3.6.1.2.1.1.1.0')), - ObjectType(ObjectIdentity('1.3.6.1.2.1.1.6.0'))) +iterator = getCmd( + SnmpEngine(), + CommunityData('public'), + UdpTransportTarget(('demo.snmplabs.com', 161)), + ContextData(), + ObjectType(ObjectIdentity('1.3.6.1.2.1.1.1.0')), + ObjectType(ObjectIdentity('1.3.6.1.2.1.1.6.0')) ) +errorIndication, errorStatus, errorIndex, varBinds = next(iterator) + 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])) diff --git a/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/v2c-getbulk.py b/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/v2c-getbulk.py index 47715195..15f684f5 100644 --- a/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/v2c-getbulk.py +++ b/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/v2c-getbulk.py @@ -18,25 +18,28 @@ Functionally similar to: """# from pysnmp.hlapi import * -for (errorIndication, - errorStatus, - errorIndex, - varBinds) in bulkCmd(SnmpEngine(), - CommunityData('public'), - UdpTransportTarget(('demo.snmplabs.com', 161)), - ContextData(), - 0, 25, - ObjectType(ObjectIdentity('1.3.6.1.2.1.2.2')), - ObjectType(ObjectIdentity('1.3.6.1.2.1.2.3')), - lexicographicMode=False): +iterator = bulkCmd( + SnmpEngine(), + CommunityData('public'), + UdpTransportTarget(('demo.snmplabs.com', 161)), + ContextData(), + 0, 25, + ObjectType(ObjectIdentity('1.3.6.1.2.1.2.2')), + ObjectType(ObjectIdentity('1.3.6.1.2.1.2.3')), + lexicographicMode=False +) + +for errorIndication, errorStatus, errorIndex, varBinds in iterator: 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])) diff --git a/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/waive-mib-lookup.py b/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/waive-mib-lookup.py index f44d160b..c22d7b19 100644 --- a/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/waive-mib-lookup.py +++ b/examples/hlapi/v3arch/asyncore/sync/manager/cmdgen/waive-mib-lookup.py @@ -20,23 +20,26 @@ Functionally similar to: """# from pysnmp.hlapi import * -for (errorIndication, - errorStatus, - errorIndex, - varBinds) in nextCmd(SnmpEngine(), - CommunityData('public'), - UdpTransportTarget(('demo.snmplabs.com', 161)), - ContextData(), - ObjectType(ObjectIdentity('1.3.6.1.2.1.1')), - lookupMib=False): +iterator = nextCmd( + SnmpEngine(), + CommunityData('public'), + UdpTransportTarget(('demo.snmplabs.com', 161)), + ContextData(), + ObjectType(ObjectIdentity('1.3.6.1.2.1.1')), + lookupMib=False +) + +for errorIndication, errorStatus, errorIndex, varBinds in iterator: 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])) diff --git a/examples/hlapi/v3arch/trollius/agent/ntforg/default-v1-trap.py b/examples/hlapi/v3arch/trollius/agent/ntforg/default-v1-trap.py index 64e61b1d..1e74aa55 100644 --- a/examples/hlapi/v3arch/trollius/agent/ntforg/default-v1-trap.py +++ b/examples/hlapi/v3arch/trollius/agent/ntforg/default-v1-trap.py @@ -28,32 +28,33 @@ from pysnmp.hlapi.v3arch.asyncio import * @trollius.coroutine def run(): snmpEngine = SnmpEngine() - (errorIndication, - errorStatus, - errorIndex, - varBinds) = yield trollius.From( - sendNotification( - snmpEngine, - CommunityData('public'), # mpModel=0), - UdpTransportTarget(('demo.snmplabs.com', 162)), - ContextData(), - 'inform', - NotificationType( - ObjectIdentity('1.3.6.1.6.3.1.1.5.2') - ).loadMibs( - 'SNMPv2-MIB' - ).addVarBinds( - ('1.3.6.1.6.3.1.1.4.3.0', '1.3.6.1.4.1.20408.4.1.1.2'), - ('1.3.6.1.2.1.1.1.0', OctetString('my system')) - ) + + iterator = sendNotification( + snmpEngine, + CommunityData('public'), # mpModel=0), + UdpTransportTarget(('demo.snmplabs.com', 162)), + ContextData(), + 'inform', + NotificationType( + ObjectIdentity('1.3.6.1.6.3.1.1.5.2') + ).loadMibs( + 'SNMPv2-MIB' + ).addVarBinds( + ('1.3.6.1.6.3.1.1.4.3.0', '1.3.6.1.4.1.20408.4.1.1.2'), + ('1.3.6.1.2.1.1.1.0', OctetString('my system')) ) ) + (errorIndication, errorStatus, + errorIndex, varBinds) = yield trollius.From(iterator) + 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])) diff --git a/examples/hlapi/v3arch/trollius/manager/cmdgen/getbulk-to-eom.py b/examples/hlapi/v3arch/trollius/manager/cmdgen/getbulk-to-eom.py index f084e398..50a77395 100644 --- a/examples/hlapi/v3arch/trollius/manager/cmdgen/getbulk-to-eom.py +++ b/examples/hlapi/v3arch/trollius/manager/cmdgen/getbulk-to-eom.py @@ -22,32 +22,38 @@ from pysnmp.hlapi.v3arch.asyncio import * @trollius.coroutine def run(varBinds): + snmpEngine = SnmpEngine() + while True: - (errorIndication, - errorStatus, - errorIndex, - varBindTable) = yield trollius.From( - bulkCmd(snmpEngine, - UsmUserData('usr-none-none'), - UdpTransportTarget(('demo.snmplabs.com', 161)), - ContextData(), - 0, 50, - *varBinds) + + iterator = bulkCmd( + snmpEngine, + UsmUserData('usr-none-none'), + UdpTransportTarget(('demo.snmplabs.com', 161)), + ContextData(), + 0, 50, + *varBinds ) + (errorIndication, errorStatus, + errorIndex, varBindTable) = yield trollius.From(iterator) + if errorIndication: print(errorIndication) break + elif errorStatus: print('%s at %s' % (errorStatus.prettyPrint(), errorIndex and varBinds[int(errorIndex) - 1][0] or '?')) + else: for varBindRow in varBindTable: for varBind in varBindRow: print(' = '.join([x.prettyPrint() for x in varBind])) varBinds = varBindTable[-1] + if isEndOfMib(varBinds): break diff --git a/examples/hlapi/v3arch/trollius/manager/cmdgen/v1-get.py b/examples/hlapi/v3arch/trollius/manager/cmdgen/v1-get.py index 771e48c6..1e17f34e 100644 --- a/examples/hlapi/v3arch/trollius/manager/cmdgen/v1-get.py +++ b/examples/hlapi/v3arch/trollius/manager/cmdgen/v1-get.py @@ -21,23 +21,27 @@ from pysnmp.hlapi.v3arch.asyncio import * @trollius.coroutine def run(): + snmpEngine = SnmpEngine() - (errorIndication, - errorStatus, - errorIndex, - varBinds) = yield trollius.From( - getCmd(snmpEngine, - CommunityData('public', mpModel=0), - UdpTransportTarget(('demo.snmplabs.com', 161)), - ContextData(), - ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysDescr', 0))) + + iterator = getCmd( + snmpEngine, + CommunityData('public', mpModel=0), + UdpTransportTarget(('demo.snmplabs.com', 161)), + ContextData(), + ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysDescr', 0)) ) + (errorIndication, errorStatus, + errorIndex, varBinds) = yield trollius.From(iterator) + 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])) diff --git a/examples/hlapi/v3arch/twisted/agent/ntforg/default-v1-trap.py b/examples/hlapi/v3arch/twisted/agent/ntforg/default-v1-trap.py index 756ce58d..bf409536 100644 --- a/examples/hlapi/v3arch/twisted/agent/ntforg/default-v1-trap.py +++ b/examples/hlapi/v3arch/twisted/agent/ntforg/default-v1-trap.py @@ -25,7 +25,7 @@ from pysnmp.hlapi.v3arch.twisted import * def success(args, hostname): - (errorStatus, errorIndex, varBinds) = args + errorStatus, errorIndex, varBinds = args if errorStatus: print('%s: %s at %s' % ( @@ -34,6 +34,7 @@ def success(args, hostname): errorIndex and varBinds[int(errorIndex) - 1][0] or '?' ) ) + else: for varBind in varBinds: print(' = '.join([x.prettyPrint() for x in varBind])) @@ -45,7 +46,8 @@ def failure(errorIndication, hostname): # noinspection PyUnusedLocal def run(reactor, hostname): - d = sendNotification( + + deferred = sendNotification( SnmpEngine(), CommunityData('public', mpModel=0), UdpTransportTarget((hostname, 162)), @@ -60,8 +62,10 @@ def run(reactor, hostname): ('1.3.6.1.2.1.1.1.0', OctetString('my system')) ) ) - d.addCallback(success, hostname).addErrback(failure, hostname) - return d + + deferred.addCallback(success, hostname).addErrback(failure, hostname) + + return deferred react(run, ['demo.snmplabs.com']) diff --git a/examples/hlapi/v3arch/twisted/agent/ntforg/multiple-notifications-at-once.py b/examples/hlapi/v3arch/twisted/agent/ntforg/multiple-notifications-at-once.py index c234a827..29f474a5 100644 --- a/examples/hlapi/v3arch/twisted/agent/ntforg/multiple-notifications-at-once.py +++ b/examples/hlapi/v3arch/twisted/agent/ntforg/multiple-notifications-at-once.py @@ -28,12 +28,13 @@ from pysnmp.hlapi.v3arch.twisted import * def success(args, hostname): - (errorStatus, errorIndex, varBinds) = args + errorStatus, errorIndex, varBinds = args if errorStatus: print('%s: %s at %s' % (hostname, errorStatus.prettyPrint(), errorIndex and varBinds[int(errorIndex) - 1][0] or '?')) + else: for varBind in varBinds: print(' = '.join([x.prettyPrint() for x in varBind])) @@ -45,7 +46,8 @@ def failure(errorIndication, hostname): # noinspection PyUnusedLocal def sendone(reactor, snmpEngine, hostname, notifyType): - d = sendNotification( + + deferred = sendNotification( snmpEngine, CommunityData('public', tag=hostname), UdpTransportTarget((hostname, 162), tagList=hostname), @@ -60,8 +62,10 @@ def sendone(reactor, snmpEngine, hostname, notifyType): ('1.3.6.1.2.1.1.1.0', OctetString('my system')) ) ) - d.addCallback(success, hostname).addErrback(failure, hostname) - return d + + deferred.addCallback(success, hostname).addErrback(failure, hostname) + + return deferred def sendall(reactor, destinations): diff --git a/examples/hlapi/v3arch/twisted/agent/ntforg/v2c-trap-inline-callbacks.py b/examples/hlapi/v3arch/twisted/agent/ntforg/v2c-trap-inline-callbacks.py index 39b15c57..6e831582 100644 --- a/examples/hlapi/v3arch/twisted/agent/ntforg/v2c-trap-inline-callbacks.py +++ b/examples/hlapi/v3arch/twisted/agent/ntforg/v2c-trap-inline-callbacks.py @@ -24,7 +24,7 @@ from pysnmp.hlapi.v3arch.twisted import * @defer.inlineCallbacks def sendtrap(reactor, snmpEngine, hostname): - yield sendNotification( + deferred = sendNotification( snmpEngine, CommunityData('public', mpModel=0), UdpTransportTarget((hostname, 162)), @@ -39,8 +39,10 @@ def sendtrap(reactor, snmpEngine, hostname): ) ) + yield deferred + + # Preserve SnmpEngine instance across [potentially] multiple calls to safe on initialization snmpEngine = SnmpEngine() react(sendtrap, [snmpEngine, 'demo.snmplabs.com']) - diff --git a/examples/hlapi/v3arch/twisted/manager/cmdgen/custom-timeout-and-retries.py b/examples/hlapi/v3arch/twisted/manager/cmdgen/custom-timeout-and-retries.py index 9c8c6eb9..96a93749 100644 --- a/examples/hlapi/v3arch/twisted/manager/cmdgen/custom-timeout-and-retries.py +++ b/examples/hlapi/v3arch/twisted/manager/cmdgen/custom-timeout-and-retries.py @@ -20,12 +20,13 @@ from pysnmp.hlapi.v3arch.twisted import * def success(args, hostname): - (errorStatus, errorIndex, varBinds) = args + errorStatus, errorIndex, varBinds = args if errorStatus: print('%s: %s at %s' % (hostname, errorStatus.prettyPrint(), errorIndex and varBinds[int(errorIndex) - 1][0] or '?')) + else: for varBind in varBinds: print(' = '.join([x.prettyPrint() for x in varBind])) @@ -37,17 +38,20 @@ def failure(errorIndication, hostname): # noinspection PyUnusedLocal def getSysDescr(reactor, hostname): + snmpEngine = SnmpEngine() - d = getCmd(snmpEngine, - CommunityData('public'), - UdpTransportTarget((hostname, 161), timeout=2.0, retries=0), - ContextData(), - ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysDescr', 0))) + deferred = getCmd( + snmpEngine, + CommunityData('public'), + UdpTransportTarget((hostname, 161), timeout=2.0, retries=0), + ContextData(), + ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysDescr', 0)) + ) - d.addCallback(success, hostname).addErrback(failure, hostname) + deferred.addCallback(success, hostname).addErrback(failure, hostname) - return d + return deferred react(getSysDescr, ['demo.snmplabs.com']) diff --git a/examples/hlapi/v3arch/twisted/manager/cmdgen/getbulk-to-eom.py b/examples/hlapi/v3arch/twisted/manager/cmdgen/getbulk-to-eom.py index 89542793..e558fe89 100644 --- a/examples/hlapi/v3arch/twisted/manager/cmdgen/getbulk-to-eom.py +++ b/examples/hlapi/v3arch/twisted/manager/cmdgen/getbulk-to-eom.py @@ -21,12 +21,13 @@ from pysnmp.hlapi.v3arch.twisted import * def success(args, reactor, snmpEngine): - (errorStatus, errorIndex, varBindTable) = args + errorStatus, errorIndex, varBindTable = args if errorStatus: print('%s: %s at %s' % (hostname, errorStatus.prettyPrint(), errorIndex and varBindTable[0][int(errorIndex) - 1][0] or '?')) + else: for varBindRow in varBindTable: for varBind in varBindRow: @@ -41,14 +42,18 @@ def failure(errorIndication): def getbulk(reactor, snmpEngine, varBinds): - d = bulkCmd(snmpEngine, - UsmUserData('usr-none-none'), - UdpTransportTarget(('demo.snmplabs.com', 161)), - ContextData(), - 0, 50, - varBinds) - d.addCallback(success, reactor, snmpEngine).addErrback(failure) - return d + deferred = bulkCmd( + snmpEngine, + UsmUserData('usr-none-none'), + UdpTransportTarget(('demo.snmplabs.com', 161)), + ContextData(), + 0, 50, + varBinds + ) + + deferred.addCallback(success, reactor, snmpEngine).addErrback(failure) + + return deferred react(getbulk, [SnmpEngine(), ObjectType(ObjectIdentity('SNMPv2-MIB', 'system'))]) diff --git a/examples/hlapi/v3arch/twisted/manager/cmdgen/multiple-concurrent-queries.py b/examples/hlapi/v3arch/twisted/manager/cmdgen/multiple-concurrent-queries.py index 90561015..4e1b3754 100644 --- a/examples/hlapi/v3arch/twisted/manager/cmdgen/multiple-concurrent-queries.py +++ b/examples/hlapi/v3arch/twisted/manager/cmdgen/multiple-concurrent-queries.py @@ -29,6 +29,7 @@ def success(args, hostname): print('%s: %s at %s' % (hostname, errorStatus.prettyPrint(), errorIndex and varBinds[int(errorIndex) - 1][0] or '?')) + else: for varBind in varBinds: print(' = '.join([x.prettyPrint() for x in varBind])) @@ -43,13 +44,17 @@ def getSystem(reactor, hostname): snmpEngine = SnmpEngine() def getScalar(objectType): - d = getCmd(snmpEngine, - CommunityData('public', mpModel=0), - UdpTransportTarget((hostname, 161)), - ContextData(), - objectType) - d.addCallback(success, hostname).addErrback(failure, hostname) - return d + deferred = getCmd( + snmpEngine, + CommunityData('public', mpModel=0), + UdpTransportTarget((hostname, 161)), + ContextData(), + objectType + ) + + deferred.addCallback(success, hostname).addErrback(failure, hostname) + + return deferred return DeferredList( [getScalar(ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysDescr', 0))), diff --git a/examples/hlapi/v3arch/twisted/manager/cmdgen/pull-mibs-from-multiple-agents-at-once.py b/examples/hlapi/v3arch/twisted/manager/cmdgen/pull-mibs-from-multiple-agents-at-once.py index 2e501262..e7776a34 100644 --- a/examples/hlapi/v3arch/twisted/manager/cmdgen/pull-mibs-from-multiple-agents-at-once.py +++ b/examples/hlapi/v3arch/twisted/manager/cmdgen/pull-mibs-from-multiple-agents-at-once.py @@ -21,12 +21,13 @@ from pysnmp.hlapi.v3arch.twisted import * def success(args, reactor, snmpEngine, hostname): - (errorStatus, errorIndex, varBindTable) = args + errorStatus, errorIndex, varBindTable = args if errorStatus: print('%s: %s at %s' % (hostname, errorStatus.prettyPrint(), errorIndex and varBindTable[0][int(errorIndex) - 1][0] or '?')) + else: for varBindRow in varBindTable: for varBind in varBindRow: @@ -41,17 +42,22 @@ def failure(errorIndication): def getbulk(reactor, snmpEngine, hostname, varBinds): - d = bulkCmd(snmpEngine, - UsmUserData('usr-md5-none', 'authkey1'), - UdpTransportTarget(hostname), - ContextData(), - 0, 25, - varBinds) - d.addCallback(success, reactor, snmpEngine, hostname).addErrback(failure) - return d + deferred = bulkCmd( + snmpEngine, + UsmUserData('usr-md5-none', 'authkey1'), + UdpTransportTarget(hostname), + ContextData(), + 0, 25, + varBinds + ) + + deferred.addCallback(success, reactor, snmpEngine, hostname).addErrback(failure) + + return deferred def getall(reactor, hostnames): + snmpEngine = SnmpEngine() return DeferredList( diff --git a/examples/hlapi/v3arch/twisted/manager/cmdgen/pull-whole-mib.py b/examples/hlapi/v3arch/twisted/manager/cmdgen/pull-whole-mib.py index 9e3f1791..ee1f376f 100644 --- a/examples/hlapi/v3arch/twisted/manager/cmdgen/pull-whole-mib.py +++ b/examples/hlapi/v3arch/twisted/manager/cmdgen/pull-whole-mib.py @@ -26,6 +26,7 @@ def success(args, reactor, snmpEngine): print('%s: %s at %s' % (hostname, errorStatus.prettyPrint(), errorIndex and varBindTable[0][int(errorIndex) - 1][0] or '?')) + else: for varBindRow in varBindTable: for varBind in varBindRow: @@ -40,13 +41,17 @@ def failure(errorIndication): def getnext(reactor, snmpEngine, varBinds): - d = nextCmd(snmpEngine, - UsmUserData('usr-md5-none', 'authkey1'), - UdpTransportTarget(('demo.snmplabs.com', 161)), - ContextData(), - varBinds) - d.addCallback(success, reactor, snmpEngine).addErrback(failure) - return d + deferred = nextCmd( + snmpEngine, + UsmUserData('usr-md5-none', 'authkey1'), + UdpTransportTarget(('demo.snmplabs.com', 161)), + ContextData(), + varBinds + ) + + deferred.addCallback(success, reactor, snmpEngine).addErrback(failure) + + return deferred react(getnext, [SnmpEngine(), ObjectType(ObjectIdentity('SNMPv2-MIB', 'system'))]) diff --git a/examples/hlapi/v3arch/twisted/manager/cmdgen/v1-get.py b/examples/hlapi/v3arch/twisted/manager/cmdgen/v1-get.py index e483354d..735e146f 100644 --- a/examples/hlapi/v3arch/twisted/manager/cmdgen/v1-get.py +++ b/examples/hlapi/v3arch/twisted/manager/cmdgen/v1-get.py @@ -20,12 +20,13 @@ from pysnmp.hlapi.v3arch.twisted import * def success(args, hostname): - (errorStatus, errorIndex, varBinds) = args + errorStatus, errorIndex, varBinds = args if errorStatus: print('%s: %s at %s' % (hostname, errorStatus.prettyPrint(), errorIndex and varBinds[int(errorIndex) - 1][0] or '?')) + else: for varBind in varBinds: print(' = '.join([x.prettyPrint() for x in varBind])) @@ -37,15 +38,17 @@ def failure(errorIndication, hostname): # noinspection PyUnusedLocal def getSysDescr(reactor, hostname): - d = getCmd(SnmpEngine(), - CommunityData('public', mpModel=0), - UdpTransportTarget((hostname, 161)), - ContextData(), - ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysDescr', 0))) + deferred = getCmd( + SnmpEngine(), + CommunityData('public', mpModel=0), + UdpTransportTarget((hostname, 161)), + ContextData(), + ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysDescr', 0)) + ) - d.addCallback(success, hostname).addErrback(failure, hostname) + deferred.addCallback(success, hostname).addErrback(failure, hostname) - return d + return deferred react(getSysDescr, ['demo.snmplabs.com']) |