The cloudshell-snmp open source Python package provides an easy-to-use interface for SNMP access and communication to a networking devices.
Note: We use tox and pre-commit for testing. For details, see Services description.
pip install cloudshell-snmpWe welcome community ideas and contributions, so if you have any feedback or would like to request enhancements, feel free to create an Issue in the repository.
-
Fork the repository.
-
Make the code change. Make sure the tests pass. Add unit tests to cover any new behavior. We do require that the coverage does not decrease with the new code.
-
Submit a Pull Request.
CloudShell SNMP offers the following key features:
SnmpMibOidandSnmpRawOid- entities used to build a requestSnmpResponseandQualiMibTable- types of objects we recieve in response from all communication methods (get, walk, get_table, etc.)- snmp service allows CloudShell SNMP to communicate with target device.
CloudShell SNMP uses thewithstatement to establish a connection to the device.
CloudShell SNMP is highly modular and implements many programming interfaces.
snmp service is the service that manages communication with the device. Allowing you to set, get, walk and get_table to/from the device.
Additionally it allows you to add more snmp MIB files by running update_mib_sources method, or load mib information using load_mib_tables.
Most communication methods requires you to pass either SnmpMibOid or SnmpRawOid,
i.e: SnmpMibOid('SNMPv2-MIB', 'sysContact', 0) or SnmpRawOid('1.3.6.1.2.1.1.4.0')
And in the result most of the commands return single or list of SnmpResponse, except get_table, since it returns table like dictionary: QualiMibTable.
Example - Executing 'update_mib_sources' and 'set' command
from cloudshell.logging.qs_logger import get_qs_logger
from cloudshell.snmp.cloudshell_snmp import Snmp
from cloudshell.snmp.core.domain.snmp_oid import SnmpMibObject, SnmpSetMibName
from cloudshell.snmp.snmp_parameters import SNMPWriteParameters
snmp_params = SNMPWriteParameters(ip, community, "v2")
logger = get_qs_logger()
logger.info("started")
snmp_handler = Snmp()
with snmp_handler.get_snmp_service(snmp_parameters=snmp_params, logger=logger) as snmp_service:
snmp_service.add_mib_folder_path("D:\\cisco\\mibs")
set_id = 1 # Represents a row id, should be incremented.
response = snmp_service.set([SnmpSetMibName("CISCO-CONFIG-COPY-MIB", "ccCopyProtocol", set_id, 1),
SnmpSetMibName("CISCO-CONFIG-COPY-MIB", "ccCopySourceFileType", set_id, 3),
SnmpSetMibName("CISCO-CONFIG-COPY-MIB", "ccCopyDestFileType", set_id, 1),
SnmpSetMibName("CISCO-CONFIG-COPY-MIB", "ccCopyServerAddress", set_id, "192.168.105.3"),
SnmpSetMibName("CISCO-CONFIG-COPY-MIB", "ccCopyFileName", set_id, "test_snmp_running_config_save"),
SnmpSetMibName("CISCO-CONFIG-COPY-MIB", "ccCopyEntryRowStatus", set_id, 4)])Example - Executing 'get', 'get_next', 'get_list' and 'get_property' command
from cloudshell.logging.qs_logger import get_qs_logger
from cloudshell.snmp.cloudshell_snmp import Snmp
from cloudshell.snmp.core.domain.snmp_oid import SnmpMibObject, SnmpSetMibName
from cloudshell.snmp.snmp_parameters import SNMPWriteParameters
snmp_params = SNMPWriteParameters(ip, community, "v2")
logger = get_qs_logger()
logger.info("started")
snmp_handler = Snmp()
with snmp_handler.get_snmp_service(snmp_parameters=snmp_params, logger=logger) as snmp_service:
response = snmp_service.get_property(SnmpMibObject("SNMPv2-MIB", "sysDescr", 0)) # Retruns empty SnmpResponse in case get command failed to retrieve data
response = snmp_service.get(SnmpMibObject("SNMPv2-MIB", "sysDescr", 0)) # Raises an exception in case requested oid is not found
response = snmp_service.get_next(SnmpMibObject("SNMPv2-MIB", "sysDescr", 0)) # same as get
response = snmp_service.get_list(SnmpMibObject("SNMPv2-MIB", "sysDescr", 0)) # same as get
Example - Executing 'walk' and 'get_table' command
from cloudshell.logging.qs_logger import get_qs_logger
from cloudshell.snmp.cloudshell_snmp import Snmp
from cloudshell.snmp.core.domain.snmp_oid import SnmpMibObject, SnmpSetMibName
from cloudshell.snmp.snmp_parameters import SNMPWriteParameters
snmp_params = SNMPWriteParameters(ip, community, "v2")
logger = get_qs_logger()
logger.info("started")
snmp_handler = Snmp()
with snmp_handler.get_snmp_service(snmp_parameters=snmp_params, logger=logger) as snmp_service:
response = snmp_service.walk(SnmpMibObject('IF-MIB', 'ifTable')) # Retruns empty SnmpResponse in case get command failed to retrieve data
response = snmp_service.get_table(SnmpMibObject('IF-MIB', 'ifTable')) # Raises an exception in case requested oid is not found