diff options
Diffstat (limited to 'examples/hlapi/v1arch/asyncore/manager')
5 files changed, 262 insertions, 0 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 new file mode 100644 index 00000000..7c282a81 --- /dev/null +++ b/examples/hlapi/v1arch/asyncore/manager/cmdgen/multiple-concurrent-queries-over-ipv4-and-ipv6.py @@ -0,0 +1,63 @@ +""" +Multiple concurrent queries ++++++++++++++++++++++++++++ + +Send a bunch of different SNMP GET requests to different peers all at once, +wait for responses asynchronously: + +* with SNMPv1, community 'public' and + with SNMPv2c, community 'public' and +* over IPv4/UDP and + over IPv6/UDP +* to an Agent at demo.snmplabs.com:161 and + to an Agent at [::1]:161 +* for instances of SNMPv2-MIB::system + SNMPv2-MIB::sysLocation.0 MIB objects +* Enable MIB lookup feature +"""# +from pysnmp.hlapi.v1arch.asyncore import * + +# List of targets in the following format: +# ((authData, transportTarget, varNames), ...) +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 + # different transport address. + (CommunityData('public'), + Udp6TransportTarget(('::1', 161)), + (ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysContact', 0)), + ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysName', 0)))), + # N-th target + # ... +) + + +def cbFun(errorIndication, errorStatus, errorIndex, varBinds, **context): + 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])) + +snmpDispatcher = SnmpDispatcher() + +# Submit a bunch of initial GET requests +for authData, transportTarget, varBinds in targets: + getCmd(snmpDispatcher, authData, transportTarget, *varBinds, + **dict(cbFun=cbFun, lookupMib=True)) + +snmpDispatcher.transportDispatcher.runDispatcher() 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 new file mode 100644 index 00000000..eaf1755d --- /dev/null +++ b/examples/hlapi/v1arch/asyncore/manager/cmdgen/pull-mibs-from-multiple-agents-at-once-over-ipv4-and-ipv6.py @@ -0,0 +1,63 @@ +""" +Walk multiple Agents at once +++++++++++++++++++++++++++++ + +Iterate over MIBs of multiple SNMP Agents asynchronously using the +following options: + +* with SNMPv1, community 'public' and + with SNMPv2c, community 'public' and +* over IPv4/UDP and + over IPv6/UDP +* to an Agent at demo.snmplabs.com:161 and + to an Agent at [::1]:161 +* pull MIB variables till EOM +* Enable MIB lookup feature + +"""# +from pysnmp.hlapi.v1arch.asyncore import * + +# List of targets in the following format: +# ((authData, transportTarget, varNames), ...) +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 + # ... +) + + +def cbFun(errorIndication, errorStatus, errorIndex, varBindTable, **context): + if errorIndication: + print(errorIndication) + + 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])) + + return context.get('nextVarBinds') + +snmpDispatcher = SnmpDispatcher() + +# Submit a bunch of initial GETNEXT requests +for authData, transportTarget, varBinds in targets: + nextCmd(snmpDispatcher, authData, transportTarget, *varBinds, + **dict(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 new file mode 100644 index 00000000..c5731570 --- /dev/null +++ b/examples/hlapi/v1arch/asyncore/manager/cmdgen/pull-whole-mib.py @@ -0,0 +1,44 @@ +""" +Walk whole MIB +++++++++++++++ + +Send a series of SNMP GETBULK requests using the following options: + +* with SNMPv2c, community 'public' +* over IPv4/UDP +* to an Agent at demo.snmplabs.com:161 +* for all OIDs past 1.3.6.1.4.1 + +Functionally similar to: + +| $ snmpwalk -v2c -c public demo.snmplabs.com 1.3.6.1.4.1 +"""# +from pysnmp.hlapi.v1arch.asyncore import * + + +def cbFun(errorIndication, errorStatus, errorIndex, varBindTable, **context): + if errorIndication: + print(errorIndication) + + 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])) + + return context.get('nextVarBinds') + +snmpDispatcher = SnmpDispatcher() + +# Submit initial GETBULK request +bulkCmd(snmpDispatcher, + CommunityData('public'), + UdpTransportTarget(('demo.snmplabs.com', 161)), + 0, 25, + ('1.3.6.1.4.1', None), + **dict(cbFun=cbFun)) + +snmpDispatcher.transportDispatcher.runDispatcher() diff --git a/examples/hlapi/v1arch/asyncore/manager/cmdgen/v1-getnext.py b/examples/hlapi/v1arch/asyncore/manager/cmdgen/v1-getnext.py new file mode 100644 index 00000000..385e869c --- /dev/null +++ b/examples/hlapi/v1arch/asyncore/manager/cmdgen/v1-getnext.py @@ -0,0 +1,48 @@ +""" +SNMP GETNEXT over SNMPv1 +++++++++++++++++++++++++ + +Send a series of SNMP GETNEXT requests using the following options: + + * with SNMPv1, community 'public' + * over IPv4/UDP + * to an Agent at demo.snmplabs.com:161 + * for the 1.3.6.1.2.1.1 OID (e.g. SNMPv2-MIB::system MIB branch) + +Functionally similar to: + +| $ snmpwalk -v1 -c public demo.snmplabs.com 1.3.6.1.2.1.1 + +"""# + +from pysnmp.hlapi.v1arch.asyncore import * + + +def cbFun(errorIndication, errorStatus, errorIndex, varBindTable, **context): + 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: + print(' = '.join([x.prettyPrint() for x in varBind])) + + return context.get('nextVarBinds') + +snmpDispatcher = SnmpDispatcher() + +stateHandle = nextCmd( + snmpDispatcher, + CommunityData('public', mpModel=0), + UdpTransportTarget(('demo.snmplabs.com', 161)), + ('1.3.6.1.5.1.1', None), + cbFun=cbFun +) + +snmpDispatcher.transportDispatcher.runDispatcher() diff --git a/examples/hlapi/v1arch/asyncore/manager/cmdgen/v2c-get.py b/examples/hlapi/v1arch/asyncore/manager/cmdgen/v2c-get.py new file mode 100644 index 00000000..9ce8d7dd --- /dev/null +++ b/examples/hlapi/v1arch/asyncore/manager/cmdgen/v2c-get.py @@ -0,0 +1,44 @@ +""" +SNMP GET over SNMPv2c ++++++++++++++++++++++ + +Send SNMP GET request using the following options: + + * with SNMPv2c, community 'public' + * over IPv4/UDP + * to an Agent at demo.snmplabs.com:161 + * for the 1.3.6.1.2.1.1.1.0 OID (e.g. SNMPv2-MIB::sysDescr.0 MIB object) + +Functionally similar to: + +| $ snmpget -v2c -c public demo.snmplabs.com 1.3.6.1.2.1.1.1.0 + +"""# + +from pysnmp.hlapi.v1arch.asyncore import * + + +def cbFun(errorIndication, errorStatus, errorIndex, varBinds, **context): + 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])) + + +snmpDispatcher = SnmpDispatcher() + +stateHandle = getCmd( + snmpDispatcher, + CommunityData('public'), + UdpTransportTarget(('demo.snmplabs.com', 161)), + ('1.3.6.1.2.1.1.1.0', None), + cbFun=cbFun +) + +snmpDispatcher.transportDispatcher.runDispatcher() |