diff options
author | elie <elie> | 2015-07-06 21:30:04 +0000 |
---|---|---|
committer | elie <elie> | 2015-07-06 21:30:04 +0000 |
commit | f308702c7867aa3cfb778a4ed6bd0b15c118073d (patch) | |
tree | 0087c2861fe78fbb7b52042310b7bd1c61785886 /examples/hlapi/asyncore/sync/manager/cmdgen | |
parent | 51841e9cba10f8d1dbaf2d1a7e8ecef0df3ce366 (diff) | |
download | pysnmp-git-f308702c7867aa3cfb778a4ed6bd0b15c118073d.tar.gz |
synchronous oneliner apps redesigned to offer Python generator-based
API along with more comprehensive set of accepted parameters
Diffstat (limited to 'examples/hlapi/asyncore/sync/manager/cmdgen')
23 files changed, 545 insertions, 532 deletions
diff --git a/examples/hlapi/asyncore/sync/manager/cmdgen/coerce-set-value-to-mib-spec.py b/examples/hlapi/asyncore/sync/manager/cmdgen/coerce-set-value-to-mib-spec.py index 6732ec29..46d4c5ec 100644 --- a/examples/hlapi/asyncore/sync/manager/cmdgen/coerce-set-value-to-mib-spec.py +++ b/examples/hlapi/asyncore/sync/manager/cmdgen/coerce-set-value-to-mib-spec.py @@ -8,29 +8,29 @@ # * to an Agent at demo.snmplabs.com:161 # * setting SNMPv2-MIB::sysName.0 to new value (type taken from MIB) # -from pysnmp.entity.rfc3413.oneliner import cmdgen +from pysnmp.entity.rfc3413.oneliner.cmdgen import * -cmdGen = cmdgen.CommandGenerator() - -errorIndication, errorStatus, errorIndex, varBinds = cmdGen.setCmd( - cmdgen.CommunityData('public'), - cmdgen.UdpTransportTarget(('demo.snmplabs.com', 161)), - cmdgen.ObjectType( - cmdgen.ObjectIdentity('SNMPv2-MIB', 'sysORDescr', 1), - 'new system name' - ) -) - -# Check for errors and print out results -if errorIndication: - print(errorIndication) -else: - if errorStatus: - print('%s at %s' % ( - errorStatus.prettyPrint(), - errorIndex and varBinds[int(errorIndex)-1][0] or '?' - ) - ) +for errorIndication, \ + errorStatus, errorIndex, \ + varBinds in setCmd(SnmpEngine(), + CommunityData('public'), + UdpTransportTarget(('demo.snmplabs.com', 161)), + ContextData(), + ObjectType( + ObjectIdentity('SNMPv2-MIB', 'sysORDescr', 1), + 'new system name' + )): + # Check for errors and print out results + if errorIndication: + print(errorIndication) else: - for name, val in varBinds: - print('%s = %s' % (name.prettyPrint(), val.prettyPrint())) + if 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 ])) + break diff --git a/examples/hlapi/asyncore/sync/manager/cmdgen/custom-asn1-mib-search-path.py b/examples/hlapi/asyncore/sync/manager/cmdgen/custom-asn1-mib-search-path.py index 9c2db805..fba316f5 100644 --- a/examples/hlapi/asyncore/sync/manager/cmdgen/custom-asn1-mib-search-path.py +++ b/examples/hlapi/asyncore/sync/manager/cmdgen/custom-asn1-mib-search-path.py @@ -9,27 +9,29 @@ # * for IF-MIB::ifInOctets.1 MIB object # * Pass attached MIB compiler non-default ASN.1 MIB source # -from pysnmp.entity.rfc3413.oneliner import cmdgen +from pysnmp.entity.rfc3413.oneliner.cmdgen import * -cmdGen = cmdgen.CommandGenerator() - -errorIndication, errorStatus, errorIndex, varBinds = cmdGen.getCmd( - cmdgen.CommunityData('public'), - cmdgen.UdpTransportTarget(('demo.snmplabs.com', 161)), - cmdgen.ObjectIdentity('IF-MIB', 'ifInOctets', 1).addAsn1MibSource('file:///usr/share/snmp', 'http://mibs.snmplabs.com/asn1/@mib@'), - lookupNames=True, lookupValues=True -) - -# Check for errors and print out results -if errorIndication: - print(errorIndication) -else: - if errorStatus: - print('%s at %s' % ( - errorStatus.prettyPrint(), - errorIndex and varBinds[int(errorIndex)-1][0] or '?' - ) - ) +for errorIndication, \ + errorStatus, errorIndex, \ + varBinds in 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@') + ), + lookupNames=True, lookupValues=True): + # Check for errors and print out results + if errorIndication: + print(errorIndication) else: - for oid, val in varBinds: - print('%s = %s' % (oid.prettyPrint(), val.prettyPrint())) + if 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 ])) + break diff --git a/examples/hlapi/asyncore/sync/manager/cmdgen/custom-contextengineid-and-contextname.py b/examples/hlapi/asyncore/sync/manager/cmdgen/custom-contextengineid-and-contextname.py index f28063b1..9c5b20f1 100644 --- a/examples/hlapi/asyncore/sync/manager/cmdgen/custom-contextengineid-and-contextname.py +++ b/examples/hlapi/asyncore/sync/manager/cmdgen/custom-contextengineid-and-contextname.py @@ -11,32 +11,33 @@ # * contextName 'a172334d7d97871b72241397f713fa12' # * setting SNMPv2-MIB::sysName.0 to new value (type taken from MIB) # -from pysnmp.entity.rfc3413.oneliner import cmdgen +from pysnmp.entity.rfc3413.oneliner.cmdgen import * from pysnmp.proto import rfc1902 -cmdGen = cmdgen.CommandGenerator() - -errorIndication, errorStatus, errorIndex, varBinds = cmdGen.setCmd( - cmdgen.UsmUserData('usr-md5-none', 'authkey1'), - cmdgen.UdpTransportTarget(('demo.snmplabs.com', 161)), - cmdgen.ObjectType( - cmdgen.ObjectIdentity('SNMPv2-MIB', 'sysORDescr', 1), - 'new system name' - ), - contextEngineId=rfc1902.OctetString(hexValue='80004fb805636c6f75644dab22cc'), - contextName='da761cfc8c94d3aceef4f60f049105ba' -) - -# Check for errors and print out results -if errorIndication: - print(errorIndication) -else: - if errorStatus: - print('%s at %s' % ( - errorStatus.prettyPrint(), - errorIndex and varBinds[int(errorIndex)-1][0] or '?' - ) - ) +for errorIndication, \ + errorStatus, errorIndex, \ + varBinds in setCmd(SnmpEngine(), + UsmUserData('usr-md5-none', 'authkey1'), + UdpTransportTarget(('demo.snmplabs.com', 161)), + ContextData( + contextEngineId=rfc1902.OctetString(hexValue='80004fb805636c6f75644dab22cc'), + contextName='da761cfc8c94d3aceef4f60f049105ba' + ), + ObjectType( + ObjectIdentity('SNMPv2-MIB', 'sysORDescr', 1), + 'new system name' + )): + # Check for errors and print out results + if errorIndication: + print(errorIndication) else: - for name, val in varBinds: - print('%s = %s' % (name.prettyPrint(), val.prettyPrint())) + if 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 ])) + break diff --git a/examples/hlapi/asyncore/sync/manager/cmdgen/custom-contextengineid.py b/examples/hlapi/asyncore/sync/manager/cmdgen/custom-contextengineid.py index 124a26fa..22d165df 100644 --- a/examples/hlapi/asyncore/sync/manager/cmdgen/custom-contextengineid.py +++ b/examples/hlapi/asyncore/sync/manager/cmdgen/custom-contextengineid.py @@ -10,35 +10,35 @@ # * to an Agent at demo.snmplabs.com:161 # * setting SNMPv2-MIB::sysName.0 to new value (type taken from MIB) # -from pysnmp.entity.rfc3413.oneliner import cmdgen +from pysnmp.entity.rfc3413.oneliner.cmdgen import * from pysnmp.proto import rfc1902 -cmdGen = cmdgen.CommandGenerator() - -errorIndication, errorStatus, errorIndex, varBinds = cmdGen.setCmd( - cmdgen.UsmUserData( - 'usr-md5-des', 'authkey1', 'privkey1', - securityEngineId=rfc1902.OctetString( - hexValue='80004fb805636c6f75644dab22cc' - ) - ), - cmdgen.UdpTransportTarget(('demo.snmplabs.com', 161)), - cmdgen.ObjectType( - cmdgen.ObjectIdentity('SNMPv2-MIB', 'sysORDescr', 1), - 'new system name' - ) -) - -# Check for errors and print out results -if errorIndication: - print(errorIndication) -else: - if errorStatus: - print('%s at %s' % ( - errorStatus.prettyPrint(), - errorIndex and varBinds[int(errorIndex)-1][0] or '?' - ) - ) +for errorIndication, \ + errorStatus, errorIndex, \ + varBinds in setCmd(SnmpEngine(), + UsmUserData( + 'usr-md5-des', 'authkey1', 'privkey1', + securityEngineId=rfc1902.OctetString( + hexValue='80004fb805636c6f75644dab22cc' + ) + ), + UdpTransportTarget(('demo.snmplabs.com', 161)), + ContextData(), + ObjectType( + ObjectIdentity('SNMPv2-MIB', 'sysORDescr', 1), + 'new system name' + )): + # Check for errors and print out results + if errorIndication: + print(errorIndication) else: - for name, val in varBinds: - print('%s = %s' % (name.prettyPrint(), val.prettyPrint())) + if 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 ])) + break diff --git a/examples/hlapi/asyncore/sync/manager/cmdgen/custom-pysnmp-mibs-search-path.py b/examples/hlapi/asyncore/sync/manager/cmdgen/custom-pysnmp-mibs-search-path.py index 4ca6b6e9..5d1eb815 100644 --- a/examples/hlapi/asyncore/sync/manager/cmdgen/custom-pysnmp-mibs-search-path.py +++ b/examples/hlapi/asyncore/sync/manager/cmdgen/custom-pysnmp-mibs-search-path.py @@ -12,30 +12,31 @@ # maxCalls == 10 request-response interactions occur # * ignoring non-increasing OIDs whenever reported by Agent # -# make sure IF-MIB.py is search path -# -from pysnmp.entity.rfc3413.oneliner import cmdgen - -cmdGen = cmdgen.CommandGenerator() - -errorIndication, errorStatus, errorIndex, varBindTable = cmdGen.bulkCmd( - cmdgen.UsmUserData('usr-none-none'), - cmdgen.UdpTransportTarget(('demo.snmplabs.com', 161)), - 0, 50, - cmdgen.ObjectIdentity('TCP-MIB', 'tcpConnTable').addMibSource('/tmp/mibs'), - lexicographicMode=True, maxRows=100, maxCalls=10,ignoreNonIncreasingOid=True -) +from pysnmp.entity.rfc3413.oneliner.cmdgen import * -if errorIndication: - print(errorIndication) -else: - if errorStatus: - print('%s at %s' % ( - errorStatus.prettyPrint(), - errorIndex and varBindTable[-1][int(errorIndex)-1][0] or '?' - ) - ) +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('/tmp/mibs')), + maxRows=100, maxCalls=10, + lexicographicMode=True, + ignoreNonIncreasingOid=True): + # Check for errors and print out results + if errorIndication: + print(errorIndication) + break else: - for varBindTableRow in varBindTable: - for name, val in varBindTableRow: - print('%s = %s' % (name.prettyPrint(), val.prettyPrint())) + if 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/asyncore/sync/manager/cmdgen/custom-timeout-and-retries.py b/examples/hlapi/asyncore/sync/manager/cmdgen/custom-timeout-and-retries.py index 35f575d5..d9fc4919 100644 --- a/examples/hlapi/asyncore/sync/manager/cmdgen/custom-timeout-and-retries.py +++ b/examples/hlapi/asyncore/sync/manager/cmdgen/custom-timeout-and-retries.py @@ -22,26 +22,28 @@ # if no response arrives, there will be no retry. Likewise, retries=1 # means one initial request plus one retry. # -from pysnmp.entity.rfc3413.oneliner import cmdgen +from pysnmp.entity.rfc3413.oneliner.cmdgen import * -cmdGen = cmdgen.CommandGenerator() - -errorIndication, errorStatus, errorIndex, varBinds = cmdGen.getCmd( - cmdgen.CommunityData('public'), - cmdgen.UdpTransportTarget(('demo.snmplabs.com', 161),timeout=1.5,retries=0), - '1.3.6.1.2.1.1.1.0', -) - -# Check for errors and print out results -if errorIndication: - print(errorIndication) -else: - if errorStatus: - print('%s at %s' % ( - errorStatus.prettyPrint(), - errorIndex and varBinds[int(errorIndex)-1][0] or '?' - ) - ) +for errorIndication, \ + errorStatus, errorIndex, \ + varBinds in getCmd(SnmpEngine(), + CommunityData('public'), + UdpTransportTarget( + ('demo.snmplabs.com', 161), timeout=1.5, retries=0 + ), + ContextData(), + ObjectType(ObjectIdentity('1.3.6.1.2.1.1.1.0'))): + # Check for errors and print out results + if errorIndication: + print(errorIndication) else: - for name, val in varBinds: - print('%s = %s' % (name.prettyPrint(), val.prettyPrint())) + if 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 ])) + break diff --git a/examples/hlapi/asyncore/sync/manager/cmdgen/custom-v3-security-name.py b/examples/hlapi/asyncore/sync/manager/cmdgen/custom-v3-security-name.py index c17e49b9..4770c5d3 100644 --- a/examples/hlapi/asyncore/sync/manager/cmdgen/custom-v3-security-name.py +++ b/examples/hlapi/asyncore/sync/manager/cmdgen/custom-v3-security-name.py @@ -14,26 +14,27 @@ # securityName can be made human-readable, also it is not an index in # usmUserTable, thus duplicate securityName parameters are possible. # -from pysnmp.entity.rfc3413.oneliner import cmdgen +from pysnmp.entity.rfc3413.oneliner.cmdgen import * -cmdGen = cmdgen.CommandGenerator() - -errorIndication, errorStatus, errorIndex, varBinds = cmdGen.getCmd( - cmdgen.UsmUserData('usr-md5-none', 'authkey1', securityName='myuser'), - cmdgen.UdpTransportTarget(('demo.snmplabs.com', 161)), - '1.3.6.1.2.1.1.1.0' -) - -# Check for errors and print out results -if errorIndication: - print(errorIndication) -else: - if errorStatus: - print('%s at %s' % ( - errorStatus.prettyPrint(), - errorIndex and varBinds[int(errorIndex)-1][0] or '?' - ) - ) +for errorIndication, \ + errorStatus, errorIndex, \ + varBinds in 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'))): + # Check for errors and print out results + if errorIndication: + print(errorIndication) else: - for name, val in varBinds: - print('%s = %s' % (name.prettyPrint(), val.prettyPrint())) + if 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 ])) + break diff --git a/examples/hlapi/asyncore/sync/manager/cmdgen/fetch-variables-over-ipv6.py b/examples/hlapi/asyncore/sync/manager/cmdgen/fetch-variables-over-ipv6.py index b766fad9..716d1e9b 100644 --- a/examples/hlapi/asyncore/sync/manager/cmdgen/fetch-variables-over-ipv6.py +++ b/examples/hlapi/asyncore/sync/manager/cmdgen/fetch-variables-over-ipv6.py @@ -9,28 +9,28 @@ # * for three OIDs: one passed as a ObjectIdentity object while others are # * in string form # -from pysnmp.entity.rfc3413.oneliner import cmdgen +from pysnmp.entity.rfc3413.oneliner.cmdgen import * -cmdGen = cmdgen.CommandGenerator() - -errorIndication, errorStatus, errorIndex, varBinds = cmdGen.getCmd( - cmdgen.UsmUserData('usr-md5-des', 'authkey1', 'privkey1'), - cmdgen.Udp6TransportTarget(('::1', 161)), - cmdgen.ObjectIdentity('1.3.6.1.2.1.1.1.0'), - '1.3.6.1.2.1.1.2.0', - '1.3.6.1.2.1.1.3.0' -) - -# Check for errors and print out results -if errorIndication: - print(errorIndication) -else: - if errorStatus: - print('%s at %s' % ( - errorStatus.prettyPrint(), - errorIndex and varBinds[int(errorIndex)-1][0] or '?' - ) - ) +for errorIndication, \ + errorStatus, errorIndex, \ + varBinds in 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'))): + # Check for errors and print out results + if errorIndication: + print(errorIndication) else: - for name, val in varBinds: - print('%s = %s' % (name.prettyPrint(), val.prettyPrint())) + if 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 ])) + break diff --git a/examples/hlapi/asyncore/sync/manager/cmdgen/get-table-object-by-index.py b/examples/hlapi/asyncore/sync/manager/cmdgen/get-table-object-by-index.py index c6419934..e83e0d2a 100644 --- a/examples/hlapi/asyncore/sync/manager/cmdgen/get-table-object-by-index.py +++ b/examples/hlapi/asyncore/sync/manager/cmdgen/get-table-object-by-index.py @@ -9,27 +9,27 @@ # * for IF-MIB::ifInOctets.1 MIB object # * perform response OIDs and values resolution at MIB # -from pysnmp.entity.rfc3413.oneliner import cmdgen +from pysnmp.entity.rfc3413.oneliner.cmdgen import * -cmdGen = cmdgen.CommandGenerator() - -errorIndication, errorStatus, errorIndex, varBinds = cmdGen.getCmd( - cmdgen.UsmUserData('usr-none-none'), - cmdgen.UdpTransportTarget(('demo.snmplabs.com', 161)), - cmdgen.ObjectIdentity('IF-MIB', 'ifInOctets', 1), - lookupNames=True, lookupValues=True -) - -# Check for errors and print out results -if errorIndication: - print(errorIndication) -else: - if errorStatus: - print('%s at %s' % ( - errorStatus.prettyPrint(), - errorIndex and varBinds[int(errorIndex)-1][0] or '?' - ) - ) +for errorIndication, \ + errorStatus, errorIndex, \ + varBinds in getCmd(SnmpEngine(), + UsmUserData('usr-none-none'), + UdpTransportTarget(('demo.snmplabs.com', 161)), + ContextData(), + ObjectType(ObjectIdentity('IF-MIB', 'ifInOctets', 1)), + lookupNames=True, lookupValues=True): + # Check for errors and print out results + if errorIndication: + print(errorIndication) else: - for name, val in varBinds: - print('%s = %s' % (name.prettyPrint(), val.prettyPrint())) + if 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 ])) + break diff --git a/examples/hlapi/asyncore/sync/manager/cmdgen/get-table-object-by-multiple-indices.py b/examples/hlapi/asyncore/sync/manager/cmdgen/get-table-object-by-multiple-indices.py index 8bc4562b..7b78a356 100644 --- a/examples/hlapi/asyncore/sync/manager/cmdgen/get-table-object-by-multiple-indices.py +++ b/examples/hlapi/asyncore/sync/manager/cmdgen/get-table-object-by-multiple-indices.py @@ -8,28 +8,33 @@ # * to an Agent at demo.snmplabs.com:161 # * for TCP-MIB::tcpConnLocalAddress."0.0.0.0".22."0.0.0.0".0 MIB object # -from pysnmp.entity.rfc3413.oneliner import cmdgen +from pysnmp.entity.rfc3413.oneliner.cmdgen import * -cmdGen = cmdgen.CommandGenerator() - -errorIndication, errorStatus, errorIndex, varBinds = cmdGen.getCmd( - cmdgen.UsmUserData('usr-sha-aes128', 'authkey1', 'privkey1', - authProtocol=cmdgen.usmHMACSHAAuthProtocol, - privProtocol=cmdgen.usmAesCfb128Protocol ), - cmdgen.UdpTransportTarget(('demo.snmplabs.com', 161)), - cmdgen.ObjectIdentity('TCP-MIB', 'tcpConnLocalAddress', '0.0.0.0', 22, '0.0.0.0', 0) -) - -# Check for errors and print out results -if errorIndication: - print(errorIndication) -else: - if errorStatus: - print('%s at %s' % ( - errorStatus.prettyPrint(), - errorIndex and varBinds[int(errorIndex)-1][0] or '?' - ) - ) +for errorIndication, \ + errorStatus, errorIndex, \ + varBinds in getCmd(SnmpEngine(), + UsmUserData('usr-sha-aes128', 'authkey1', 'privkey1', + authProtocol=usmHMACSHAAuthProtocol, + privProtocol=usmAesCfb128Protocol ), + UdpTransportTarget(('demo.snmplabs.com', 161)), + ContextData(), + ObjectType( + ObjectIdentity('TCP-MIB', + 'tcpConnLocalAddress', + '0.0.0.0', 22, + '0.0.0.0', 0) + )): + # Check for errors and print out results + if errorIndication: + print(errorIndication) else: - for name, val in varBinds: - print('%s = %s' % (name.prettyPrint(), val.prettyPrint())) + if 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 ])) + break diff --git a/examples/hlapi/asyncore/sync/manager/cmdgen/getbulk-fetch-scalar-and-table-variables.py b/examples/hlapi/asyncore/sync/manager/cmdgen/getbulk-fetch-scalar-and-table-variables.py index 99df3c11..9b255f35 100644 --- a/examples/hlapi/asyncore/sync/manager/cmdgen/getbulk-fetch-scalar-and-table-variables.py +++ b/examples/hlapi/asyncore/sync/manager/cmdgen/getbulk-fetch-scalar-and-table-variables.py @@ -11,32 +11,30 @@ # * stop when response OIDs leave the scopes of the table OR maxRows == 20 # * perform response OIDs and values resolution at MIB # -from pysnmp.entity.rfc3413.oneliner import cmdgen +from pysnmp.entity.rfc3413.oneliner.cmdgen import * -cmdGen = cmdgen.CommandGenerator() - -# Send a series of SNMP GETBULK requests -# make sure IF-MIB.py and IP-MIB.py are in search path - -errorIndication, errorStatus, errorIndex, varBindTable = cmdGen.bulkCmd( - cmdgen.UsmUserData('usr-md5-des', 'authkey1', 'privkey1'), - cmdgen.Udp6TransportTarget(('::1', 161)), - 1, 25, - cmdgen.ObjectIdentity('IP-MIB', 'ipAdEntAddr'), - cmdgen.ObjectIdentity('IF-MIB', 'ifEntry'), - lookupNames=True, lookupValues=True, maxRows=20 -) - -if errorIndication: - print(errorIndication) -else: - if errorStatus: - print('%s at %s' % ( - errorStatus.prettyPrint(), - errorIndex and varBindTable[-1][int(errorIndex)-1][0] or '?' - ) - ) +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')), + lookupNames=True, lookupValues=True, maxRows=20): + # Check for errors and print out results + if errorIndication: + print(errorIndication) + break else: - for varBindTableRow in varBindTable: - for name, val in varBindTableRow: - print('%s = %s' % (name.prettyPrint(), val.prettyPrint())) + if 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/asyncore/sync/manager/cmdgen/getnext-limit-number-of-variables.py b/examples/hlapi/asyncore/sync/manager/cmdgen/getnext-limit-number-of-variables.py index bdeda853..99eba513 100644 --- a/examples/hlapi/asyncore/sync/manager/cmdgen/getnext-limit-number-of-variables.py +++ b/examples/hlapi/asyncore/sync/manager/cmdgen/getnext-limit-number-of-variables.py @@ -12,30 +12,31 @@ # # make sure IF-MIB.py is search path # -from pysnmp.entity.rfc3413.oneliner import cmdgen +from pysnmp.entity.rfc3413.oneliner.cmdgen import * -cmdGen = cmdgen.CommandGenerator() - -errorIndication, errorStatus, errorIndex, varBindTable = cmdGen.nextCmd( - cmdgen.UsmUserData('usr-sha-aes128', 'authkey1', 'privkey1', - authProtocol=cmdgen.usmHMACSHAAuthProtocol, - privProtocol=cmdgen.usmAesCfb128Protocol), - cmdgen.UdpTransportTarget(('demo.snmplabs.com', 161)), - cmdgen.ObjectIdentity('IF-MIB', '').loadMibs(), - lexicographicMode=True, maxRows=100, - ignoreNonIncreasingOid=True -) - -if errorIndication: - print(errorIndication) -else: - if errorStatus: - print('%s at %s' % ( - errorStatus.prettyPrint(), - errorIndex and varBindTable[-1][int(errorIndex)-1][0] or '?' - ) - ) +for errorIndication, \ + errorStatus, errorIndex, \ + varBinds in nextCmd(SnmpEngine(), + UsmUserData('usr-sha-aes128', 'authkey1', 'privkey1', + authProtocol=usmHMACSHAAuthProtocol, + privProtocol=usmAesCfb128Protocol), + UdpTransportTarget(('demo.snmplabs.com', 161)), + ContextData(), + ObjectType(ObjectIdentity('IF-MIB', '').loadMibs()), + lexicographicMode=True, maxRows=100, + ignoreNonIncreasingOid=True): + # Check for errors and print out results + if errorIndication: + print(errorIndication) + break else: - for varBindTableRow in varBindTable: - for name, val in varBindTableRow: - print('%s = %s' % (name.prettyPrint(), val.prettyPrint())) + if 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/asyncore/sync/manager/cmdgen/getnext-multiple-oids-to-eom.py b/examples/hlapi/asyncore/sync/manager/cmdgen/getnext-multiple-oids-to-eom.py index c5181ca3..178cd09e 100644 --- a/examples/hlapi/asyncore/sync/manager/cmdgen/getnext-multiple-oids-to-eom.py +++ b/examples/hlapi/asyncore/sync/manager/cmdgen/getnext-multiple-oids-to-eom.py @@ -9,27 +9,28 @@ # * for two OIDs in string form # * stop when response OIDs leave the scopes of initial OIDs # -from pysnmp.entity.rfc3413.oneliner import cmdgen +from pysnmp.entity.rfc3413.oneliner.cmdgen import * -cmdGen = cmdgen.CommandGenerator() - -errorIndication, errorStatus, errorIndex, varBindTable = cmdGen.nextCmd( - cmdgen.CommunityData('public'), - cmdgen.UdpTransportTarget(('demo.snmplabs.com', 161)), - '1.3.6.1.2.1.2.2.1.2', - '1.3.6.1.2.1.2.2.1.3' -) - -if errorIndication: - print(errorIndication) -else: - if errorStatus: - print('%s at %s' % ( - errorStatus.prettyPrint(), - errorIndex and varBindTable[-1][int(errorIndex)-1][0] or '?' - ) - ) +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'))): + # Check for errors and print out results + if errorIndication: + print(errorIndication) + break else: - for varBindTableRow in varBindTable: - for name, val in varBindTableRow: - print('%s = %s' % (name.prettyPrint(), val.prettyPrint())) + if 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/asyncore/sync/manager/cmdgen/preload-pysnmp-mibs.py b/examples/hlapi/asyncore/sync/manager/cmdgen/preload-pysnmp-mibs.py index 847ba252..01c74be0 100644 --- a/examples/hlapi/asyncore/sync/manager/cmdgen/preload-pysnmp-mibs.py +++ b/examples/hlapi/asyncore/sync/manager/cmdgen/preload-pysnmp-mibs.py @@ -12,27 +12,28 @@ # # make sure IF-MIB.py is in search path # -from pysnmp.entity.rfc3413.oneliner import cmdgen +from pysnmp.entity.rfc3413.oneliner.cmdgen import * -cmdGen = cmdgen.CommandGenerator() - -errorIndication, errorStatus, errorIndex, varBindTable = cmdGen.nextCmd( - cmdgen.UsmUserData('usr-md5-des', 'authkey1', 'privkey1'), - cmdgen.Udp6TransportTarget(('::1', 161)), - cmdgen.ObjectIdentity('IF-MIB', 'ifEntry'), - lookupNames=True, lookupValues=True -) - -if errorIndication: - print(errorIndication) -else: - if errorStatus: - print('%s at %s' % ( - errorStatus.prettyPrint(), - errorIndex and varBindTable[-1][int(errorIndex)-1][0] or '?' - ) - ) +for errorIndication, \ + errorStatus, errorIndex, \ + varBinds in nextCmd(SnmpEngine(), + UsmUserData('usr-md5-des', 'authkey1', 'privkey1'), + Udp6TransportTarget(('::1', 161)), + ContextData(), + ObjectType(ObjectIdentity('IF-MIB', 'ifEntry')), + lookupNames=True, lookupValues=True): + # Check for errors and print out results + if errorIndication: + print(errorIndication) + break else: - for varBindTableRow in varBindTable: - for name, val in varBindTableRow: - print('%s = %s' % (name.prettyPrint(), val.prettyPrint())) + if 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/asyncore/sync/manager/cmdgen/pull-whole-mib.py b/examples/hlapi/asyncore/sync/manager/cmdgen/pull-whole-mib.py index 2d62e85c..658f6fc5 100644 --- a/examples/hlapi/asyncore/sync/manager/cmdgen/pull-whole-mib.py +++ b/examples/hlapi/asyncore/sync/manager/cmdgen/pull-whole-mib.py @@ -10,29 +10,28 @@ # * stop when response OIDs leave the scopes of the table # * perform response values resolution at MIB # -# make sure IF-MIB.py is in search path -# -from pysnmp.entity.rfc3413.oneliner import cmdgen - -cmdGen = cmdgen.CommandGenerator() - -errorIndication, errorStatus, errorIndex, varBindTable = cmdGen.nextCmd( - cmdgen.UsmUserData('usr-md5-none', 'authkey1'), - cmdgen.UdpTransportTarget(('demo.snmplabs.com', 161)), - cmdgen.ObjectIdentity('IF-MIB', ''), - lookupValues=True -) +from pysnmp.entity.rfc3413.oneliner.cmdgen import * -if errorIndication: - print(errorIndication) -else: - if errorStatus: - print('%s at %s' % ( - errorStatus.prettyPrint(), - errorIndex and varBindTable[-1][int(errorIndex)-1][0] or '?' - ) - ) +for errorIndication, \ + errorStatus, errorIndex, \ + varBinds in nextCmd(SnmpEngine(), + UsmUserData('usr-md5-none', 'authkey1'), + UdpTransportTarget(('demo.snmplabs.com', 161)), + ContextData(), + ObjectType(ObjectIdentity('IF-MIB', '')), + lookupValues=True): + # Check for errors and print out results + if errorIndication: + print(errorIndication) + break else: - for varBindTableRow in varBindTable: - for name, val in varBindTableRow: - print('%s = %s' % (name.prettyPrint(), val.prettyPrint())) + if 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/asyncore/sync/manager/cmdgen/pull-whole-snmp-table.py b/examples/hlapi/asyncore/sync/manager/cmdgen/pull-whole-snmp-table.py index 7be99e22..1c3a998e 100644 --- a/examples/hlapi/asyncore/sync/manager/cmdgen/pull-whole-snmp-table.py +++ b/examples/hlapi/asyncore/sync/manager/cmdgen/pull-whole-snmp-table.py @@ -9,32 +9,32 @@ # * for some columns of the IF-MIB::ifEntry table # * stop when response OIDs leave the scopes of initial OIDs # -# make sure IF-MIB.py is in search path -# -from pysnmp.entity.rfc3413.oneliner import cmdgen - -cmdGen = cmdgen.CommandGenerator() - -errorIndication, errorStatus, errorIndex, varBindTable = cmdGen.nextCmd( - cmdgen.CommunityData('public', mpModel=0), - cmdgen.UdpTransportTarget(('demo.snmplabs.com', 161)), - cmdgen.ObjectIdentity('IF-MIB', 'ifDescr'), - cmdgen.ObjectIdentity('IF-MIB', 'ifType'), - cmdgen.ObjectIdentity('IF-MIB', 'ifMtu'), - cmdgen.ObjectIdentity('IF-MIB', 'ifSpeed'), - cmdgen.ObjectIdentity('IF-MIB', 'ifPhysAddress') -) +from pysnmp.entity.rfc3413.oneliner.cmdgen import * -if errorIndication: - print(errorIndication) -else: - if errorStatus: - print('%s at %s' % ( - errorStatus.prettyPrint(), - errorIndex and varBindTable[-1][int(errorIndex)-1][0] or '?' - ) - ) +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'))): + # Check for errors and print out results + if errorIndication: + print(errorIndication) + break else: - for varBindTableRow in varBindTable: - for name, val in varBindTableRow: - print('%s = %s' % (name.prettyPrint(), val.prettyPrint())) + if 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/asyncore/sync/manager/cmdgen/set-multiple-scalar-values.py b/examples/hlapi/asyncore/sync/manager/cmdgen/set-multiple-scalar-values.py index b225b71f..bba4a84d 100644 --- a/examples/hlapi/asyncore/sync/manager/cmdgen/set-multiple-scalar-values.py +++ b/examples/hlapi/asyncore/sync/manager/cmdgen/set-multiple-scalar-values.py @@ -8,29 +8,29 @@ # * to an Agent at demo.snmplabs.com:161 # * setting two OIDs to new values (types explicitly specified) # -from pysnmp.entity.rfc3413.oneliner import cmdgen +from pysnmp.entity.rfc3413.oneliner.cmdgen import * from pysnmp.proto import rfc1902 -cmdGen = cmdgen.CommandGenerator() - -errorIndication, errorStatus, errorIndex, varBinds = cmdGen.setCmd( - cmdgen.CommunityData('public', mpModel=0), - cmdgen.UdpTransportTarget(('demo.snmplabs.com', 161)), - ('1.3.6.1.2.1.1.9.1.2.1', rfc1902.ObjectName('1.3.6.1.4.1.20408.1.1')), - ('1.3.6.1.2.1.1.9.1.2.1', '1.3.6.1.4.1.20408.1.1'), - ('1.3.6.1.2.1.1.9.1.3.1', rfc1902.OctetString('new system name')) -) - -# Check for errors and print out results -if errorIndication: - print(errorIndication) -else: - if errorStatus: - print('%s at %s' % ( - errorStatus.prettyPrint(), - errorIndex and varBinds[int(errorIndex)-1][0] or '?' - ) - ) +for errorIndication, \ + errorStatus, errorIndex, \ + varBinds in setCmd(SnmpEngine(), + CommunityData('public', mpModel=0), + UdpTransportTarget(('demo.snmplabs.com', 161)), + ContextData(), + ('1.3.6.1.2.1.1.9.1.2.1', rfc1902.ObjectName('1.3.6.1.4.1.20408.1.1')), + ('1.3.6.1.2.1.1.9.1.2.1', '1.3.6.1.4.1.20408.1.1'), + ('1.3.6.1.2.1.1.9.1.3.1', rfc1902.OctetString('new system name'))): + # Check for errors and print out results + if errorIndication: + print(errorIndication) else: - for name, val in varBinds: - print('%s = %s' % (name.prettyPrint(), val.prettyPrint())) + if 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 ])) + break diff --git a/examples/hlapi/asyncore/sync/manager/cmdgen/specific-v3-engine-id.py b/examples/hlapi/asyncore/sync/manager/cmdgen/specific-v3-engine-id.py index efdcbc8e..ca1d1fef 100644 --- a/examples/hlapi/asyncore/sync/manager/cmdgen/specific-v3-engine-id.py +++ b/examples/hlapi/asyncore/sync/manager/cmdgen/specific-v3-engine-id.py @@ -13,14 +13,11 @@ # ** to the same SNMP Engine ID # ** for an OID in text form # -from pysnmp.entity import engine -from pysnmp.entity.rfc3413.oneliner import cmdgen +from pysnmp.entity.rfc3413.oneliner.cmdgen import * -snmpEngine = engine.SnmpEngine() +snmpEngine = SnmpEngine() -cmdGen = cmdgen.CommandGenerator(snmpEngine) - -transportTarget = cmdgen.UdpTransportTarget(('demo.snmplabs.com', 161)) +transportTarget = UdpTransportTarget(('demo.snmplabs.com', 161)) # # To discover remote SNMP EngineID we will tap on SNMP engine inner workings @@ -39,11 +36,12 @@ snmpEngine.observer.registerObserver( # Send probe SNMP request with invalid credentials -authData = cmdgen.UsmUserData('non-existing-user') +authData = UsmUserData('non-existing-user') -errorIndication, errorStatus, errorIndex, varBinds = cmdGen.getCmd( - authData, transportTarget -) +for errorIndication, errorStatus, errorIndex, \ + varBinds in getCmd(snmpEngine, authData, + transportTarget, ContextData()): + break # See if our SNMP engine received REPORT PDU containing securityEngineId @@ -59,12 +57,14 @@ print('Remote securityEngineId = %s' % securityEngineId.prettyPrint()) # Query remote SNMP Engine using usmUserTable entry configured for it # -authData = cmdgen.UsmUserData('usr-md5-none', 'authkey1', - securityEngineId=securityEngineId) +authData = UsmUserData('usr-md5-none', 'authkey1', + securityEngineId=securityEngineId) -errorIndication, errorStatus, errorIndex, varBinds = cmdGen.getCmd( - authData, transportTarget, '1.3.6.1.2.1.1.1.0' -) +for errorIndication, errorStatus, errorIndex, \ + varBinds in getCmd(snmpEngine, authData, \ + transportTarget, ContextData(), \ + ObjectType(ObjectIdentity('1.3.6.1.2.1.1.1.0'))): + break # Check for errors and print out results if errorIndication: diff --git a/examples/hlapi/asyncore/sync/manager/cmdgen/usm-md5-none.py b/examples/hlapi/asyncore/sync/manager/cmdgen/usm-md5-none.py index 47f025f1..c01a61d0 100644 --- a/examples/hlapi/asyncore/sync/manager/cmdgen/usm-md5-none.py +++ b/examples/hlapi/asyncore/sync/manager/cmdgen/usm-md5-none.py @@ -8,26 +8,26 @@ # * to an Agent at demo.snmplabs.com:161 # * for IF-MIB::ifInOctets.1 MIB object # -from pysnmp.entity.rfc3413.oneliner import cmdgen +from pysnmp.entity.rfc3413.oneliner.cmdgen import * -cmdGen = cmdgen.CommandGenerator() - -errorIndication, errorStatus, errorIndex, varBinds = cmdGen.getCmd( - cmdgen.UsmUserData('usr-md5-none', 'authkey1'), - cmdgen.UdpTransportTarget(('demo.snmplabs.com', 161)), - cmdgen.ObjectIdentity('IF-MIB', 'ifInOctets', 1) -) - -# Check for errors and print out results -if errorIndication: - print(errorIndication) -else: - if errorStatus: - print('%s at %s' % ( - errorStatus.prettyPrint(), - errorIndex and varBinds[int(errorIndex)-1][0] or '?' - ) - ) +for errorIndication, \ + errorStatus, errorIndex, \ + varBinds in getCmd(SnmpEngine(), + UsmUserData('usr-md5-none', 'authkey1'), + UdpTransportTarget(('demo.snmplabs.com', 161)), + ContextData(), + ObjectType(ObjectIdentity('IF-MIB', 'ifInOctets', 1))): + # Check for errors and print out results + if errorIndication: + print(errorIndication) else: - for name, val in varBinds: - print('%s = %s' % (name.prettyPrint(), val.prettyPrint())) + if 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 ])) + break diff --git a/examples/hlapi/asyncore/sync/manager/cmdgen/v1-get.py b/examples/hlapi/asyncore/sync/manager/cmdgen/v1-get.py index 664df9dd..aca41293 100644 --- a/examples/hlapi/asyncore/sync/manager/cmdgen/v1-get.py +++ b/examples/hlapi/asyncore/sync/manager/cmdgen/v1-get.py @@ -9,28 +9,27 @@ # * for two instances of SNMPv2-MIB::sysDescr.0 MIB object, # * one in label and another in MIB symbol form # -from pysnmp.entity.rfc3413.oneliner import cmdgen +from pysnmp.entity.rfc3413.oneliner.cmdgen import * -cmdGen = cmdgen.CommandGenerator() - -errorIndication, errorStatus, errorIndex, varBinds = cmdGen.getCmd( - cmdgen.CommunityData('public', mpModel=0), - cmdgen.UdpTransportTarget(('demo.snmplabs.com', 161)), - cmdgen.ObjectIdentity('iso.org.dod.internet.mgmt.mib-2.system.sysDescr.0'), - cmdgen.ObjectIdentity('SNMPv2-MIB', 'sysDescr', 0) -) - -# Check for errors and print out results -if errorIndication: - print(errorIndication) -else: - if errorStatus: - print('%s at %s' % ( - errorStatus.prettyPrint(), - errorIndex and varBinds[int(errorIndex)-1][0] or '?' - ) - ) +for errorIndication, \ + errorStatus, errorIndex, \ + varBinds in getCmd(SnmpEngine(), + CommunityData('public', mpModel=0), + UdpTransportTarget(('demo.snmplabs.com', 161)), + ContextData(), + ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysDescr', 0)), + lookupNames=True, lookupValues=True): + # Check for errors and print out results + if errorIndication: + print(errorIndication) else: - for name, val in varBinds: - print('%s = %s' % (name.prettyPrint(), val.prettyPrint())) - + if 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 ])) + break diff --git a/examples/hlapi/asyncore/sync/manager/cmdgen/v2c-get.py b/examples/hlapi/asyncore/sync/manager/cmdgen/v2c-get.py index de39b6ce..5fe5bfe9 100644 --- a/examples/hlapi/asyncore/sync/manager/cmdgen/v2c-get.py +++ b/examples/hlapi/asyncore/sync/manager/cmdgen/v2c-get.py @@ -8,27 +8,27 @@ # * to an Agent at demo.snmplabs.com:161 # * for two OIDs in string form # -from pysnmp.entity.rfc3413.oneliner import cmdgen +from pysnmp.entity.rfc3413.oneliner.cmdgen import * -cmdGen = cmdgen.CommandGenerator() - -errorIndication, errorStatus, errorIndex, varBinds = cmdGen.getCmd( - cmdgen.CommunityData('public'), - cmdgen.UdpTransportTarget(('demo.snmplabs.com', 161)), - '1.3.6.1.2.1.1.1.0', - '1.3.6.1.2.1.1.6.0' -) - -# Check for errors and print out results -if errorIndication: - print(errorIndication) -else: - if errorStatus: - print('%s at %s' % ( - errorStatus.prettyPrint(), - errorIndex and varBinds[int(errorIndex)-1][0] or '?' - ) - ) +for errorIndication, \ + errorStatus, errorIndex, \ + varBinds in 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'))): + # Check for errors and print out results + if errorIndication: + print(errorIndication) else: - for name, val in varBinds: - print('%s = %s' % (name.prettyPrint(), val.prettyPrint())) + if 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 ])) + break diff --git a/examples/hlapi/asyncore/sync/manager/cmdgen/v2c-getbulk.py b/examples/hlapi/asyncore/sync/manager/cmdgen/v2c-getbulk.py index 1aa138b1..0bed0f08 100644 --- a/examples/hlapi/asyncore/sync/manager/cmdgen/v2c-getbulk.py +++ b/examples/hlapi/asyncore/sync/manager/cmdgen/v2c-getbulk.py @@ -10,28 +10,29 @@ # * for two OIDs in string form # * stop when response OIDs leave the scopes of initial OIDs # -from pysnmp.entity.rfc3413.oneliner import cmdgen +from pysnmp.entity.rfc3413.oneliner.cmdgen import * -cmdGen = cmdgen.CommandGenerator() - -errorIndication, errorStatus, errorIndex, varBindTable = cmdGen.bulkCmd( - cmdgen.CommunityData('public'), - cmdgen.UdpTransportTarget(('demo.snmplabs.com', 161)), - 0, 25, - '1.3.6.1.2.1.2.2', - '1.3.6.1.2.1.2.3', -) - -if errorIndication: - print(errorIndication) -else: - if errorStatus: - print('%s at %s' % ( - errorStatus.prettyPrint(), - errorIndex and varBindTable[-1][int(errorIndex)-1][0] or '?' - ) - ) +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'))): + # Check for errors and print out results + if errorIndication: + print(errorIndication) + break else: - for varBindTableRow in varBindTable: - for name, val in varBindTableRow: - print('%s = %s' % (name.prettyPrint(), val.prettyPrint())) + if 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/asyncore/sync/manager/cmdgen/waive-mib-lookup.py b/examples/hlapi/asyncore/sync/manager/cmdgen/waive-mib-lookup.py index c7f08970..e59b07bd 100644 --- a/examples/hlapi/asyncore/sync/manager/cmdgen/waive-mib-lookup.py +++ b/examples/hlapi/asyncore/sync/manager/cmdgen/waive-mib-lookup.py @@ -20,27 +20,28 @@ # currently loaded MIBs, unresolved OIDs and values will still be # returned. # -from pysnmp.entity.rfc3413.oneliner import cmdgen +from pysnmp.entity.rfc3413.oneliner.cmdgen import * -cmdGen = cmdgen.CommandGenerator() - -errorIndication, errorStatus, errorIndex, varBindTable = cmdGen.nextCmd( - cmdgen.CommunityData('public'), - cmdgen.UdpTransportTarget(('demo.snmplabs.com', 161)), - '1.3.6.1.2.1.1', - lookupNames=True, lookupValues=True -) - -if errorIndication: - print(errorIndication) -else: - if errorStatus: - print('%s at %s' % ( - errorStatus.prettyPrint(), - errorIndex and varBindTable[-1][int(errorIndex)-1] or '?' - ) - ) +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')), + lookupNames=True, lookupValues=True): + # Check for errors and print out results + if errorIndication: + print(errorIndication) + break else: - for varBindTableRow in varBindTable: - for name, val in varBindTableRow: - print('%s = %s' % (name.prettyPrint(), val.prettyPrint())) + if 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 ])) |