diff options
Diffstat (limited to 'examples/hlapi/v1arch/asyncore')
31 files changed, 1476 insertions, 0 deletions
diff --git a/examples/hlapi/v1arch/asyncore/agent/ntforg/generic-v1-trap.py b/examples/hlapi/v1arch/asyncore/agent/ntforg/generic-v1-trap.py new file mode 100644 index 00000000..ca969f10 --- /dev/null +++ b/examples/hlapi/v1arch/asyncore/agent/ntforg/generic-v1-trap.py @@ -0,0 +1,52 @@ +""" +Generic SNMPv1 TRAP ++++++++++++++++++++ + +Send SNMPv1 TRAP using the following options: + +* SNMPv1 +* with community name 'public' +* over IPv4/UDP +* send TRAP notification +* with Uptime 12345 +* with Generic Trap #1 (warmStart) and Specific Trap 0 +* with Agent Address 127.0.0.1 +* with Enterprise OID 1.3.6.1.4.1.20408.4.1.1.2 +* include managed object information '1.3.6.1.2.1.1.1.0' = 'my system' + +Functionally similar to: + +| $ snmptrap -v1 -c public demo.snmplabs.com 1.3.6.1.4.1.20408.4.1.1.2 0.0.0.0 1 0 0 1.3.6.1.2.1.1.1.0 s "my system" + +"""# +from pysnmp.hlapi.v1arch.asyncore import * + + +def cbFun(errorIndication, errorStatus, errorIndex, varBinds, **context): + if errorIndication: + print(errorIndication) + + +snmpDispatcher = SnmpDispatcher() + +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')), + cbFun=cbFun +) + +snmpDispatcher.transportDispatcher.runDispatcher() diff --git a/examples/hlapi/v1arch/asyncore/agent/ntforg/generic-v2c-trap.py b/examples/hlapi/v1arch/asyncore/agent/ntforg/generic-v2c-trap.py new file mode 100644 index 00000000..36c01e60 --- /dev/null +++ b/examples/hlapi/v1arch/asyncore/agent/ntforg/generic-v2c-trap.py @@ -0,0 +1,44 @@ +""" +Generic SNMPv2c TRAP +++++++++++++++++++++ + +Send SNMPv1 TRAP using the following options: + +* SNMPv2c +* with community name 'public' +* over IPv4/UDP +* send TRAP notification +* with Uptime 12345 +* with Generic Trap #1 (warmStart) and Specific Trap 0 +* include managed object information '1.3.6.1.2.1.1.1.0' = 'my system' + +Functionally similar to: + +| $ snmptrap -v2c -c public demo.snmplabs.com 12345 1.3.6.1.6.3.1.1.5.2 1.3.6.1.2.1.1.1.0 s "my system" + +"""# +from pysnmp.hlapi.v1arch.asyncore import * + + +def cbFun(errorIndication, errorStatus, errorIndex, varBinds, **context): + if errorIndication: + print(errorIndication) + + +snmpDispatcher = SnmpDispatcher() + +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')), + cbFun=cbFun +) + +snmpDispatcher.transportDispatcher.runDispatcher() diff --git a/examples/hlapi/v1arch/asyncore/agent/ntforg/v2c-inform.py b/examples/hlapi/v1arch/asyncore/agent/ntforg/v2c-inform.py new file mode 100644 index 00000000..8271a9f5 --- /dev/null +++ b/examples/hlapi/v1arch/asyncore/agent/ntforg/v2c-inform.py @@ -0,0 +1,51 @@ +""" +SNMPv2c INFORM +++++++++++++++ + +Send SNMP INFORM notification using the following options: + +* SNMPv2c +* with community name 'public' +* over IPv4/UDP +* send INFORM notification +* with TRAP ID 'warmStart' specified as a string OID +* include managed object information 1.3.6.1.2.1.1.5.0 = 'system name' + +Functionally similar to: + +| $ snmpinform -v2c -c public demo.snmplabs.com 12345 1.3.6.1.4.1.20408.4.1.1.2 1.3.6.1.2.1.1.1.0 s "my system" + +"""# +from pysnmp.hlapi.v1arch.asyncore import * + + +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() + +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')), + cbFun=cbFun +) + +snmpDispatcher.transportDispatcher.runDispatcher() diff --git a/examples/hlapi/v1arch/asyncore/agent/ntforg/v2c-trap-with-notification-objects.py b/examples/hlapi/v1arch/asyncore/agent/ntforg/v2c-trap-with-notification-objects.py new file mode 100644 index 00000000..a3bc18ae --- /dev/null +++ b/examples/hlapi/v1arch/asyncore/agent/ntforg/v2c-trap-with-notification-objects.py @@ -0,0 +1,53 @@ +""" +Generic SNMPv2c TRAP +++++++++++++++++++++ + +Send SNMPv1 TRAP using the following options: + +* SNMPv2c +* with community name 'public' +* over IPv4/UDP +* send TRAP notification +* with Uptime 12345 +* with Generic Trap #1 (warmStart) and Specific Trap 0 +* include managed object information '1.3.6.1.2.1.1.1.0' = 'my system' + +Functionally similar to: + +| $ snmptrap -v2c -c public demo.snmplabs.com 12345 1.3.6.1.6.3.1.1.5.2 1.3.6.1.2.1.1.1.0 s "my system" + +"""# +from pysnmp.hlapi.v1arch.asyncore import * + + +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() + +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')), + cbFun=cbFun, + lookupMib=True +) + +snmpDispatcher.transportDispatcher.runDispatcher() 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() 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 new file mode 100644 index 00000000..2f1fa0fd --- /dev/null +++ b/examples/hlapi/v1arch/asyncore/sync/agent/ntforg/generic-v1-trap.py @@ -0,0 +1,46 @@ +""" +Generic SNMPv1 TRAP ++++++++++++++++++++ + +Send SNMPv1 TRAP using the following options: + +* SNMPv1 +* with community name 'public' +* over IPv4/UDP +* send TRAP notification +* with Uptime 12345 +* with Generic Trap #1 (warmStart) and Specific Trap 0 +* with Agent Address 127.0.0.1 +* with Enterprise OID 1.3.6.1.4.1.20408.4.1.1.2 +* include managed object information '1.3.6.1.2.1.1.1.0' = 'my system' + +Functionally similar to: + +| $ snmptrap -v1 -c public demo.snmplabs.com 1.3.6.1.4.1.20408.4.1.1.2 0.0.0.0 1 0 0 1.3.6.1.2.1.1.1.0 s "my system" + +"""# +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')) + ) +) + +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 new file mode 100644 index 00000000..10a04ed4 --- /dev/null +++ b/examples/hlapi/v1arch/asyncore/sync/agent/ntforg/generic-v2c-trap.py @@ -0,0 +1,38 @@ +""" +Generic SNMPv2c TRAP +++++++++++++++++++++ + +Send SNMPv1 TRAP using the following options: + +* SNMPv2c +* with community name 'public' +* over IPv4/UDP +* send TRAP notification +* with Uptime 12345 +* with Generic Trap #1 (warmStart) and Specific Trap 0 +* include managed object information '1.3.6.1.2.1.1.1.0' = 'my system' + +Functionally similar to: + +| $ snmptrap -v2c -c public demo.snmplabs.com 12345 1.3.6.1.6.3.1.1.5.2 1.3.6.1.2.1.1.1.0 s "my system" + +"""# +from pysnmp.hlapi.v1arch import * + +errorIndication, errorStatus, errorIndex, varBinds = next( + sendNotification( + SnmpDispatcher(), + CommunityData('public'), + UdpTransportTarget(('demo.snmplabs.com', 162)), + 'trap', + # SNMPv2-MIB::sysUpTime.0 = 12345 + ('1.3.6.1.2.1.1.3.0', TimeTicks(12345)), + # SNMPv2-SMI::snmpTrapOID.0 = SNMPv2-MIB::warmStart + ('1.3.6.1.6.3.1.1.4.1.0', ObjectIdentifier('1.3.6.1.6.3.1.1.5.2')), + # SNMPv2-MIB::sysName.0 + ('1.3.6.1.2.1.1.1.0', OctetString('my system')) + ) +) + +if errorIndication: + print(errorIndication) diff --git a/examples/hlapi/v1arch/asyncore/sync/agent/ntforg/v2c-inform.py b/examples/hlapi/v1arch/asyncore/sync/agent/ntforg/v2c-inform.py new file mode 100644 index 00000000..c1799993 --- /dev/null +++ b/examples/hlapi/v1arch/asyncore/sync/agent/ntforg/v2c-inform.py @@ -0,0 +1,45 @@ +""" +SNMPv2c INFORM +++++++++++++++ + +Send SNMP INFORM notification using the following options: + +* SNMPv2c +* with community name 'public' +* over IPv4/UDP +* send INFORM notification +* with TRAP ID 'warmStart' specified as a string OID +* include managed object information 1.3.6.1.2.1.1.5.0 = 'system name' + +Functionally similar to: + +| $ snmpinform -v2c -c public demo.snmplabs.com 12345 1.3.6.1.4.1.20408.4.1.1.2 1.3.6.1.2.1.1.1.0 s "my system" + +"""# +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')) + ) +) + +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/agent/ntforg/v2c-trap-with-notification-objects.py b/examples/hlapi/v1arch/asyncore/sync/agent/ntforg/v2c-trap-with-notification-objects.py new file mode 100644 index 00000000..052946bc --- /dev/null +++ b/examples/hlapi/v1arch/asyncore/sync/agent/ntforg/v2c-trap-with-notification-objects.py @@ -0,0 +1,39 @@ +""" +Generic SNMPv2c TRAP +++++++++++++++++++++ + +Send SNMPv1 TRAP using the following options: + +* SNMPv2c +* with community name 'public' +* over IPv4/UDP +* send TRAP notification +* with Uptime 12345 +* with Generic Trap #1 (warmStart) and Specific Trap 0 +* include managed object information '1.3.6.1.2.1.1.1.0' = 'my system' + +Functionally similar to: + +| $ snmptrap -v2c -c public demo.snmplabs.com 12345 1.3.6.1.6.3.1.1.5.2 1.3.6.1.2.1.1.1.0 s "my system" + +"""# +from pysnmp.hlapi.v1arch import * + +errorIndication, errorStatus, errorIndex, varBinds = next( + sendNotification( + SnmpDispatcher(), + CommunityData('public'), + UdpTransportTarget(('demo.snmplabs.com', 162)), + 'trap', + # SNMPv2-MIB::sysUpTime.0 = 12345 + ('1.3.6.1.2.1.1.3.0', TimeTicks(12345)), + # SNMPv2-SMI::snmpTrapOID.0 = SNMPv2-MIB::warmStart + NotificationType(ObjectIdentity('SNMPv2-MIB', 'warmStart')), + # SNMPv2-MIB::sysName.0 + ('1.3.6.1.2.1.1.1.0', OctetString('my system')), + lookupMib=True + ) +) + +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 new file mode 100644 index 00000000..a6789f7f --- /dev/null +++ b/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/coerce-set-value-to-mib-spec.py @@ -0,0 +1,38 @@ +""" +Coerce value to SET to MIB spec ++++++++++++++++++++++++++++++++ + +Send SNMP SET request using the following options: + +* with SNMPv2c, community 'public' +* over IPv4/UDP +* to an Agent at demo.snmplabs.com:161 +* with MIB lookup enabled +* setting SNMPv2-MIB::sysName.0 to new value (type taken from MIB) + +Functionally similar to: + +| $ snmpset -v2c -c public demo.snmplabs.com SNMPv2-MIB::sysDescr.0 = "new system name" + +"""# +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) +) + +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/custom-asn1-mib-search-path.py b/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/custom-asn1-mib-search-path.py new file mode 100644 index 00000000..759171c1 --- /dev/null +++ b/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/custom-asn1-mib-search-path.py @@ -0,0 +1,40 @@ +""" +Custom ASN.1 MIB path ++++++++++++++++++++++ + +Send SNMP GET request using the following options: + +* with SNMPv2c, community 'public' +* over IPv4/UDP +* to an Agent at demo.snmplabs.com:161 +* for IF-MIB::ifInOctets.1 MIB object +* pass non-default ASN.1 MIB source to MIB compiler +* with MIB lookup enabled + +Functionally similar to: + +| $ snmpget -v2c -c public -M /usr/share/snmp demo.snmplabs.com IF-MIB::ifInOctets.1 + +"""# +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) +) + +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/custom-pysnmp-mibs-search-path.py b/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/custom-pysnmp-mibs-search-path.py new file mode 100644 index 00000000..a3686d69 --- /dev/null +++ b/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/custom-pysnmp-mibs-search-path.py @@ -0,0 +1,49 @@ +""" +Custom PySNMP MIBs location ++++++++++++++++++++++++++++ + +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 within TCP-MIB::tcpConnTable column +* TCP-MIB Python module will be searched by a user-specified filesystem + path (/opt/mib/pysnmp) and in Python package (python_packaged_mibs) + which should be in sys.path +* with MIB lookup enabled + +Functionally similar to: + +| $ snmpbulkwalk -v2c -c public -Cn0 -Cr50 demo.snmplabs.com TCP-MIB::tcpConnTable + +"""# +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): + + 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/v1arch/asyncore/sync/manager/cmdgen/custom-timeout-and-retries.py b/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/custom-timeout-and-retries.py new file mode 100644 index 00000000..7a4253cf --- /dev/null +++ b/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/custom-timeout-and-retries.py @@ -0,0 +1,51 @@ +""" +Custom request timeout +++++++++++++++++++++++ + +Send SNMP GET request using the following options: + +* with SNMPv2c, community 'public' +* over IPv4/UDP +* to an Agent at demo.snmplabs.com:161 +* for an OID in string form +* use custom timeout and request retries values + +Transport timing settings (maximum number of request retries and +individual request timeout in seconds) can be set on a per-target basis +as explained by the code that follows. + +Keep in mind that while timeout value can be specified in fractions of a +second, default pysnmp timer resolution is quite low (about 0.5 sec) +so there's no much point in using timeouts which is not a multiple of 0.5 +Internal timer can be programmatically adjusted to finer resolution if needed. + +If retries value is set to 0, pysnmp will issue a single request. Even +if no response arrives, there will be no retry. Likewise, retries=1 +means one initial request plus one retry. + +Functionally similar to: + +| $ snmpget -v1 -c public -t 2 -r 0 demo.snmplabs.com 1.3.6.1.2.1.1.1.0 + +"""# +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))) +) + +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/enable-mib-lookup.py b/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/enable-mib-lookup.py new file mode 100644 index 00000000..2f4f8edb --- /dev/null +++ b/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/enable-mib-lookup.py @@ -0,0 +1,43 @@ +""" +Enable MIB lookup ++++++++++++++++++ + +Perform SNMP GETNEXT operation with the following options: + +* with SNMPv2c, community 'public' +* over IPv4/UDP +* to an Agent at demo.snmplabs.com:161 +* for an OID in string form +* resolve request and response OIDs and values from/to human-friendly form + +The `lookupMib=True` keyword argument makes pysnmp resolving +request and response variable-bindings from/to human-friendly form. + +Functionally similar to: + +| $ snmpwalk -v2c -c public -ObentU demo.snmplabs.com 1.3.6.1.2.1 + +"""# +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): + + 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/v1arch/asyncore/sync/manager/cmdgen/fetch-variables-over-ipv6.py b/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/fetch-variables-over-ipv6.py new file mode 100644 index 00000000..bd8d9ca2 --- /dev/null +++ b/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/fetch-variables-over-ipv6.py @@ -0,0 +1,36 @@ +""" +GET over IPv6 ++++++++++++++ + +Send SNMP GET request using the following options: + +* with SNMPv2c, community 'public' +* over IPv6/UDP +* to an Agent at [::1]:161 +* for three OIDs in string form + +Functionally similar to: + +| $ snmpget -v2c -c public udp6:[::1]:161 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 +"""# +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)) +) + +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/get-table-object-by-index.py b/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/get-table-object-by-index.py new file mode 100644 index 00000000..b7fccdcb --- /dev/null +++ b/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/get-table-object-by-index.py @@ -0,0 +1,38 @@ +""" +GET table row ++++++++++++++ + +Send SNMP GET request using the following options: + +* with SNMPv2c, community name "public" +* over IPv4/UDP +* to an Agent at demo.snmplabs.com:161 +* with MIB lookup enabled +* for IF-MIB::ifInOctets.1 and IF-MIB::ifOutOctets.1 MIB object + +Functionally similar to: + +| $ snmpget -v 2c -c public demo.snmplabs.com IF-MIB::ifInOctets.1 IF-MIB::ifOutOctets.1 + +"""# +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) +) + +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/get-table-object-by-multiple-indices.py b/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/get-table-object-by-multiple-indices.py new file mode 100644 index 00000000..cec5ad25 --- /dev/null +++ b/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/get-table-object-by-multiple-indices.py @@ -0,0 +1,40 @@ +""" +Fetch table row by composite index +++++++++++++++++++++++++++++++++++ + +Send SNMP GET request using the following options: + +* with SNMPv2c, community 'public' +* over IPv4/UDP +* to an Agent at demo.snmplabs.com:161 +* for TCP-MIB::tcpConnLocalAddress."0.0.0.0".22."0.0.0.0".0 MIB object +* with MIB lookup enabled + +Functionally similar to: + +| $ snmpget -v2c -c public demo.snmplabs.com TCP-MIB::tcpConnLocalAddress."0.0.0.0".22."0.0.0.0".0 + +"""# +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) +) + +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/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 new file mode 100644 index 00000000..49257a1c --- /dev/null +++ b/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/getbulk-fetch-scalar-and-table-variables-over-ipv6.py @@ -0,0 +1,45 @@ +""" +Fetch scalar and table variables +++++++++++++++++++++++++++++++++ + +Send a series of SNMP GETBULK requests using the following options: + +* with SNMPv2c, community name "public" +* over IPv6/UDP +* to an Agent at demo.snmplabs.com:161 +* with MIB lookup enabled +* with values non-repeaters = 1, max-repetitions = 25 +* for IP-MIB::ipAdEntAddr and all columns of the IF-MIB::ifEntry table +* stop when response OIDs leave the scopes of the table + +Functionally similar to: + +| $ snmpbulkwalk -v2c -c public -Cn1, -Cr25 demo.snmplabs.com IP-MIB::ipAdEntAddr IP-MIB::ipAddrEntry + +"""# +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): + + 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/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 new file mode 100644 index 00000000..22d52589 --- /dev/null +++ b/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/getbulk-limit-number-of-packets.py @@ -0,0 +1,44 @@ +""" +Walk Agent, limit number of packets ++++++++++++++++++++++++++++++++++++ + +Send a series of SNMP GETBULK requests using the following options: + +* with SNMPv2c, community name "public" +* over IPv4/UDP +* to an Agent at demo.snmplabs.com:161 +* for all OIDs past SNMPv2-MIB::system +* with MIB lookup enabled +* run till end-of-mib condition is reported by Agent OR + maxCalls == 10 request-response interactions occur + +Functionally similar to: + +| $ snmpbulkwalk -v2c -c public -Cn0 -Cr50 demo.snmplabs.com SNMPv2-MIB::system + +"""# +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): + + 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/v1arch/asyncore/sync/manager/cmdgen/multiple-get-calls.py b/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/multiple-get-calls.py new file mode 100644 index 00000000..57a1132b --- /dev/null +++ b/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/multiple-get-calls.py @@ -0,0 +1,48 @@ +""" +Sequence Of GET's ++++++++++++++++++ + +Send two SNMP GET requests in a row using the following options: + +* with SNMPv2c, community name "public" +* over IPv4/UDP +* to an Agent at demo.snmplabs.com:161 +* for IF-MIB::ifInOctets.1 and IF-MIB::ifOutOctets.1 MIB objects +* with MIB lookup enabled + +Use a queue of MIB objects to query. + +The next() call is used to forward Python iterator to the position where it +could consume input + +Functionally similar to: + +| $ snmpget -v2c -c public demo.snmplabs.com IF-MIB::ifInOctets.1 + +"""# +from pysnmp.hlapi.v1arch import * + +queue = [ + [ObjectType(ObjectIdentity('IF-MIB', 'ifInOctets', 1))], + [ObjectType(ObjectIdentity('IF-MIB', 'ifOutOctets', 1))] +] + +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) + + 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/preload-pysnmp-mibs.py b/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/preload-pysnmp-mibs.py new file mode 100644 index 00000000..39d1e7dd --- /dev/null +++ b/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/preload-pysnmp-mibs.py @@ -0,0 +1,41 @@ +""" +Preload PySNMP MIBs ++++++++++++++++++++ + +Send a series of SNMP GETNEXT requests using the following options: + +* with SNMPv2c, community name "public" +* over IPv4/UDP +* to an Agent at demo.snmplabs.com:161 +* for all OIDs starting from 1.3.6 +* with MIB lookup enabled +* preload all Python MIB modules found in search path + +Functionally similar to: + +| $ snmpwalk -v2c -c public -m ALL demo.snmplabs.com:161 1.3.6 + +"""# +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): + + 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/v1arch/asyncore/sync/manager/cmdgen/pull-whole-mib.py b/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/pull-whole-mib.py new file mode 100644 index 00000000..69e943d9 --- /dev/null +++ b/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/pull-whole-mib.py @@ -0,0 +1,39 @@ +""" +Walk whole MIB +++++++++++++++ + +Send a series of SNMP GETNEXT requests using the following options: + +* with SNMPv2c, community 'public' +* over IPv4/UDP +* to an Agent at demo.snmplabs.com:161 +* for all OIDs in IF-MIB +* with MIB lookup enabled + +Functionally similar to: + +| $ snmpwalk -v2c -c public demo.snmplabs.com IF-MIB:: + +"""# +from pysnmp.hlapi.v1arch import * + +for (errorIndication, + errorStatus, + errorIndex, + varBinds) in nextCmd(SnmpDispatcher(), + CommunityData('public'), + UdpTransportTarget(('demo.snmplabs.com', 161)), + ObjectType(ObjectIdentity('IF-MIB'))): + + if errorIndication: + print(errorIndication) + break + + elif errorStatus: + print('%s at %s' % (errorStatus.prettyPrint(), + errorIndex and varBinds[int(errorIndex) - 1][0] or '?')) + break + + else: + for varBind in varBinds: + print(' = '.join([x.prettyPrint() for x in varBind])) 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 new file mode 100644 index 00000000..8d81b1e5 --- /dev/null +++ b/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/pull-whole-snmp-table.py @@ -0,0 +1,47 @@ +""" +Fetch whole SNMP table +++++++++++++++++++++++ + +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 some columns of the IF-MIB::ifEntry table +* with MIB lookup enabled +* stop when response OIDs leave the scopes of initial OIDs + +Functionally similar to: + +| $ snmpwalk -v1 -c public demo.snmplabs.com IF-MIB::ifDescr IF-MIB::ifType IF-MIB::ifMtu IF-MIB::ifSpeed IF-MIB::ifPhysAddress IF-MIB::ifType + +"""# +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): + + 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/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 new file mode 100644 index 00000000..29ec78b6 --- /dev/null +++ b/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/query-agents-from-multuple-threads-over-ipv4-and-ipv6.py @@ -0,0 +1,130 @@ +""" +Query Agents from multiple threads +++++++++++++++++++++++++++++++++++ + +Send a bunch of SNMP GET requests simultaneously using the following options: + +* process 4 GET requests in 3 parallel threads +* 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 demo.snmplabs.com:1161 and + to an Agent at [::1]:161 +* for instances of SNMPv2-MIB::sysDescr.0 and + SNMPv2-MIB::sysLocation.0 MIB objects +* with MIB lookup enabled + +Functionally similar to: + +| $ snmpget -v1 -c public demo.snmplabs.com SNMPv2-MIB::sysDescr.0 SNMPv2-MIB::sysLocation.0 +| $ snmpget -v2c -c public demo.snmplabs.com SNMPv2-MIB::sysDescr.0 SNMPv2-MIB::sysLocation.0 +| $ snmpget -v2c -c public demo.snmplabs.com:1161 SNMPv2-MIB::sysDescr.0 SNMPv2-MIB::sysLocation.0 +| $ snmpget -v2c -c public '[::1]' SNMPv2-MIB::sysDescr.0 SNMPv2-MIB::sysLocation.0 + +"""# +from sys import version_info +from threading import Thread +from pysnmp.hlapi.v1arch import * + +if version_info[0] == 2: + from Queue import Queue +else: + from queue import Queue + +# 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(('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 + # ... +) + + +class Worker(Thread): + def __init__(self, requests, responses): + Thread.__init__(self) + self.snmpDispatcher = SnmpDispatcher() + self.requests = requests + self.responses = responses + self.setDaemon(True) + self.start() + + def run(self): + while True: + authData, transportTarget, varBinds = self.requests.get() + + self.responses.append( + next(getCmd(self.snmpDispatcher, + authData, transportTarget, *varBinds, lookupMib=True)) + ) + + if hasattr(self.requests, 'task_done'): # 2.5+ + self.requests.task_done() + + +class ThreadPool(object): + def __init__(self, num_threads): + self.requests = Queue(num_threads) + self.responses = [] + for _ in range(num_threads): + Worker(self.requests, self.responses) + + def addRequest(self, authData, transportTarget, varBinds): + self.requests.put((authData, transportTarget, varBinds)) + + def getResponses(self): + return self.responses + + def waitCompletion(self): + if hasattr(self.requests, 'join'): + self.requests.join() # 2.5+ + else: + from time import sleep + # this is a lame substitute for missing .join() + # adding an explicit synchronization might be a better solution + while not self.requests.empty(): + sleep(1) + + +pool = ThreadPool(3) + +# Submit GET requests +for authData, transportTarget, varBinds in targets: + pool.addRequest(authData, transportTarget, varBinds) + +# Wait for responses or errors +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 new file mode 100644 index 00000000..b5498003 --- /dev/null +++ b/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/set-multiple-scalar-values.py @@ -0,0 +1,46 @@ +""" +SET scalars values +++++++++++++++++++ + +Send SNMP SET request using the following options: + +* with SNMPv1, community 'public' +* over IPv4/UDP +* to an Agent at demo.snmplabs.com:161 +* with MIB lookup enabled +* setting three var-bindings to new values + +Please note, that in this example MIB lookup is only used +for the second var-bindins. For the rest, value types are +inferred from passed objects. + +Functionally similar to: + +| $ snmpset -v1 -c public demo.snmplabs.com 1.3.6.1.2.1.1.9.1.2.1 o 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 s "new system name" + +"""# +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) +) + +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/v1-get.py b/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/v1-get.py new file mode 100644 index 00000000..b54802af --- /dev/null +++ b/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/v1-get.py @@ -0,0 +1,36 @@ +""" +SNMPv1 +++++++ + +Send SNMP GET request using the following options: + + * with SNMPv1, community 'public' + * over IPv4/UDP + * to an Agent at demo.snmplabs.com:161 + * for an instance of SNMPv2-MIB::sysDescr.0 MIB object + * having MIB lookup feature enabled + +Functionally similar to: + +| $ snmpget -v1 -c public demo.snmplabs.com SNMPv2-MIB::sysDescr.0 + +"""# +from pysnmp.hlapi.v1arch import * + +for response in getCmd(SnmpDispatcher(), + CommunityData('public', mpModel=0), + UdpTransportTarget(('demo.snmplabs.com', 161)), + ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysDescr', 0)), + lookupMib=True): + + errorIndication, errorStatus, errorIndex, varBinds = response + + if errorIndication: + print(errorIndication) + + elif errorStatus: + print('%s at %s' % (errorStatus.prettyPrint(), + errorIndex and varBinds[int(errorIndex) - 1][0] or '?')) + else: + for varBind in varBinds: + print(' = '.join([x.prettyPrint() for x in varBind])) diff --git a/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/v2c-get.py b/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/v2c-get.py new file mode 100644 index 00000000..e2ec5815 --- /dev/null +++ b/examples/hlapi/v1arch/asyncore/sync/manager/cmdgen/v2c-get.py @@ -0,0 +1,35 @@ +""" +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 two var-bindings with OIDs in string form + +Functionally similar to: + +| $ snmpget -v2c -c public demo.snmplabs.com 1.3.6.1.2.1.1.1.0 1.3.6.1.2.1.1.6.0 + +"""# +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)): + + errorIndication, errorStatus, errorIndex, varBinds = response + + if errorIndication: + print(errorIndication) + + elif errorStatus: + print('%s at %s' % (errorStatus.prettyPrint(), + errorIndex and varBinds[int(errorIndex) - 1][0] or '?')) + else: + for varBind in varBinds: + print(' = '.join([x.prettyPrint() for x in varBind])) |