snmp_cmds.commands module¶
This module provides the individual SNMP commands for use in one-off situations, or situations where you would need to make a single SNMP request to many devices. If you plan on making multiple calls to the same device, you might want to check out api.py instead.
- snmp_cmds.commands.snmpget(ipaddress, oid, community='public', port=161, timeout=3)[source]¶
Wrapper around Net-SNMP’s
snmpget
commandRuns 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
community (
str
) – the snmpv2 community stringipaddress (
str
) – the IP address of the target SNMP serveroid (
str
) – the Object IDentifier to request from the target SNMP serverport (
Union
[int
,str
]) – the port on which SNMP is running on the target servertimeout (
Union
[int
,str
]) – the number of seconds to wait for a response from the 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
- snmp_cmds.commands.snmpgetsome(ipaddress, oids, community='public', port=161, timeout=3)[source]¶
Warpper around Net-SNMP’s
Runs Net-SNMP’s ‘snmpget’ command on a list of OIDs, and returns a list of tuples of the form (oid, result).
- Parameters
community (
str
) – the snmpv2 community stringipaddress (
str
) – the IP address of the target SNMP serveroids (
List
[str
]) – a list of Object IDentifiers to request from the target SNMP serverport (
Union
[str
,int
]) – the port on which SNMP is running on the target servertimeout (
Union
[int
,str
]) – the number of seconds to wait for a response from the SNMP server
- Return type
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
- snmp_cmds.commands.snmpset(ipaddress, oid, value_type, value, community='private', port=161, timeout=3)[source]¶
Runs Net-SNMP’s ‘snmpset’ command on a given OID, and returns the result if successful.
- Parameters
community (
str
) – the snmpv2 community stringipaddress (
str
) – the IP address of the target SNMP serveroid (
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 setport (
Union
[int
,str
]) – the port on which SNMP is running on the target servertimeout (
Union
[int
,str
]) – the number of seconds to wait for a response from the SNMP server
- 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.
- snmp_cmds.commands.snmptable(ipaddress, oid, community='public', port=161, timeout=3, sortkey=None)[source]¶
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
community (
str
) – the snmpv2 community stringipaddress (
str
) – the IP address of the target SNMP serveroid (
str
) – the Object IDentifier to request from the target SNMP serverport (
Union
[str
,int
]) – the port on which SNMP is running on the target serversortkey (
Optional
[str
]) – the key within each dict upon which to sort the list of resultstimeout (
int
) – the number of seconds to wait for a response from the SNMP server
- 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
- snmp_cmds.commands.snmpwalk(ipaddress, oid, community='public', port=161, timeout=3)[source]¶
Runs Net-SNMP’s ‘snmpget’ command on a list of OIDs, and returns a list of tuples of the form (oid, result).
- Parameters
community (
str
) – the snmpv2 community stringipaddress (
str
) – the IP address of the target SNMP serveroid (
str
) – the Object IDentifier to request from the target SNMP serverport (
Union
[str
,int
]) – the port on which SNMP is running on the target servertimeout (
int
) – the number of seconds to wait for a response from the SNMP server
- Return type
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