diff options
author | elie <elie> | 2012-12-17 22:12:06 +0000 |
---|---|---|
committer | elie <elie> | 2012-12-17 22:12:06 +0000 |
commit | 10b56a1406691dc76994c1e4faff9dfcf74ff738 (patch) | |
tree | b555cc1a5e004b52ac4ccf3d21b3b2bdb8429d9f /examples | |
parent | 5147d65f1359879e105daff56a88abd09f057803 (diff) | |
download | pysnmp-10b56a1406691dc76994c1e4faff9dfcf74ff738.tar.gz |
oneliner examples split into dedicated files for clarity
Diffstat (limited to 'examples')
34 files changed, 1021 insertions, 850 deletions
diff --git a/examples/v3arch/oneliner/agent/ntforg.py b/examples/v3arch/oneliner/agent/ntforg.py deleted file mode 100644 index a4f08d0..0000000 --- a/examples/v3arch/oneliner/agent/ntforg.py +++ /dev/null @@ -1,267 +0,0 @@ -# Various uses of the Notification Originator (TRAP/INFORM) -from pysnmp.entity.rfc3413.oneliner import ntforg -from pysnmp.proto import rfc1902 - -ntfOrg = ntforg.NotificationOriginator() - -# Using -# SNMPv2c -# with community name 'public' -# over IPv4/UDP -# send TRAP notification -# with TRAP ID 'coldStart' specified as a MIB symbol -# include managed object information specified as a MIB symbol -errorIndication = ntfOrg.sendNotification( - ntforg.CommunityData('public'), - ntforg.UdpTransportTarget(('localhost', 162)), - 'trap', - ntforg.MibVariable('SNMPv2-MIB', 'coldStart'), - (ntforg.MibVariable('SNMPv2-MIB', 'sysName', 0), 'new name') -) - -if errorIndication: - print('Notification not sent: %s' % errorIndication) - - -# Using -# SNMPv1 -# with community name 'public' -# over IPv4/UDP -# send TRAP notification -# with Generic Trap #6 (enterpriseSpecific) and Specific Trap 432 -# overriding Uptime value with 12345 -# overriding Agent Address with '127.0.0.1' -# overriding Enterprise OID with 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' -errorIndication = ntfOrg.sendNotification( - ntforg.CommunityData('public', mpModel=0), - ntforg.UdpTransportTarget(('localhost', 162)), - 'trap', - '1.3.6.1.4.1.20408.4.1.1.2.0.432', - ('1.3.6.1.2.1.1.3.0', 12345), - ('1.3.6.1.6.3.18.1.3.0', '127.0.0.1'), - ('1.3.6.1.6.3.1.1.4.3.0', '1.3.6.1.4.1.20408.4.1.1.2'), - ('1.3.6.1.2.1.1.1.0', rfc1902.OctetString('my system')) -) - -if errorIndication: - print('Notification not sent: %s' % errorIndication) - - -# Using -# SNMPv1 -# with community name 'public' -# over IPv4/UDP -# send TRAP notification -# with Generic Trap #1 (warmStart) and Specific Trap 0 -# with default Uptime -# with default Agent Address -# 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' -errorIndication = ntfOrg.sendNotification( - ntforg.CommunityData('public', mpModel=0), - ntforg.UdpTransportTarget(('localhost', 162)), - 'trap', - '1.3.6.1.6.3.1.1.5.2', - ('1.3.6.1.6.3.1.1.4.3.0', '1.3.6.1.4.1.20408.4.1.1.2'), - ('1.3.6.1.2.1.1.1.0', rfc1902.OctetString('my system')) -) - -if errorIndication: - print('Notification not sent: %s' % errorIndication) - - -# Using -# SNMPv2c -# with community name 'public' -# over IPv4/UDP -# send TRAP notification -# with TRAP ID 'coldStart' specified as a MIB symbol -# include managed object information specified as a MIB symbol -# perform response OIDs and values resolution at MIB -errorIndication, errorStatus, errorIndex, varBinds = ntfOrg.sendNotification( - ntforg.CommunityData('public'), - ntforg.UdpTransportTarget(('localhost', 162)), - 'inform', - ntforg.MibVariable('SNMPv2-MIB', 'coldStart'), - (ntforg.MibVariable('SNMPv2-MIB', 'sysName', 0), 'my system'), - lookupNames=True, lookupValues=True -) - -if errorIndication: - print('Notification not sent: %s' % errorIndication) -elif errorStatus: - print('Notification Receiver returned error: %s @%s' % (errorStatus, errorIndex)) -else: - for name, val in varBinds: - print('%s = %s' % (name.prettyPrint(), val.prettyPrint())) - - -# Using -# SNMPv3 -# with user 'usr-md5-des', auth: MD5, priv 3DES -# 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' -errorIndication, errorStatus, errorIndex, varBinds = ntfOrg.sendNotification( - ntforg.UsmUserData('usr-md5-des', 'authkey1', 'privkey1'), - ntforg.UdpTransportTarget(('localhost', 162)), - 'inform', - '1.3.6.1.6.3.1.1.5.2', - ('1.3.6.1.2.1.1.5.0', rfc1902.OctetString('system name')) -) - -if errorIndication: - print('Notification not sent: %s' % errorIndication) -elif errorStatus: - print('Notification Receiver returned error: %s @%s' % (errorStatus, errorIndex)) -else: - for name, val in varBinds: - print('%s = %s' % (name.prettyPrint(), val.prettyPrint())) - - -# -# Using -# SNMPv3 -# with user 'usr-md5-none', MD5 auth, no priv -# send INFORM notification -# in behalf of contextEngineId = SnmpEngineId, contextName 'my-context' -# over IPv4/UDP -# with TRAP ID 'warmStart' specified as a string OID -# -# Sending SNMPv3 Notification in behalf of non-default ContextName -# requires having a collection of Managed Objects registered under -# the ContextName being used, so we re-create SnmpEngine and -# NotificationOriginator alone. -# -from pysnmp.entity import engine -from pysnmp.entity.rfc3413 import context -from pysnmp.entity.rfc3413.oneliner import ntforg - -snmpEngine = engine.SnmpEngine() -snmpContext = context.SnmpContext(snmpEngine) - -# register default collection of Managed Objects under new contextName -snmpContext.registerContextName('my-context', snmpContext.getMibInstrum()) - -ntfOrg = ntforg.NotificationOriginator(snmpEngine, snmpContext) - -errorIndication, errorStatus, errorIndex, varBinds = ntfOrg.sendNotification( - ntforg.UsmUserData('usr-md5-none', 'authkey1'), - ntforg.UdpTransportTarget(('localhost', 162)), - 'inform', - '1.3.6.1.6.3.1.1.5.2', - contextName='my-context' -) - -if errorIndication: - print('Notification not sent: %s' % errorIndication) -elif errorStatus: - print('Notification Receiver returned error: %s @%s' % (errorStatus, errorIndex)) -else: - for name, val in varBinds: - print('%s = %s' % (name.prettyPrint(), val.prettyPrint())) - - -# -# Using -# SNMPv3 -# with user 'usr-md5-none', MD5 auth, no priv -# send INFORM notification -# in behalf of contextEngineId 0x8000000004030201, contextName '' -# over IPv4/UDP -# with TRAP ID 'warmStart' specified as a string OID -# -# Sending SNMPv3 Notification in behalf of non-default ContextEngineId -# requires having a collection of Managed Objects registered under -# the ContextEngineId being used, so we re-create SnmpEngine and -# NotificationOriginator alone. -# -from pysnmp.entity import engine -from pysnmp.entity.rfc3413 import context -from pysnmp.entity.rfc3413.oneliner import ntforg -from pysnmp.proto import rfc1902 - -snmpEngine = engine.SnmpEngine() -snmpContext = context.SnmpContext( - snmpEngine,contextEngineId=rfc1902.OctetString(hexValue='8000000004030201') -) - -ntfOrg = ntforg.NotificationOriginator(snmpEngine, snmpContext) - -errorIndication, errorStatus, errorIndex, varBinds = ntfOrg.sendNotification( - ntforg.UsmUserData('usr-md5-none', 'authkey1'), - ntforg.UdpTransportTarget(('localhost', 162)), - 'inform', - '1.3.6.1.6.3.1.1.5.2' -) - -if errorIndication: - print('Notification not sent: %s' % errorIndication) -elif errorStatus: - print('Notification Receiver returned error: %s @%s' % (errorStatus, errorIndex)) -else: - for name, val in varBinds: - print('%s = %s' % (name.prettyPrint(), val.prettyPrint())) - - -# Using -# SNMPv3 -# with user 'usr-none-none', no auth, no priv -# over IPv4/UDP -# send INFORM notification -# with TRAP ID 'warmStart' specified as a string OID -errorIndication, errorStatus, errorIndex, varBinds = ntfOrg.sendNotification( - ntforg.UsmUserData('usr-none-none'), - ntforg.UdpTransportTarget(('localhost', 162)), - 'inform', - '1.3.6.1.6.3.1.1.5.2', - contextName=rfc1902.OctetString('') -) - -if errorIndication: - print('Notification not sent: %s' % errorIndication) -elif errorStatus: - print('Notification Receiver returned error: %s @%s' % (errorStatus, errorIndex)) -else: - for name, val in varBinds: - print('%s = %s' % (name.prettyPrint(), val.prettyPrint())) - - -# -# Using -# SNMPv3 -# with local snmpEngineId = 0x8000000001020304 -# with user 'usr-sha-aes', auth: SHA, priv: AES128 -# over IPv6/UDP -# send TRAP notification -# with TRAP ID 'authenticationFailure' specified as a MIB symbol -# do not include any additional managed object information -# -# SNMPv3 TRAPs requires pre-sharing the Notification Originator's -# value of SnmpEngineId with Notification Receiver. To facilitate that -# we will use static (e.g. not autogenerated) version of snmpEngineId. -# Thus we re-create SnmpEngine and NotificationOriginator alone. -# -from pysnmp.entity import engine -from pysnmp.entity.rfc3413 import context -from pysnmp.entity.rfc3413.oneliner import ntforg -from pysnmp.proto import rfc1902 - -# This SNMP Engine ID value should also be configured to TRAP receiver. -snmpEngineId = rfc1902.OctetString(hexValue='8000000001020304') - -ntfOrg = ntforg.NotificationOriginator(engine.SnmpEngine(snmpEngineId)) - -errorIndication = ntfOrg.sendNotification( - ntforg.UsmUserData('usr-sha-aes', 'authkey1', 'privkey1', - authProtocol=ntforg.usmHMACSHAAuthProtocol, - privProtocol=ntforg.usmAesCfb128Protocol), - ntforg.Udp6TransportTarget(('::1', 162)), - 'trap', - ntforg.MibVariable('SNMPv2-MIB', 'authenticationFailure') -) - -if errorIndication: - print('Notification not sent: %s' % errorIndication) diff --git a/examples/v3arch/oneliner/agent/ntforg/inform-v2c-with-mib-lookup.py b/examples/v3arch/oneliner/agent/ntforg/inform-v2c-with-mib-lookup.py new file mode 100644 index 0000000..3e3951c --- /dev/null +++ b/examples/v3arch/oneliner/agent/ntforg/inform-v2c-with-mib-lookup.py @@ -0,0 +1,36 @@ +# +# Notification Originator +# +# Send SNMP notification using the following options: +# +# * SNMPv2c +# * with community name 'public' +# * over IPv4/UDP +# * send TRAP notification +# * with TRAP ID 'coldStart' specified as a MIB symbol +# * include managed object information specified as a MIB symbol +# * perform response OIDs and values resolution at MIB +# +from pysnmp.entity.rfc3413.oneliner import ntforg + +ntfOrg = ntforg.NotificationOriginator() + +errorIndication, errorStatus, errorIndex, varBinds = ntfOrg.sendNotification( + ntforg.CommunityData('public'), + ntforg.UdpTransportTarget(('localhost', 162)), + 'inform', + ntforg.MibVariable('SNMPv2-MIB', 'coldStart'), + ( ntforg.MibVariable('SNMPv2-MIB', 'sysName', 0), 'my system' ), + lookupNames=True, lookupValues=True +) + +if errorIndication: + print('Notification not sent: %s' % errorIndication) +elif errorStatus: + print('Notification Receiver returned error: %s @%s' % + (errorStatus, errorIndex)) +else: + for name, val in varBinds: + print('%s = %s' % (name.prettyPrint(), val.prettyPrint())) + + diff --git a/examples/v3arch/oneliner/agent/ntforg/inform-v3-with-custom-contextengineid.py b/examples/v3arch/oneliner/agent/ntforg/inform-v3-with-custom-contextengineid.py new file mode 100644 index 0000000..60ab7b2 --- /dev/null +++ b/examples/v3arch/oneliner/agent/ntforg/inform-v3-with-custom-contextengineid.py @@ -0,0 +1,43 @@ +# +# Notification Originator +# +# Send SNMP notification using the following options: +# +# * SNMPv3 +# * with user 'usr-md5-none', MD5 auth, no priv +# * send INFORM notification +# * in behalf of contextEngineId 0x8000000004030201, contextName '' +# * over IPv4/UDP +# * with TRAP ID 'warmStart' specified as a string OID +# +# Sending SNMPv3 Notification in behalf of non-default ContextEngineId +# requires having a collection of Managed Objects registered under +# the ContextEngineId being used. +# +from pysnmp.entity import engine +from pysnmp.entity.rfc3413 import context +from pysnmp.entity.rfc3413.oneliner import ntforg +from pysnmp.proto import rfc1902 + +snmpEngine = engine.SnmpEngine() +snmpContext = context.SnmpContext( + snmpEngine,contextEngineId=rfc1902.OctetString(hexValue='8000000004030201') +) + +ntfOrg = ntforg.NotificationOriginator(snmpEngine, snmpContext) + +errorIndication, errorStatus, errorIndex, varBinds = ntfOrg.sendNotification( + ntforg.UsmUserData('usr-md5-none', 'authkey1'), + ntforg.UdpTransportTarget(('localhost', 162)), + 'inform', + '1.3.6.1.6.3.1.1.5.2' +) + +if errorIndication: + print('Notification not sent: %s' % errorIndication) +elif errorStatus: + print('Notification Receiver returned error: %s @%s' % + (errorStatus, errorIndex)) +else: + for name, val in varBinds: + print('%s = %s' % (name.prettyPrint(), val.prettyPrint())) diff --git a/examples/v3arch/oneliner/agent/ntforg/inform-v3-with-custom-contextname.py b/examples/v3arch/oneliner/agent/ntforg/inform-v3-with-custom-contextname.py new file mode 100644 index 0000000..1d1085e --- /dev/null +++ b/examples/v3arch/oneliner/agent/ntforg/inform-v3-with-custom-contextname.py @@ -0,0 +1,43 @@ +# +# Notification Originator +# +# Send SNMP notification using the following options: +# +# * SNMPv3 +# * with user 'usr-md5-none', MD5 auth, no priv +# * send INFORM notification +# * in behalf of contextEngineId = SnmpEngineId, contextName 'my-context' +# * over IPv4/UDP +# * with TRAP ID 'warmStart' specified as a string OID +# +# Sending SNMPv3 Notification in behalf of non-default ContextName +# requires having a collection of Managed Objects registered under +# the ContextName being used. +# +from pysnmp.entity import engine +from pysnmp.entity.rfc3413 import context +from pysnmp.entity.rfc3413.oneliner import ntforg + +snmpEngine = engine.SnmpEngine() +snmpContext = context.SnmpContext(snmpEngine) + +# register default collection of Managed Objects under new contextName +snmpContext.registerContextName('my-context', snmpContext.getMibInstrum()) + +ntfOrg = ntforg.NotificationOriginator(snmpEngine, snmpContext) + +errorIndication, errorStatus, errorIndex, varBinds = ntfOrg.sendNotification( + ntforg.UsmUserData('usr-md5-none', 'authkey1'), + ntforg.UdpTransportTarget(('localhost', 162)), + 'inform', + '1.3.6.1.6.3.1.1.5.2', + contextName='my-context' +) + +if errorIndication: + print('Notification not sent: %s' % errorIndication) +elif errorStatus: + print('Notification Receiver returned error: %s @%s' % (errorStatus, errorIndex)) +else: + for name, val in varBinds: + print('%s = %s' % (name.prettyPrint(), val.prettyPrint())) diff --git a/examples/v3arch/oneliner/agent/ntforg/inform-v3.py b/examples/v3arch/oneliner/agent/ntforg/inform-v3.py new file mode 100644 index 0000000..ddfc7dd --- /dev/null +++ b/examples/v3arch/oneliner/agent/ntforg/inform-v3.py @@ -0,0 +1,33 @@ +## +# Notification Originator +# +# Send SNMP notification using the following options: +# +# * SNMPv3 +# * with user 'usr-md5-des', auth: MD5, priv 3DES +# * 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' +# +from pysnmp.entity.rfc3413.oneliner import ntforg +from pysnmp.proto import rfc1902 + +ntfOrg = ntforg.NotificationOriginator() + +errorIndication, errorStatus, errorIndex, varBinds = ntfOrg.sendNotification( + ntforg.UsmUserData('usr-md5-des', 'authkey1', 'privkey1'), + ntforg.UdpTransportTarget(('localhost', 162)), + 'inform', + '1.3.6.1.6.3.1.1.5.2', + ('1.3.6.1.2.1.1.5.0', rfc1902.OctetString('system name')) +) + +if errorIndication: + print('Notification not sent: %s' % errorIndication) +elif errorStatus: + print('Notification Receiver returned error: %s @%s' % + (errorStatus, errorIndex)) +else: + for name, val in varBinds: + print('%s = %s' % (name.prettyPrint(), val.prettyPrint())) diff --git a/examples/v3arch/oneliner/agent/ntforg/trap-v1-with-custom-values.py b/examples/v3arch/oneliner/agent/ntforg/trap-v1-with-custom-values.py new file mode 100644 index 0000000..e2ba44a --- /dev/null +++ b/examples/v3arch/oneliner/agent/ntforg/trap-v1-with-custom-values.py @@ -0,0 +1,33 @@ +# +# Notification Originator +# +# Send SNMP notification using the following options: +# +# * SNMPv1 +# * with community name 'public' +# * over IPv4/UDP +# * send TRAP notification +# * with Generic Trap #6 (enterpriseSpecific) and Specific Trap 432 +# * overriding Uptime value with 12345 +# * overriding Agent Address with '127.0.0.1' +# * overriding Enterprise OID with 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' +# +from pysnmp.entity.rfc3413.oneliner import ntforg +from pysnmp.proto import rfc1902 + +ntfOrg = ntforg.NotificationOriginator() + +errorIndication = ntfOrg.sendNotification( + ntforg.CommunityData('public', mpModel=0), + ntforg.UdpTransportTarget(('localhost', 162)), + 'trap', + '1.3.6.1.4.1.20408.4.1.1.2.0.432', + ('1.3.6.1.2.1.1.3.0', 12345), + ('1.3.6.1.6.3.18.1.3.0', '127.0.0.1'), + ('1.3.6.1.6.3.1.1.4.3.0', '1.3.6.1.4.1.20408.4.1.1.2'), + ('1.3.6.1.2.1.1.1.0', rfc1902.OctetString('my system')) +) + +if errorIndication: + print('Notification not sent: %s' % errorIndication) diff --git a/examples/v3arch/oneliner/agent/ntforg/trap-v1-with-default-values.py b/examples/v3arch/oneliner/agent/ntforg/trap-v1-with-default-values.py new file mode 100644 index 0000000..4a845f4 --- /dev/null +++ b/examples/v3arch/oneliner/agent/ntforg/trap-v1-with-default-values.py @@ -0,0 +1,31 @@ +# +# Notification Originator +# +# Send SNMP notification using the following options: +# +# * SNMPv1 +# * with community name 'public' +# * over IPv4/UDP +# * send TRAP notification +# * with Generic Trap #1 (warmStart) and Specific Trap 0 +# * with default Uptime +# * with default Agent Address +# * 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' +# +from pysnmp.entity.rfc3413.oneliner import ntforg +from pysnmp.proto import rfc1902 + +ntfOrg = ntforg.NotificationOriginator() + +errorIndication = ntfOrg.sendNotification( + ntforg.CommunityData('public', mpModel=0), + ntforg.UdpTransportTarget(('localhost', 162)), + 'trap', + '1.3.6.1.6.3.1.1.5.2', + ('1.3.6.1.6.3.1.1.4.3.0', '1.3.6.1.4.1.20408.4.1.1.2'), + ('1.3.6.1.2.1.1.1.0', rfc1902.OctetString('my system')) +) + +if errorIndication: + print('Notification not sent: %s' % errorIndication) diff --git a/examples/v3arch/oneliner/agent/ntforg/trap-v2c-with-mib-lookup.py b/examples/v3arch/oneliner/agent/ntforg/trap-v2c-with-mib-lookup.py new file mode 100644 index 0000000..7d01883 --- /dev/null +++ b/examples/v3arch/oneliner/agent/ntforg/trap-v2c-with-mib-lookup.py @@ -0,0 +1,26 @@ +# +# Notification Originator +# +# Send SNMP notification using the following options: +# +# * SNMPv2c +# * with community name 'public' +# * over IPv4/UDP +# * send TRAP notification +# * with TRAP ID 'coldStart' specified as a MIB symbol +# * include managed object information specified as a MIB symbol +# +from pysnmp.entity.rfc3413.oneliner import ntforg + +ntfOrg = ntforg.NotificationOriginator() + +errorIndication = ntfOrg.sendNotification( + ntforg.CommunityData('public'), + ntforg.UdpTransportTarget(('localhost', 162)), + 'trap', + ntforg.MibVariable('SNMPv2-MIB', 'coldStart'), + ( ntforg.MibVariable('SNMPv2-MIB', 'sysName', 0), 'new name' ) +) + +if errorIndication: + print('Notification not sent: %s' % errorIndication) diff --git a/examples/v3arch/oneliner/agent/ntforg/trap-v3-with-custom-engineid.py b/examples/v3arch/oneliner/agent/ntforg/trap-v3-with-custom-engineid.py new file mode 100644 index 0000000..98060f5 --- /dev/null +++ b/examples/v3arch/oneliner/agent/ntforg/trap-v3-with-custom-engineid.py @@ -0,0 +1,38 @@ +# +# Notification Originator +# +# Send SNMP notification using the following options: +# +# * SNMPv3 +# * with local snmpEngineId = 0x8000000001020304 +# * with user 'usr-sha-aes', auth: SHA, priv: AES128 +# * over IPv6/UDP +# * send TRAP notification +# * with TRAP ID 'authenticationFailure' specified as a MIB symbol +# * do not include any additional managed object information +# +# SNMPv3 TRAPs requires pre-sharing the Notification Originator's +# value of SnmpEngineId with Notification Receiver. To facilitate that +# we will use static (e.g. not autogenerated) version of snmpEngineId. +# +from pysnmp.entity import engine +from pysnmp.entity.rfc3413 import context +from pysnmp.entity.rfc3413.oneliner import ntforg +from pysnmp.proto import rfc1902 + +# This SNMP Engine ID value should also be configured to TRAP receiver. +snmpEngineId = rfc1902.OctetString(hexValue='8000000001020304') + +ntfOrg = ntforg.NotificationOriginator(engine.SnmpEngine(snmpEngineId)) + +errorIndication = ntfOrg.sendNotification( + ntforg.UsmUserData('usr-sha-aes', 'authkey1', 'privkey1', + authProtocol=ntforg.usmHMACSHAAuthProtocol, + privProtocol=ntforg.usmAesCfb128Protocol), + ntforg.Udp6TransportTarget(('::1', 162)), + 'trap', + ntforg.MibVariable('SNMPv2-MIB', 'authenticationFailure') +) + +if errorIndication: + print('Notification not sent: %s' % errorIndication) diff --git a/examples/v3arch/oneliner/manager/bulkgen.py b/examples/v3arch/oneliner/manager/bulkgen.py deleted file mode 100644 index ff11c20..0000000 --- a/examples/v3arch/oneliner/manager/bulkgen.py +++ /dev/null @@ -1,101 +0,0 @@ -# Various GETBULK Command Generator uses -from pysnmp.entity.rfc3413.oneliner import cmdgen - -cmdGen = cmdgen.CommandGenerator() - -# Send a series of SNMP GETBULK requests -# with SNMPv2c, community 'public' -# over IPv4/UDP -# to an Agent at localhost:161 -# with values non-repeaters = 0, max-repetitions = 25 -# for two OIDs in string form -# stop when response OIDs leave the scopes of initial OIDs -errorIndication, errorStatus, errorIndex, varBindTable = cmdGen.bulkCmd( - cmdgen.CommunityData('public'), - cmdgen.UdpTransportTarget(('localhost', 161)), - 0, 25, - '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] or '?' - ) - ) - else: - for varBindTableRow in varBindTable: - for name, val in varBindTableRow: - print('%s = %s' % (name.prettyPrint(), val.prettyPrint())) - - -# Send a series of SNMP GETBULK requests -# with SNMPv3 with user 'usr-md5-des', MD5 auth and DES privacy protocols -# over IPv6/UDP -# to an Agent at [::1]:161 -# 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 OR maxRows == 20 -# perform response OIDs and values resolution at MIB -# 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.MibVariable('IP-MIB', 'ipAdEntAddr'), - cmdgen.MibVariable('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] or '?' - ) - ) - else: - for varBindTableRow in varBindTable: - for name, val in varBindTableRow: - print('%s = %s' % (name.prettyPrint(), val.prettyPrint())) - - -# Send a series of SNMP GETBULK requests -# with SNMPv3, user 'usr-none-none', no authentication, no privacy -# over IPv4/UDP -# to an Agent at localhost:161 -# for all OIDs past TCP-MIB::tcpConnTable -# TCP-MIB will be searched by a user-specified path -# run till end-of-mib condition is reported by Agent OR maxRows == 20 -# ignoring non-increasing OIDs whenever reported by Agent -# make sure IF-MIB.py is search path - -errorIndication, errorStatus, errorIndex, varBindTable = cmdGen.bulkCmd( - cmdgen.UsmUserData('usr-none-none'), - cmdgen.UdpTransportTarget(('localhost', 161)), - 0, 50, - cmdgen.MibVariable('TCP-MIB', 'tcpConnTable').addMibSource('/tmp/mymibs'), - 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] or '?' - ) - ) - else: - for varBindTableRow in varBindTable: - for name, val in varBindTableRow: - print('%s = %s' % (name.prettyPrint(), val.prettyPrint())) - diff --git a/examples/v3arch/oneliner/manager/async/getgen.py b/examples/v3arch/oneliner/manager/cmdgen/get-async-multiple-transports-and-protocols.py index 538cb72..7714a7e 100644 --- a/examples/v3arch/oneliner/manager/async/getgen.py +++ b/examples/v3arch/oneliner/manager/cmdgen/get-async-multiple-transports-and-protocols.py @@ -1,7 +1,21 @@ -# GET Command Generator -# Sends a number of GET queries to various Agents +# +# Asynchronous Command Generator +# +# Send a bunch of SNMP GET requests all at once using the following options: +# +# * with SNMPv1, community 'public' and +# with SNMPv2c, community public and +# with SNMPv3, user 'usr-md5-des', MD5 auth and DES privacy +# * over IPv4/UDP and +# over IPv6/UDP +# * to an Agent at localhost:161 and +# to an Agent at [::1]:161 +# * for instances of SNMPv2-MIB::sysDescr.0 and +# SNMPv2-MIB::sysLocation.0 MIB objects +# from pysnmp.entity.rfc3413.oneliner import cmdgen +# List of targets in the followin format: # ( ( authData, transportTarget, varNames ), ... ) targets = ( # 1-st target (SNMPv1 over IPv4/UDP) @@ -28,6 +42,7 @@ targets = ( # ... ) +# Wait for responses or errors def cbFun(sendRequestHandle, errorIndication, errorStatus, errorIndex, varBinds, cbCtx): (authData, transportTarget) = cbCtx @@ -51,6 +66,7 @@ def cbFun(sendRequestHandle, errorIndication, errorStatus, errorIndex, cmdGen = cmdgen.AsynCommandGenerator() +# Submit GET requests for authData, transportTarget, varNames in targets: cmdGen.getCmd( authData, transportTarget, varNames, diff --git a/examples/v3arch/oneliner/manager/cmdgen/get-v1.py b/examples/v3arch/oneliner/manager/cmdgen/get-v1.py new file mode 100644 index 0000000..6bfc6fb --- /dev/null +++ b/examples/v3arch/oneliner/manager/cmdgen/get-v1.py @@ -0,0 +1,36 @@ +# +# Command Generator +# +# Send SNMP GET request using the following options: +# +# * with SNMPv1, community 'public' +# * over IPv4/UDP +# * to an Agent at localhost:161 +# * 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 + +cmdGen = cmdgen.CommandGenerator() + +errorIndication, errorStatus, errorIndex, varBinds = cmdGen.getCmd( + cmdgen.CommunityData('public', mpModel=0), + cmdgen.UdpTransportTarget(('localhost', 161)), + cmdgen.MibVariable('iso.org.dod.internet.mgmt.mib-2.system.sysDescr.0'), + cmdgen.MibVariable('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] or '?' + ) + ) + else: + for name, val in varBinds: + print('%s = %s' % (name.prettyPrint(), val.prettyPrint())) + diff --git a/examples/v3arch/oneliner/manager/cmdgen/get-v2c.py b/examples/v3arch/oneliner/manager/cmdgen/get-v2c.py new file mode 100644 index 0000000..60bf91f --- /dev/null +++ b/examples/v3arch/oneliner/manager/cmdgen/get-v2c.py @@ -0,0 +1,34 @@ +# +# Command Generator +# +# Send SNMP GET request using the following options: +# +# * with SNMPv2c, community 'public' +# * over IPv4/UDP +# * to an Agent at localhost:161 +# * for two OIDs in string form +# +from pysnmp.entity.rfc3413.oneliner import cmdgen + +cmdGen = cmdgen.CommandGenerator() + +errorIndication, errorStatus, errorIndex, varBinds = cmdGen.getCmd( + cmdgen.CommunityData('public'), + cmdgen.UdpTransportTarget(('localhost', 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] or '?' + ) + ) + else: + for name, val in varBinds: + print('%s = %s' % (name.prettyPrint(), val.prettyPrint())) diff --git a/examples/v3arch/oneliner/manager/cmdgen/get-v3-over-ipv6-with-mib-lookup.py b/examples/v3arch/oneliner/manager/cmdgen/get-v3-over-ipv6-with-mib-lookup.py new file mode 100644 index 0000000..dba8174 --- /dev/null +++ b/examples/v3arch/oneliner/manager/cmdgen/get-v3-over-ipv6-with-mib-lookup.py @@ -0,0 +1,36 @@ +# +# Command Generator +# +# Send SNMP GET request using the following options: +# +# * with SNMPv3 with user 'usr-md5-des', MD5 auth and DES privacy protocols +# * over IPv6/UDP +# * to an Agent at [::1]:161 +# * for three OIDs: one passed as a MibVariable object while others are +# * in string form +# +from pysnmp.entity.rfc3413.oneliner import cmdgen + +cmdGen = cmdgen.CommandGenerator() + +errorIndication, errorStatus, errorIndex, varBinds = cmdGen.getCmd( + cmdgen.UsmUserData('usr-md5-des', 'authkey1', 'privkey1'), + cmdgen.Udp6TransportTarget(('::1', 161)), + cmdgen.MibVariable('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] or '?' + ) + ) + else: + for name, val in varBinds: + print('%s = %s' % (name.prettyPrint(), val.prettyPrint())) diff --git a/examples/v3arch/oneliner/manager/cmdgen/get-v3-table-object-by-index.py b/examples/v3arch/oneliner/manager/cmdgen/get-v3-table-object-by-index.py new file mode 100644 index 0000000..9720b6e --- /dev/null +++ b/examples/v3arch/oneliner/manager/cmdgen/get-v3-table-object-by-index.py @@ -0,0 +1,35 @@ +# +# Command Generator +# +# Send SNMP GET request using the following options: +# +# * with SNMPv3, user 'usr-none-none', no authentication, no privacy +# * over IPv4/UDP +# * to an Agent at localhost:161 +# * for IP-MIB::ipAdEntAddr.127.0.0.1 MIB object +# * perform response OIDs and values resolution at MIB +# +from pysnmp.entity.rfc3413.oneliner import cmdgen + +cmdGen = cmdgen.CommandGenerator() + +errorIndication, errorStatus, errorIndex, varBinds = cmdGen.getCmd( + cmdgen.UsmUserData('usr-none-none'), + cmdgen.UdpTransportTarget(('localhost', 161)), + cmdgen.MibVariable('IP-MIB', 'ipAdEntAddr', '127.0.0.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] or '?' + ) + ) + else: + for name, val in varBinds: + print('%s = %s' % (name.prettyPrint(), val.prettyPrint())) diff --git a/examples/v3arch/oneliner/manager/cmdgen/get-v3-table-object-by-multiple-indices.py b/examples/v3arch/oneliner/manager/cmdgen/get-v3-table-object-by-multiple-indices.py new file mode 100644 index 0000000..9d06642 --- /dev/null +++ b/examples/v3arch/oneliner/manager/cmdgen/get-v3-table-object-by-multiple-indices.py @@ -0,0 +1,35 @@ +# +# Command Generator +# +# Send SNMP GET request using the following options: +# +# * with SNMPv3, user 'usr-sha-aes128', SHA auth, AES128 privacy +# * over IPv4/UDP +# * to an Agent at localhost:161 +# * for TCP-MIB::tcpConnLocalAddress."0.0.0.0".22."0.0.0.0".0 MIB object +# +from pysnmp.entity.rfc3413.oneliner import cmdgen + +cmdGen = cmdgen.CommandGenerator() + +errorIndication, errorStatus, errorIndex, varBinds = cmdGen.getCmd( + cmdgen.UsmUserData('usr-sha-aes128', 'authkey1', 'privkey1', + authProtocol=cmdgen.usmHMACSHAAuthProtocol, + privProtocol=cmdgen.usmAesCfb128Protocol ), + cmdgen.UdpTransportTarget(('localhost', 161)), + cmdgen.MibVariable('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] or '?' + ) + ) + else: + for name, val in varBinds: + print('%s = %s' % (name.prettyPrint(), val.prettyPrint())) diff --git a/examples/v3arch/oneliner/manager/cmdgen/get-v3-with-mib-lookup.py b/examples/v3arch/oneliner/manager/cmdgen/get-v3-with-mib-lookup.py new file mode 100644 index 0000000..65d3198 --- /dev/null +++ b/examples/v3arch/oneliner/manager/cmdgen/get-v3-with-mib-lookup.py @@ -0,0 +1,33 @@ +# +# Command Generator +# +# Send SNMP GET request using the following options: +# +# * with SNMPv3, user 'usr-md5-none', MD5 authentication, no privacy +# * over IPv4/UDP +# * to an Agent at localhost:161 +# * for IP-MIB::ipAdEntAddr.127.0.0.1 MIB object +# +from pysnmp.entity.rfc3413.oneliner import cmdgen + +cmdGen = cmdgen.CommandGenerator() + +errorIndication, errorStatus, errorIndex, varBinds = cmdGen.getCmd( + cmdgen.UsmUserData('usr-md5-none', 'authkey1'), + cmdgen.UdpTransportTarget(('localhost', 161)), + cmdgen.MibVariable('IP-MIB', 'ipAdEntAddr', '127.0.0.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] or '?' + ) + ) + else: + for name, val in varBinds: + print('%s = %s' % (name.prettyPrint(), val.prettyPrint())) diff --git a/examples/v3arch/oneliner/manager/cmdgen/getbulk-v2c.py b/examples/v3arch/oneliner/manager/cmdgen/getbulk-v2c.py new file mode 100644 index 0000000..731c9cf --- /dev/null +++ b/examples/v3arch/oneliner/manager/cmdgen/getbulk-v2c.py @@ -0,0 +1,37 @@ +# +# Command Generator +# +# Send SNMP GETBULK request using the following options: +# +# * with SNMPv2c, community 'public' +# * over IPv4/UDP +# * to an Agent at localhost:161 +# * with values non-repeaters = 0, max-repetitions = 25 +# * for two OIDs in string form +# * stop when response OIDs leave the scopes of initial OIDs +# +from pysnmp.entity.rfc3413.oneliner import cmdgen + +cmdGen = cmdgen.CommandGenerator() + +errorIndication, errorStatus, errorIndex, varBindTable = cmdGen.bulkCmd( + cmdgen.CommunityData('public'), + cmdgen.UdpTransportTarget(('localhost', 161)), + 0, 25, + '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] or '?' + ) + ) + else: + for varBindTableRow in varBindTable: + for name, val in varBindTableRow: + print('%s = %s' % (name.prettyPrint(), val.prettyPrint())) diff --git a/examples/v3arch/oneliner/manager/cmdgen/getbulk-v3-over-ipv6-with-mib-lookup.py b/examples/v3arch/oneliner/manager/cmdgen/getbulk-v3-over-ipv6-with-mib-lookup.py new file mode 100644 index 0000000..65f46b2 --- /dev/null +++ b/examples/v3arch/oneliner/manager/cmdgen/getbulk-v3-over-ipv6-with-mib-lookup.py @@ -0,0 +1,42 @@ +# +# Command Generator +# +# Send SNMP GETBULK request using the following options: +# +# * with SNMPv3 with user 'usr-md5-des', MD5 auth and DES privacy protocols +# * over IPv6/UDP +# * to an Agent at [::1]:161 +# * 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 OR maxRows == 20 +# * perform response OIDs and values resolution at MIB +# +from pysnmp.entity.rfc3413.oneliner import cmdgen + +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.MibVariable('IP-MIB', 'ipAdEntAddr'), + cmdgen.MibVariable('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] or '?' + ) + ) + else: + for varBindTableRow in varBindTable: + for name, val in varBindTableRow: + print('%s = %s' % (name.prettyPrint(), val.prettyPrint())) diff --git a/examples/v3arch/oneliner/manager/cmdgen/getbulk-v3-with-custom-mib-path-and-options.py b/examples/v3arch/oneliner/manager/cmdgen/getbulk-v3-with-custom-mib-path-and-options.py new file mode 100644 index 0000000..9920530 --- /dev/null +++ b/examples/v3arch/oneliner/manager/cmdgen/getbulk-v3-with-custom-mib-path-and-options.py @@ -0,0 +1,40 @@ +# +# Command Generator +# +# Send SNMP GETBULK request using the following options: +# +# * with SNMPv3, user 'usr-none-none', no authentication, no privacy +# * over IPv4/UDP +# * to an Agent at localhost:161 +# * for all OIDs past TCP-MIB::tcpConnTable +# * TCP-MIB will be searched by a user-specified path +# * run till end-of-mib condition is reported by Agent OR maxRows == 20 +# * 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(('localhost', 161)), + 0, 50, + cmdgen.MibVariable('TCP-MIB', 'tcpConnTable').addMibSource('/tmp/mymibs'), + 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] or '?' + ) + ) + else: + for varBindTableRow in varBindTable: + for name, val in varBindTableRow: + print('%s = %s' % (name.prettyPrint(), val.prettyPrint())) diff --git a/examples/v3arch/oneliner/manager/async/nextgen.py b/examples/v3arch/oneliner/manager/cmdgen/getnext-async-multiple-transports-and-protocols.py index bab7ceb..3de91eb 100644 --- a/examples/v3arch/oneliner/manager/async/nextgen.py +++ b/examples/v3arch/oneliner/manager/cmdgen/getnext-async-multiple-transports-and-protocols.py @@ -1,7 +1,21 @@ -# GETNEXT Command Generator +## +# Asynchronous Command Generator +# +# Send a bunch of SNMP GETNEXT requests all at once using the following options: +# +# * with SNMPv1, community 'public' and +# with SNMPv2c, community public and +# with SNMPv3, user 'usr-md5-des', MD5 auth and DES privacy +# * over IPv4/UDP and +# over IPv6/UDP +# * to an Agent at localhost:161 and +# to an Agent at [::1]:161 +# * for multiple MIB subtrees and tables +# from pysnmp.entity.rfc3413.oneliner import cmdgen from pysnmp.proto import rfc1902 +# List of targets in the followin format: # ( ( authData, transportTarget, varNames ), ... ) targets = ( # 1-st target (SNMPv1 over IPv4/UDP) @@ -24,6 +38,7 @@ targets = ( # ... ) +# Wait for responses or errors, submit GETNEXT requests for further OIDs def cbFun(sendRequestHandle, errorIndication, errorStatus, errorIndex, varBindTable, cbCtx): (varBindHead, authData, transportTarget) = cbCtx @@ -47,7 +62,7 @@ def cbFun(sendRequestHandle, errorIndication, errorStatus, errorIndex, else: print('went out of table at %s' % (name, )) return - + for varBindRow in varBindTable: for oid, val in varBindRow: if val is None: @@ -59,6 +74,7 @@ def cbFun(sendRequestHandle, errorIndication, errorStatus, errorIndex, cmdGen = cmdgen.AsynCommandGenerator() +# Submit initial GETNEXT requests and wait for responses for authData, transportTarget, varNames in targets: varBindHead = [ x[0] for x in cmdGen.makeReadVarBinds(varNames) ] cmdGen.nextCmd( diff --git a/examples/v3arch/oneliner/manager/cmdgen/getnext-v1.py b/examples/v3arch/oneliner/manager/cmdgen/getnext-v1.py new file mode 100644 index 0000000..2b1bf45 --- /dev/null +++ b/examples/v3arch/oneliner/manager/cmdgen/getnext-v1.py @@ -0,0 +1,40 @@ +# +# Command Generator +# +# Send SNMP GETNEXT request using the following options: +# +# * with SNMPv1, community 'public' +# * over IPv4/UDP +# * to an Agent at localhost:161 +# * 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(('localhost', 161)), + cmdgen.MibVariable('IF-MIB', 'ifDescr'), + cmdgen.MibVariable('IF-MIB', 'ifType'), + cmdgen.MibVariable('IF-MIB', 'ifMtu'), + cmdgen.MibVariable('IF-MIB', 'ifSpeed'), + cmdgen.MibVariable('IF-MIB', 'ifPhysAddress') +) + +if errorIndication: + print(errorIndication) +else: + if errorStatus: + print('%s at %s' % ( + errorStatus.prettyPrint(), + errorIndex and varBindTable[-1][int(errorIndex)-1] or '?' + ) + ) + else: + for varBindTableRow in varBindTable: + for name, val in varBindTableRow: + print('%s = %s' % (name.prettyPrint(), val.prettyPrint())) diff --git a/examples/v3arch/oneliner/manager/cmdgen/getnext-v2c.py b/examples/v3arch/oneliner/manager/cmdgen/getnext-v2c.py new file mode 100644 index 0000000..d148e24 --- /dev/null +++ b/examples/v3arch/oneliner/manager/cmdgen/getnext-v2c.py @@ -0,0 +1,35 @@ +# +# Command Generator +# +# Send SNMP GETNEXT requests using the following options: +# +# * with SNMPv2c, community 'public' +# * over IPv4/UDP +# * to an Agent at localhost:161 +# * for two OIDs in string form +# * stop when response OIDs leave the scopes of initial OIDs +# +from pysnmp.entity.rfc3413.oneliner import cmdgen + +cmdGen = cmdgen.CommandGenerator() + +errorIndication, errorStatus, errorIndex, varBindTable = cmdGen.nextCmd( + cmdgen.CommunityData('public'), + cmdgen.UdpTransportTarget(('localhost', 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] or '?' + ) + ) + else: + for varBindTableRow in varBindTable: + for name, val in varBindTableRow: + print('%s = %s' % (name.prettyPrint(), val.prettyPrint())) diff --git a/examples/v3arch/oneliner/manager/cmdgen/getnext-v3-over-ipv6-with-mib-lookup.py b/examples/v3arch/oneliner/manager/cmdgen/getnext-v3-over-ipv6-with-mib-lookup.py new file mode 100644 index 0000000..e8ec42f --- /dev/null +++ b/examples/v3arch/oneliner/manager/cmdgen/getnext-v3-over-ipv6-with-mib-lookup.py @@ -0,0 +1,38 @@ +# +# Command Generator +# +# Send SNMP GETNEXT requests using the following options: +# +# * with SNMPv3 with user 'usr-md5-des', MD5 auth and DES privacy protocols +# * over IPv6/UDP +# * to an Agent at [::1]:161 +# * for all columns of the IF-MIB::ifEntry table +# * stop when response OIDs leave the scopes of the table +# * perform response OIDs and 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-des', 'authkey1', 'privkey1'), + cmdgen.Udp6TransportTarget(('::1', 161)), + cmdgen.MibVariable('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] or '?' + ) + ) + else: + for varBindTableRow in varBindTable: + for name, val in varBindTableRow: + print('%s = %s' % (name.prettyPrint(), val.prettyPrint())) diff --git a/examples/v3arch/oneliner/manager/cmdgen/getnext-v3-pull-whole-mib-with-mib-lookup.py b/examples/v3arch/oneliner/manager/cmdgen/getnext-v3-pull-whole-mib-with-mib-lookup.py new file mode 100644 index 0000000..46c5ae4 --- /dev/null +++ b/examples/v3arch/oneliner/manager/cmdgen/getnext-v3-pull-whole-mib-with-mib-lookup.py @@ -0,0 +1,38 @@ +# +# Command Generator +# +# Send SNMP GETNEXT requests using the following options: +# +# * with SNMPv3, user 'usr-md5-none', MD5 authentication, no privacy +# * over IPv4/UDP +# * to an Agent at localhost:161 +# * for all OIDs in IF-MIB +# * 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(('localhost', 161)), + cmdgen.MibVariable('IF-MIB', ''), + lookupValues=True +) + +if errorIndication: + print(errorIndication) +else: + if errorStatus: + print('%s at %s' % ( + errorStatus.prettyPrint(), + errorIndex and varBindTable[-1][int(errorIndex)-1] or '?' + ) + ) + else: + for varBindTableRow in varBindTable: + for name, val in varBindTableRow: + print('%s = %s' % (name.prettyPrint(), val.prettyPrint())) diff --git a/examples/v3arch/oneliner/manager/cmdgen/getnext-v3-pull-whole-mib-with-options.py b/examples/v3arch/oneliner/manager/cmdgen/getnext-v3-pull-whole-mib-with-options.py new file mode 100644 index 0000000..c5543c4 --- /dev/null +++ b/examples/v3arch/oneliner/manager/cmdgen/getnext-v3-pull-whole-mib-with-options.py @@ -0,0 +1,41 @@ +# +# Command Generator +# +# Send SNMP GETNEXT requests using the following options: +# +# * with SNMPv3, user 'usr-sha-aes128', SHA auth, AES128 privacy +# * over Local Domain Sockets +# * to an Agent at localhost:161 +# * for all OIDs past IF-MIB +# * run till end-of-mib condition is reported by Agent OR maxRows == 100 +# * 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.nextCmd( + cmdgen.UsmUserData('usr-sha-aes128', 'authkey1', 'privkey1', + authProtocol=cmdgen.usmHMACSHAAuthProtocol, + privProtocol=cmdgen.usmAesCfb128Protocol), + cmdgen.UdpTransportTarget(('localhost', 161)), + cmdgen.MibVariable('IF-MIB', ''), + 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] or '?' + ) + ) + else: + for varBindTableRow in varBindTable: + for name, val in varBindTableRow: + print('%s = %s' % (name.prettyPrint(), val.prettyPrint())) diff --git a/examples/v3arch/oneliner/manager/cmdgen/set-v1-multiple-values.py b/examples/v3arch/oneliner/manager/cmdgen/set-v1-multiple-values.py new file mode 100644 index 0000000..846ce30 --- /dev/null +++ b/examples/v3arch/oneliner/manager/cmdgen/set-v1-multiple-values.py @@ -0,0 +1,36 @@ +# +# Command Generator +# +# Send SNMP SET request using the following options: +# +# * with SNMPv1, community 'public' +# * over IPv4/UDP +# * to an Agent at localhost:161 +# * setting two OIDs to new values (types explicitly specified) +# +from pysnmp.entity.rfc3413.oneliner import cmdgen +from pysnmp.proto import rfc1902 + +cmdGen = cmdgen.CommandGenerator() + +errorIndication, errorStatus, errorIndex, varBinds = cmdGen.setCmd( + cmdgen.CommunityData('public'), + cmdgen.UdpTransportTarget(('localhost', 161)), + ('1.3.6.1.2.1.1.2.0', rfc1902.ObjectName('1.3.6.1.4.1.20408.1.1')), + ('1.3.6.1.2.1.1.2.0', '1.3.6.1.4.1.20408.1.1'), + ('1.3.6.1.2.1.1.5.0', 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] or '?' + ) + ) + else: + for name, val in varBinds: + print('%s = %s' % (name.prettyPrint(), val.prettyPrint())) diff --git a/examples/v3arch/oneliner/manager/cmdgen/set-v2c-with-value-type-mib-lookup.py b/examples/v3arch/oneliner/manager/cmdgen/set-v2c-with-value-type-mib-lookup.py new file mode 100644 index 0000000..7ac61ba --- /dev/null +++ b/examples/v3arch/oneliner/manager/cmdgen/set-v2c-with-value-type-mib-lookup.py @@ -0,0 +1,34 @@ +# +# Command Generator +# +# Send SNMP SET request using the following options: +# +# * with SNMPv2c, community 'public' +# * over IPv4/UDP +# * to an Agent at localhost:161 +# * setting SNMPv2-MIB::sysName.0 to new value (type taken from MIB) +# +from pysnmp.entity.rfc3413.oneliner import cmdgen +from pysnmp.proto import rfc1902 + +cmdGen = cmdgen.CommandGenerator() + +errorIndication, errorStatus, errorIndex, varBinds = cmdGen.setCmd( + cmdgen.CommunityData('public'), + cmdgen.UdpTransportTarget(('localhost', 161)), + (cmdgen.MibVariable('SNMPv2-MIB', 'sysName', 0), '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] or '?' + ) + ) + else: + for name, val in varBinds: + print('%s = %s' % (name.prettyPrint(), val.prettyPrint())) diff --git a/examples/v3arch/oneliner/manager/cmdgen/set-v3-with-custom-contextengineid-and-contextname.py b/examples/v3arch/oneliner/manager/cmdgen/set-v3-with-custom-contextengineid-and-contextname.py new file mode 100644 index 0000000..71e49d1 --- /dev/null +++ b/examples/v3arch/oneliner/manager/cmdgen/set-v3-with-custom-contextengineid-and-contextname.py @@ -0,0 +1,39 @@ +# +# Command Generator +# +# Send SNMP SET request using the following options: +# +# * with SNMPv3 with user 'usr-none-none', no auth and no privacy protocols +# * over IPv4/UDP +# * to an Agent at localhost:161 +# * addressing particular set of Managed Objects at remote SNMP Engine by: +# * contextEngineId 0x8000000001020304 and +# * contextName 'my-context' +# * setting SNMPv2-MIB::sysName.0 to new value (type taken from MIB) +# +from pysnmp.entity.rfc3413.oneliner import cmdgen +from pysnmp.proto import rfc1902 + +cmdGen = cmdgen.CommandGenerator() + +errorIndication, errorStatus, errorIndex, varBinds = cmdGen.setCmd( + cmdgen.UsmUserData('usr-none-none'), + cmdgen.UdpTransportTarget(('localhost', 161)), + (cmdgen.MibVariable('SNMPv2-MIB', 'sysName', 0), 'new system name'), + contextEngineId=rfc1902.OctetString(hexValue='8000000001020304'), + contextName='my-context' +) + +# 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] or '?' + ) + ) + else: + for name, val in varBinds: + print('%s = %s' % (name.prettyPrint(), val.prettyPrint())) diff --git a/examples/v3arch/oneliner/manager/cmdgen/set-v3-with-custom-contextengineid.py b/examples/v3arch/oneliner/manager/cmdgen/set-v3-with-custom-contextengineid.py new file mode 100644 index 0000000..d865bc4 --- /dev/null +++ b/examples/v3arch/oneliner/manager/cmdgen/set-v3-with-custom-contextengineid.py @@ -0,0 +1,38 @@ +# +# Command Generator +# +# Send SNMP GET request using the following options: +# +# * with SNMPv3 with user 'usr-md5-des', MD5 auth and DES privacy protocols +# * use remote SNMP Engine ID 0x8000000004030201 (USM autodiscovery will run) +# * over IPv4/UDP +# * to an Agent at localhost:161 +# * setting SNMPv2-MIB::sysName.0 to new value (type taken from MIB) +# +from pysnmp.entity.rfc3413.oneliner import cmdgen +from pysnmp.proto import rfc1902 + +cmdGen = cmdgen.CommandGenerator() + +errorIndication, errorStatus, errorIndex, varBinds = cmdGen.setCmd( + cmdgen.UsmUserData( + 'usr-md5-des', 'authkey1', 'privkey1', + contextEngineId=rfc1902.OctetString(hexValue='8000000004030201') + ), + cmdgen.UdpTransportTarget(('localhost', 161)), + (cmdgen.MibVariable('SNMPv2-MIB', 'sysName', 0), '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] or '?' + ) + ) + else: + for name, val in varBinds: + print('%s = %s' % (name.prettyPrint(), val.prettyPrint())) diff --git a/examples/v3arch/oneliner/manager/cmdgen/set-v3-with-value-type-mib-lookup.py b/examples/v3arch/oneliner/manager/cmdgen/set-v3-with-value-type-mib-lookup.py new file mode 100644 index 0000000..5e9ed23 --- /dev/null +++ b/examples/v3arch/oneliner/manager/cmdgen/set-v3-with-value-type-mib-lookup.py @@ -0,0 +1,35 @@ +# +# Command Generator +# +# Send SNMP SET request using the following options: +# +# * with SNMPv3 with user 'usr-md5-des', MD5 auth and DES privacy protocols +# * over IPv4/UDP +# * to an Agent at localhost:161 +# * setting SNMPv2-MIB::sysName.0 to new value (type taken from MIB) +# * perform response OIDs and values resolution at MIB +# +from pysnmp.entity.rfc3413.oneliner import cmdgen + +cmdGen = cmdgen.CommandGenerator() + +errorIndication, errorStatus, errorIndex, varBinds = cmdGen.setCmd( + cmdgen.UsmUserData('usr-md5-des', 'authkey1', 'privkey1'), + cmdgen.UdpTransportTarget(('localhost', 161)), + (cmdgen.MibVariable('SNMPv2-MIB', 'sysName', 0), 'new system name'), + 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] or '?' + ) + ) + else: + for name, val in varBinds: + print('%s = %s' % (name.prettyPrint(), val.prettyPrint())) diff --git a/examples/v3arch/oneliner/manager/getgen.py b/examples/v3arch/oneliner/manager/getgen.py deleted file mode 100644 index 77da6f0..0000000 --- a/examples/v3arch/oneliner/manager/getgen.py +++ /dev/null @@ -1,171 +0,0 @@ -# Various uses of GET Command Generator uses -from pysnmp.entity.rfc3413.oneliner import cmdgen - -cmdGen = cmdgen.CommandGenerator() - -# Send SNMP GET request -# with SNMPv2c, community 'public' -# over IPv4/UDP -# to an Agent at localhost:161 -# for two OIDs in string form -errorIndication, errorStatus, errorIndex, varBinds = cmdGen.getCmd( - cmdgen.CommunityData('public'), - cmdgen.UdpTransportTarget(('localhost', 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] or '?' - ) - ) - else: - for name, val in varBinds: - print('%s = %s' % (name.prettyPrint(), val.prettyPrint())) - - -# Send SNMP GET request -# with SNMPv1, community 'public' -# over IPv4/UDP -# to an Agent at localhost:161 -# for two instances of SNMPv2-MIB::sysDescr.0 MIB object, -# one in label and another in MIB symbol form -errorIndication, errorStatus, errorIndex, varBinds = cmdGen.getCmd( - cmdgen.CommunityData('public', mpModel=0), - cmdgen.UdpTransportTarget(('localhost', 161)), - cmdgen.MibVariable('iso.org.dod.internet.mgmt.mib-2.system.sysDescr.0'), - cmdgen.MibVariable('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] or '?' - ) - ) - else: - for name, val in varBinds: - print('%s = %s' % (name.prettyPrint(), val.prettyPrint())) - - -# Send SNMP GET request -# with SNMPv3 with user 'usr-md5-des', MD5 auth and DES privacy protocols -# over IPv6/UDP -# to an Agent at [::1]:161 -# for three OIDs: one passed as a MibVariable object while others are -# in string form -errorIndication, errorStatus, errorIndex, varBinds = cmdGen.getCmd( - cmdgen.UsmUserData('usr-md5-des', 'authkey1', 'privkey1'), - cmdgen.Udp6TransportTarget(('::1', 161)), - cmdgen.MibVariable('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] or '?' - ) - ) - else: - for name, val in varBinds: - print('%s = %s' % (name.prettyPrint(), val.prettyPrint())) - - -# Send SNMP GET request -# with SNMPv3, user 'usr-md5-none', MD5 authentication, no privacy -# over IPv4/UDP -# to an Agent at localhost:161 -# for IP-MIB::ipAdEntAddr.127.0.0.1 MIB object -errorIndication, errorStatus, errorIndex, varBinds = cmdGen.getCmd( - cmdgen.UsmUserData('usr-md5-none', 'authkey1'), - cmdgen.UdpTransportTarget(('localhost', 161)), - cmdgen.MibVariable('IP-MIB', 'ipAdEntAddr', '127.0.0.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] or '?' - ) - ) - else: - for name, val in varBinds: - print('%s = %s' % (name.prettyPrint(), val.prettyPrint())) - - -# Send SNMP GET request -# with SNMPv3, user 'usr-none-none', no authentication, no privacy -# over IPv4/UDP -# to an Agent at localhost:161 -# for IP-MIB::ipAdEntAddr.127.0.0.1 MIB object -# perform response OIDs and values resolution at MIB - -errorIndication, errorStatus, errorIndex, varBinds = cmdGen.getCmd( - cmdgen.UsmUserData('usr-none-none'), - cmdgen.UdpTransportTarget(('localhost', 161)), - cmdgen.MibVariable('IP-MIB', 'ipAdEntAddr', '127.0.0.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] or '?' - ) - ) - else: - for name, val in varBinds: - print('%s = %s' % (name.prettyPrint(), val.prettyPrint())) - - -# Send SNMP GET request -# with SNMPv3, user 'usr-sha-aes128', SHA auth, AES128 privacy -# over IPv4/UDP -# to an Agent at /tmp/snmp-agent -# for TCP-MIB::tcpConnLocalAddress."0.0.0.0".22."0.0.0.0".0 MIB object -errorIndication, errorStatus, errorIndex, varBinds = cmdGen.getCmd( - cmdgen.UsmUserData('usr-sha-aes128', 'authkey1', 'privkey1', - authProtocol=cmdgen.usmHMACSHAAuthProtocol, - privProtocol=cmdgen.usmAesCfb128Protocol ), - cmdgen.UdpTransportTarget(('localhost', 161)), - cmdgen.MibVariable('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] or '?' - ) - ) - else: - for name, val in varBinds: - print('%s = %s' % (name.prettyPrint(), val.prettyPrint())) - diff --git a/examples/v3arch/oneliner/manager/nextgen.py b/examples/v3arch/oneliner/manager/nextgen.py deleted file mode 100644 index 44cc2d0..0000000 --- a/examples/v3arch/oneliner/manager/nextgen.py +++ /dev/null @@ -1,159 +0,0 @@ -# Various GETNEXT Command Generator uses -from pysnmp.entity.rfc3413.oneliner import cmdgen - -cmdGen = cmdgen.CommandGenerator() - -# Send a series of SNMP GETNEXT requests -# with SNMPv2c, community 'public' -# over IPv4/UDP -# to an Agent at localhost:161 -# for two OIDs in string form -# stop when response OIDs leave the scopes of initial OIDs -errorIndication, errorStatus, errorIndex, varBindTable = cmdGen.nextCmd( - cmdgen.CommunityData('public'), - cmdgen.UdpTransportTarget(('localhost', 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] or '?' - ) - ) - else: - for varBindTableRow in varBindTable: - for name, val in varBindTableRow: - print('%s = %s' % (name.prettyPrint(), val.prettyPrint())) - - -# Send a series of SNMP GETNEXT requests -# with SNMPv1, community 'public' -# over IPv4/UDP -# to an Agent at localhost:161 -# 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 -errorIndication, errorStatus, errorIndex, varBindTable = cmdGen.nextCmd( - cmdgen.CommunityData('public', mpModel=0), - cmdgen.UdpTransportTarget(('localhost', 161)), - cmdgen.MibVariable('IF-MIB', 'ifDescr'), - cmdgen.MibVariable('IF-MIB', 'ifType'), - cmdgen.MibVariable('IF-MIB', 'ifMtu'), - cmdgen.MibVariable('IF-MIB', 'ifSpeed'), - cmdgen.MibVariable('IF-MIB', 'ifPhysAddress') -) - -if errorIndication: - print(errorIndication) -else: - if errorStatus: - print('%s at %s' % ( - errorStatus.prettyPrint(), - errorIndex and varBindTable[-1][int(errorIndex)-1] or '?' - ) - ) - else: - for varBindTableRow in varBindTable: - for name, val in varBindTableRow: - print('%s = %s' % (name.prettyPrint(), val.prettyPrint())) - - -# Send a series of SNMP GETNEXT requests -# with SNMPv3 with user 'usr-md5-des', MD5 auth and DES privacy protocols -# over IPv6/UDP -# to an Agent at [::1]:161 -# for all columns of the IF-MIB::ifEntry table -# stop when response OIDs leave the scopes of the table -# perform response OIDs and values resolution at MIB -# make sure IF-MIB.py is in search path -errorIndication, errorStatus, errorIndex, varBindTable = cmdGen.nextCmd( - cmdgen.UsmUserData('usr-md5-des', 'authkey1', 'privkey1'), - cmdgen.Udp6TransportTarget(('::1', 161)), - cmdgen.MibVariable('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] or '?' - ) - ) - else: - for varBindTableRow in varBindTable: - for name, val in varBindTableRow: - print('%s = %s' % (name.prettyPrint(), val.prettyPrint())) - - -# Send a series of SNMP GETNEXT requests -# with SNMPv3, user 'usr-md5-none', MD5 authentication, no privacy -# over IPv4/UDP -# to an Agent at localhost:161 -# for all OIDs in IF-MIB -# 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 - -errorIndication, errorStatus, errorIndex, varBindTable = cmdGen.nextCmd( - cmdgen.UsmUserData('usr-md5-none', 'authkey1'), - cmdgen.UdpTransportTarget(('localhost', 161)), - cmdgen.MibVariable('IF-MIB', ''), - lookupValues=True -) - -if errorIndication: - print(errorIndication) -else: - if errorStatus: - print('%s at %s' % ( - errorStatus.prettyPrint(), - errorIndex and varBindTable[-1][int(errorIndex)-1] or '?' - ) - ) - else: - for varBindTableRow in varBindTable: - for name, val in varBindTableRow: - print('%s = %s' % (name.prettyPrint(), val.prettyPrint())) - - -# Send a series of SNMP GETNEXT requests -# with SNMPv3, user 'usr-sha-aes128', SHA auth, AES128 privacy -# over Local Domain Sockets -# to an Agent at localhost:161 -# for all OIDs past IF-MIB -# run till end-of-mib condition is reported by Agent OR maxRows == 100 -# ignoring non-increasing OIDs whenever reported by Agent -# make sure IF-MIB.py is search path - -errorIndication, errorStatus, errorIndex, varBindTable = cmdGen.nextCmd( - cmdgen.UsmUserData('usr-sha-aes128', 'authkey1', 'privkey1', - authProtocol=cmdgen.usmHMACSHAAuthProtocol, - privProtocol=cmdgen.usmAesCfb128Protocol), - cmdgen.UdpTransportTarget(('localhost', 161)), - cmdgen.MibVariable('IF-MIB', ''), - 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] or '?' - ) - ) - else: - for varBindTableRow in varBindTable: - for name, val in varBindTableRow: - print('%s = %s' % (name.prettyPrint(), val.prettyPrint())) - diff --git a/examples/v3arch/oneliner/manager/setgen.py b/examples/v3arch/oneliner/manager/setgen.py deleted file mode 100644 index c7fba2f..0000000 --- a/examples/v3arch/oneliner/manager/setgen.py +++ /dev/null @@ -1,148 +0,0 @@ -# Various SET Command Generator uses -from pysnmp.entity.rfc3413.oneliner import cmdgen -from pysnmp.proto import rfc1902 - -cmdGen = cmdgen.CommandGenerator() - -# Send SNMP SET request -# with SNMPv2c, community 'public' -# over IPv4/UDP -# to an Agent at localhost:161 -# setting SNMPv2-MIB::sysName.0 to new value (type taken from MIB) -errorIndication, errorStatus, errorIndex, varBinds = cmdGen.setCmd( - cmdgen.CommunityData('public'), - cmdgen.UdpTransportTarget(('localhost', 161)), - (cmdgen.MibVariable('SNMPv2-MIB', 'sysName', 0), '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] or '?' - ) - ) - else: - for name, val in varBinds: - print('%s = %s' % (name.prettyPrint(), val.prettyPrint())) - - -# Send SNMP SET request -# with SNMPv1, community 'public' -# over IPv4/UDP -# to an Agent at localhost:161 -# setting two OIDs to new values (types explicitly specified) -errorIndication, errorStatus, errorIndex, varBinds = cmdGen.setCmd( - cmdgen.CommunityData('public'), - cmdgen.UdpTransportTarget(('localhost', 161)), - ('1.3.6.1.2.1.1.2.0', rfc1902.ObjectName('1.3.6.1.4.1.20408.1.1')), - ('1.3.6.1.2.1.1.2.0', '1.3.6.1.4.1.20408.1.1'), - ('1.3.6.1.2.1.1.5.0', 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] or '?' - ) - ) - else: - for name, val in varBinds: - print('%s = %s' % (name.prettyPrint(), val.prettyPrint())) - - -# Send SNMP SET request -# with SNMPv3 with user 'usr-md5-des', MD5 auth and DES privacy protocols -# over IPv4/UDP -# to an Agent at localhost:161 -# setting SNMPv2-MIB::sysName.0 to new value (type taken from MIB) -# perform response OIDs and values resolution at MIB -errorIndication, errorStatus, errorIndex, varBinds = cmdGen.setCmd( - cmdgen.UsmUserData('usr-md5-des', 'authkey1', 'privkey1'), - cmdgen.UdpTransportTarget(('localhost', 161)), - (cmdgen.MibVariable('SNMPv2-MIB', 'sysName', 0), 'new system name'), - 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] or '?' - ) - ) - else: - for name, val in varBinds: - print('%s = %s' % (name.prettyPrint(), val.prettyPrint())) - - -# Send SNMP SET request -# with SNMPv3 with user 'usr-none-none', no auth and no privacy protocols -# over IPv4/UDP -# to an Agent at localhost:161 -# addressing particular set of Managed Objects at remote SNMP Engine by: -# * contextEngineId 0x8000000001020304 and -# * contextName 'my-context' -# setting SNMPv2-MIB::sysName.0 to new value (type taken from MIB) -errorIndication, errorStatus, errorIndex, varBinds = cmdGen.setCmd( - cmdgen.UsmUserData('usr-none-none'), - cmdgen.UdpTransportTarget(('localhost', 161)), - (cmdgen.MibVariable('SNMPv2-MIB', 'sysName', 0), 'new system name'), - contextEngineId=rfc1902.OctetString(hexValue='8000000001020304'), - contextName='my-context' -) - -# 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] or '?' - ) - ) - else: - for name, val in varBinds: - print('%s = %s' % (name.prettyPrint(), val.prettyPrint())) - - -# Send SNMP SET request -# with SNMPv3 with user 'usr-md5-des', MD5 auth and DES privacy protocols -# use remote SNMP Engine ID 0x8000000004030201 (USM autodiscovery will run) -# over IPv4/UDP -# to an Agent at localhost:161 -# setting SNMPv2-MIB::sysName.0 to new value (type taken from MIB) -errorIndication, errorStatus, errorIndex, varBinds = cmdGen.setCmd( - cmdgen.UsmUserData( - 'usr-md5-des', 'authkey1', 'privkey1', - contextEngineId=rfc1902.OctetString(hexValue='8000000004030201') - ), - cmdgen.UdpTransportTarget(('localhost', 161)), - (cmdgen.MibVariable('SNMPv2-MIB', 'sysName', 0), '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] or '?' - ) - ) - else: - for name, val in varBinds: - print('%s = %s' % (name.prettyPrint(), val.prettyPrint())) - |