snmp_cmds.api module¶
This module provides a Session class for making multiple SNMP requests to the same device. If you plan on making relatively few SNMP requests to relatively many SNMP targets, you may want to check out commands.py instead.
- class snmp_cmds.api.Session(ipaddress, port=161, read_community='public', write_community='private', timeout=3)[source]¶
Bases:
object
This class represents an open session to a single SNMP target device. Once instantiated, it can be used to make easy and convenient calls to get or set information on that target.
- get(oid)[source]¶
A simple convenience wrapper around
snmpget()
Runs the equivalent of ‘
snmpget -Oqv -Pe -t {timeout} -r 0 -v 2c -c {community} {host} {oid}
’ and parses the result. if the response from the server is aNo Such Object
or aNo Such Instance
error, this function returnsNone
. Otherwise, it returns the value retrieved from the server- Parameters
oid (
str
) – the Object IDentifier to request from the target SNMP server- Return type
Optional
[str
]- Returns
the value stored at that OID on the target SNMP server if successful,
None
otherwise- Raises
SNMPTimeout – if the target SNMP server fails to respond
SNMPInvalidAddress – if the hostname or IP address supplied is not valid or cannot be resolved
SNMPError – if the underlying Net-SNMP command produces an unknown or unhandled error
- get_some(oids)[source]¶
A simple convenience wrapper around
snmpgetsome()
Runs Net-SNMP’s ‘snmpget’ command on a list of OIDs, and returns a list of tuples of the form (oid, result).
- Parameters
oids (
List
[str
]) – a list of Object IDentifiers to request from the target SNMP server- Return type
Optional
[List
[Tuple
[str
,str
]]]- Returns
a list of tuples of the form (oid, result)
- Raises
SNMPTimeout – if the target SNMP server fails to respond
SNMPInvalidAddress – if the hostname or IP address supplied is not valid or cannot be resolved
SNMPError – if the underlying Net-SNMP command produces an unknown or unhandled error
- get_table(oid, sortkey=None)[source]¶
A simple convenience wrapper around
snmptable()
Runs Net-SNMP’s ‘snmptable’ command on a given OID, converts the results into a list of dictionaries, and optionally sorts the list by a given key.
- Parameters
oid (
str
) – the Object IDentifier to request from the target SNMP serversortkey (
Optional
[str
]) – the key within each dict upon which to sort the list of results
- Return type
Union
[List
[Dict
[str
,str
]],Dict
[str
,Dict
[str
,str
]]]- Returns
a list of dicts, one for each row of the table. The keys of the dicts correspond to the column names of the table.
- Raises
SNMPTimeout – if the target SNMP server fails to respond
SNMPInvalidAddress – if the hostname or IP address supplied is not valid or cannot be resolved
SNMPError – if the underlying Net-SNMP command produces an unknown or unhandled error
SNMPTableError – if the requested OID is not a valid table
- set(oid, value_type, value)[source]¶
A simple convenience wrapper around
snmpset()
Runs Net-SNMP’s ‘snmpset’ command on a given OID, and returns the result if successful.
- Parameters
oid (
str
) – the Object IDentifier to request from the target SNMP servervalue_type (
str
) – the SNMP value type to set. can be one of (i/u/t/a/o/s/x/d/b)value (
str
) – the value to set
- Return type
str
- Returns
the value that was set on the SNMP target
- Raises
SNMPTimeout – if the target SNMP server fails to respond
SNMPInvalidAddress – if the hostname or IP address supplied is not valid or cannot be resolved
SNMPError – if the underlying Net-SNMP command produces an unknown or unhandled error
SNMPWriteError – if the snmpset operation failed for a known reason. The message associated with this error should always contain information regarding the reason for the error.
- walk(oid)[source]¶
A simple convenience wrapper around
snmpwalk()
Runs Net-SNMP’s ‘snmpget’ command on a list of OIDs, and returns a list of tuples of the form (oid, result).
- Parameters
oid (
str
) – the Object IDentifier to request from the target SNMP server- Return type
Optional
[List
[Tuple
[str
,str
]]]- Returns
a list of tuples of the form (oid, result)
- Raises
SNMPTimeout – if the target SNMP server fails to respond
SNMPInvalidAddress – if the hostname or IP address supplied is not valid or cannot be resolved
SNMPError – if the underlying Net-SNMP command produces an unknown or unhandled error