Session Python Module¶
- class ezsnmp.session.Session(hostname: str = 'localhost', port_number: str | int = '', version: str | int = '3', community: str = 'public', auth_protocol: str = '', auth_passphrase: str = '', security_engine_id: str = '', context_engine_id: str = '', security_level: str = '', context: str = '', security_username: str = '', privacy_protocol: str = '', privacy_passphrase: str = '', boots_time: str = '', retries: str | int = '3', timeout: str | int = '1', load_mibs: str = '', mib_directories: str = '', print_enums_numerically: bool = False, print_full_oids: bool = False, print_oids_numerically: bool = False, print_timeticks_numerically: bool = False)¶
Bases:
SessionBase
Python wrapper class for SessionBase, providing a Pythonic interface for managing NetSNMP sessions.
- __init__(hostname: str = 'localhost', port_number: str | int = '', version: str | int = '3', community: str = 'public', auth_protocol: str = '', auth_passphrase: str = '', security_engine_id: str = '', context_engine_id: str = '', security_level: str = '', context: str = '', security_username: str = '', privacy_protocol: str = '', privacy_passphrase: str = '', boots_time: str = '', retries: str | int = '3', timeout: str | int = '1', load_mibs: str = '', mib_directories: str = '', print_enums_numerically: bool = False, print_full_oids: bool = False, print_oids_numerically: bool = False, print_timeticks_numerically: bool = False)¶
Initialize the SessionBase object with NetSNMP session parameters.
- Parameters:
hostname (str) – The hostname or IP address of the SNMP agent.
port_number (Union[str, int]) – The port number of the SNMP agent.
version (Union[str, int]) – The SNMP version to use (1, 2c, or 3).
community (str) – The community string for SNMPv1/v2c.
auth_protocol (str) – The authentication protocol (e.g., “MD5”, “SHA”).
auth_passphrase (str) – The authentication passphrase.
security_engine_id (str) – The security engine ID.
context_engine_id (str) – The context engine ID.
security_level (str) – The security level (e.g., “noAuthNoPriv”, “authNoPriv”, “authPriv”).
context (str) – The context.
security_username (str) – The security username.
privacy_protocol (str) – The privacy protocol (e.g., “DES”, “AES”).
privacy_passphrase (str) – The privacy passphrase.
boots_time (str) – The boots time.
retries (Union[str, int]) – The number of retries.
timeout (Union[str, int]) – The timeout value in seconds.
load_mibs (str) – Comma-separated string of MIB modules to load.
mib_directories (str) – Comma-separated string of directories to search for MIB files.
print_enums_numerically (bool) – Whether to print enums numerically.
print_full_oids (bool) – Whether to print full OIDs.
print_oids_numerically (bool) – Whether to print OIDs numerically.
:param Whether to print timeticks numerically. :type print_timeticks_numerically: bool
- property args¶
Get the tuple of arguments used for NetSNMP commands.
- Type:
tuple
- property auth_passphrase¶
Get the authentication passphrase.
- Type:
str
- property auth_protocol¶
Get the authentication protocol.
- Type:
str
- property boots_time¶
Get the boots time.
- Type:
str
- bulk_get(oids=[])¶
Performs an SNMP BULK GET operation to retrieve values for multiple OIDs.
- Parameters:
oids (list) – List of Object Identifiers (OIDs) to retrieve values from
- Returns:
A tuple of Result objects containing SNMP variable bindings with attributes: oid (str), index (str), value (str), and type (str)
- Return type:
tuple[Result]
- Raises:
GenericError – If the exception type is GenericErrorBase.
ConnectionError – If the exception type is ConnectionErrorBase.
NoSuchInstanceError – If the exception type is NoSuchInstanceErrorBase.
NoSuchNameError – If the exception type is NoSuchNameErrorBase.
NoSuchObjectError – If the exception type is NoSuchObjectErrorBase.
PacketError – If the exception type is PacketErrorBase.
ParseError – If the exception type is ParseErrorBase.
TimeoutError – If the exception type is TimeoutErrorBase.
UndeterminedTypeError – If the exception type is UndeterminedTypeErrorBase.
UnknownObjectIDError – If the exception type is UnknownObjectIDErrorBase.
Exception – If the exception type does not match any of the above, the original exception e is raised.
- Example:
>>> from ezsnmp import Session >>> session = Session(hostname="localhost", community="public", version="2") >>> results = session.bulk_get(["1.3.6.1.2.1.1.1.0", "1.3.6.1.2.1.1.2.0"]) >>> for item in results: ... print("OID:", item.oid) ... print("Index:", item.index) ... print("Value:", item.value) ... print("Type:", item.type) ... print("---")
- bulk_walk(oids=[])¶
Performs a bulk SNMP walk operation to retrieve a collection of values from multiple OIDs. The bulk walk operation is designed to return multiple OIDs in a single request, making it more efficient than regular walk operations for retrieving large amounts of data.
- Parameters:
oids (list[str]) – List of base OIDs to start the walks from
- Returns:
A tuple of Result objects containing SNMP variable bindings. Each Result object has attributes: oid (str), index (str), value (str), and type (str)
- Return type:
tuple[Result]
- Raises:
ConnectionError – If the exception type is ConnectionErrorBase.
GenericError – If the exception type is GenericErrorBase.
NoSuchInstanceError – If the exception type is NoSuchInstanceErrorBase.
NoSuchNameError – If the exception type is NoSuchNameErrorBase.
NoSuchObjectError – If the exception type is NoSuchObjectErrorBase.
PacketError – If the exception type is PacketErrorBase.
ParseError – If the exception type is ParseErrorBase.
TimeoutError – If the exception type is TimeoutErrorBase.
UndeterminedTypeError – If the exception type is UndeterminedTypeErrorBase.
UnknownObjectIDError – If the exception type is UnknownObjectIDErrorBase.
Exception – If the exception type does not match any of the above, the original exception e is raised.
- Example:
>>> from ezsnmp import Session >>> session = Session(hostname="localhost", community="public", version="2") >>> results = session.bulk_walk(["1.3.6.1.2.1.1", "1.3.6.1.2.1.2"]) >>> for item in results: ... print("OID:", item.oid) ... print("Index:", item.index) ... print("Value:", item.value) ... print("Type:", item.type) ... print("---")
- property community¶
Get the community string for SNMPv1/v2c.
- Type:
str
- property context¶
Get the context.
- Type:
str
- property context_engine_id¶
Get the context engine ID.
- Type:
str
- get(oids=[])¶
Performs an SNMP GET operation to retrieve values for multiple OIDs.
- Parameters:
oids (list) – List of Object Identifiers (OIDs) to retrieve values from
- Returns:
A tuple of Result objects containing SNMP variable bindings with attributes: oid (str), index (str), value (str), and type (str)
- Return type:
tuple[Result]
- Raises:
GenericError – If the exception type is GenericErrorBase.
ConnectionError – If the exception type is ConnectionErrorBase.
NoSuchInstanceError – If the exception type is NoSuchInstanceErrorBase.
NoSuchNameError – If the exception type is NoSuchNameErrorBase.
NoSuchObjectError – If the exception type is NoSuchObjectErrorBase.
PacketError – If the exception type is PacketErrorBase.
ParseError – If the exception type is ParseErrorBase.
TimeoutError – If the exception type is TimeoutErrorBase.
UndeterminedTypeError – If the exception type is UndeterminedTypeErrorBase.
UnknownObjectIDError – If the exception type is UnknownObjectIDErrorBase.
Exception – If the exception type does not match any of the above, the original exception e is raised.
- Example:
>>> from ezsnmp import Session >>> session = Session(hostname="localhost", community="public", version="2") >>> results = session.get(["1.3.6.1.2.1.1.1.0", "1.3.6.1.2.1.1.2.0"]) >>> for item in results: ... print("OID:", item.oid) ... print("Index:", item.index) ... print("Value:", item.value) ... print("Type:", item.type) ... print("---")
- get_next(oids=[])¶
Performs an SNMP GET-NEXT operation to retrieve values for the next objects after the specified OIDs.
- Parameters:
oids (list) – List of Object Identifiers (OIDs) to get next values from
- Returns:
A tuple of Result objects containing SNMP variable bindings with attributes: oid (str), index (str), value (str), and type (str)
- Return type:
tuple[Result]
- Raises:
GenericError – If the exception type is GenericErrorBase.
ConnectionError – If the exception type is ConnectionErrorBase.
NoSuchInstanceError – If the exception type is NoSuchInstanceErrorBase.
NoSuchNameError – If the exception type is NoSuchNameErrorBase.
NoSuchObjectError – If the exception type is NoSuchObjectErrorBase.
PacketError – If the exception type is PacketErrorBase.
ParseError – If the exception type is ParseErrorBase.
TimeoutError – If the exception type is TimeoutErrorBase.
UndeterminedTypeError – If the exception type is UndeterminedTypeErrorBase.
UnknownObjectIDError – If the exception type is UnknownObjectIDErrorBase.
Exception – If the exception type does not match any of the above, the original exception e is raised.
- Example:
>>> from ezsnmp import Session >>> session = Session(hostname="localhost", community="public", version="2") >>> results = session.get_next(["1.3.6.1.2.1.1.1.0"]) >>> for item in results: ... print("OID:", item.oid) ... print("Index:", item.index) ... print("Value:", item.value) ... print("Type:", item.type) ... print("---")
- property hostname¶
Get the hostname or IP address of the SNMP agent.
- Type:
str
- property port_number¶
Get the port number of the SNMP agent.
- Type:
str
- property privacy_passphrase¶
Get the privacy passphrase.
- Type:
str
- property privacy_protocol¶
Get the privacy protocol.
- Type:
str
- property retries¶
Get the number of retries.
- Type:
int
- property security_engine_id¶
Get the security engine ID.
- Type:
str
- property security_level¶
Get the security level.
- Type:
str
- property security_username¶
Get the security username.
- Type:
str
- set(oids=[])¶
Performs an SNMP SET operation to set values for multiple OIDs.
- Parameters:
oids (list) – List containing groups of OID, type and value. Each group should be a sequence of [oid, type, value] where type is a string indicating the SNMP data type (e.g. ‘i’ for INTEGER, ‘s’ for STRING, ‘o’ for OBJECT IDENTIFIER)
- Returns:
A tuple of Result objects containing SNMP variable bindings with attributes: oid (str), index (str), value (str), and type (str)
- Return type:
tuple[Result]
- Raises:
GenericError – If the exception type is GenericErrorBase.
ConnectionError – If the exception type is ConnectionErrorBase.
NoSuchInstanceError – If the exception type is NoSuchInstanceErrorBase.
NoSuchNameError – If the exception type is NoSuchNameErrorBase.
NoSuchObjectError – If the exception type is NoSuchObjectErrorBase.
PacketError – If the exception type is PacketErrorBase.
ParseError – If the exception type is ParseErrorBase.
TimeoutError – If the exception type is TimeoutErrorBase.
UndeterminedTypeError – If the exception type is UndeterminedTypeErrorBase.
UnknownObjectIDError – If the exception type is UnknownObjectIDErrorBase.
Exception – If the exception type does not match any of the above, the original exception e is raised.
- Example:
>>> from ezsnmp import Session >>> session = Session(hostname="localhost", community="public", version="2") >>> results = session.set([ ... ".1.3.6.1.6.3.12.1.2.1.2.116.101.115.116", "o", ".1.3.6.1.6.1.1", ... ".1.3.6.1.6.3.12.1.2.1.3.116.101.115.116", "s", "1234", ... ".1.3.6.1.6.3.12.1.2.1.9.116.101.115.116", "i", "4" ... ]) >>> for item in results: ... print("OID:", item.oid) ... print("Index:", item.index) ... print("Value:", item.value) ... print("Type:", item.type) ... print("---")
- property timeout¶
Get the timeout value.
- Type:
int
- property version¶
Get the SNMP version being used.
- Type:
str
- walk(oid='.')¶
Walks through the SNMP tree starting from the given OID. This method performs an SNMP walk operation, which retrieves a subtree of management values from the SNMP agent, starting from the specified OID.
- Parameters:
oid (str) – The starting OID for the SNMP walk. If not specified, the walk will start from the root.
- Returns:
A tuple of Result objects containing SNMP variable bindings. Each Result object has attributes: oid (str), index (str), value (str), and type (str)
- Return type:
tuple[Result]
- Raises:
ConnectionError – If the exception type is ConnectionErrorBase.
GenericError – If the exception type is GenericErrorBase.
NoSuchInstanceError – If the exception type is NoSuchInstanceErrorBase.
NoSuchNameError – If the exception type is NoSuchNameErrorBase.
NoSuchObjectError – If the exception type is NoSuchObjectErrorBase.
PacketError – If the exception type is PacketErrorBase.
ParseError – If the exception type is ParseErrorBase.
TimeoutError – If the exception type is TimeoutErrorBase.
UndeterminedTypeError – If the exception type is UndeterminedTypeErrorBase.
UnknownObjectIDError – If the exception type is UnknownObjectIDErrorBase.
Exception – If the exception type does not match any of the above, the original exception e is raised.
- Example:
>>> from ezsnmp import Session >>> session = Session(hostname="localhost", community="public", version="2") >>> results = session.walk("1.3.6.1.2.1") >>> for item in results: ... print("OID:", item.oid) ... print("Index:", item.index) ... print("Value:", item.value) ... print("Type:", item.type) ... print("---")