def test_variables_003_snmp_variable_repr_binary(): var = SNMPVariable( 'sysDescr', '0', iso_8859_1(chr(20)) + 'my thingo' + iso_8859_1(chr(155)), 'OCTETSTR') assert var.__repr__() == ( "<SNMPVariable value='my thingo (contains binary)' " "(oid='sysDescr', oid_index='0', snmp_type='OCTETSTR')>")
def set_multiple(self, oid_values): """ Perform an SNMP SET operation on multiple OIDs with multiple values using the prepared session. :param oid_values: a list of tuples whereby each tuple contains a (oid, value) or an (oid, value, snmp_type) :return: a list of SNMPVariable objects containing the values that were retrieved via SNMP """ vars_list = SNMPVariableList() for oid_value in oid_values: if len(oid_value) == 2: oid, value = oid_value snmp_type = None else: oid, value, snmp_type = oid_value # OIDs specified as a tuple (e.g. ('sysContact', 0)) if isinstance(oid, tuple): oid, oid_index = oid vars_list.append( SNMPVariable(oid=oid, oid_index=oid_index, value=value, snmp_type=snmp_type)) # OIDs specefied as a string (e.g. 'sysContact.0') else: vars_list.append( SNMPVariable(oid=oid, value=value, snmp_type=snmp_type)) # Perform the set operation and return whether or not it worked success = self.get_interface().set(self, vars_list) return bool(success)
def set(self, oid, value, snmp_type=None): """ Perform an SNMP SET operation using the prepared session. :param oid: the OID that you wish to set which may be a string representing the entire OID (e.g. 'sysDescr.0') or may be a tuple containing the name as its first item and index as its second (e.g. ('sysDescr', 0)) :param value: the value to set the OID to :param snmp_type: if a numeric OID is used and the object is not in the parsed MIB, a type must be explicitly supplied :return: a boolean indicating the success of the operation """ vars_list = SNMPVariableList() # OIDs specified as a tuple (e.g. ('sysContact', 0)) if isinstance(oid, tuple): oid, oid_index = oid vars_list.append( SNMPVariable(oid=oid, oid_index=oid_index, value=value, snmp_type=snmp_type)) # OIDs specefied as a string (e.g. 'sysContact.0') else: vars_list.append( SNMPVariable(oid=oid, value=value, snmp_type=snmp_type)) # Perform the set operation and return whether or not it worked success = self.get_interface().set(self, vars_list) return bool(success)
def build_interface_vars(oids): """ Prepare variable binding list to be used for the C interface. Args: oids: A list of OIDS in string or tuple form Returns: list: A list of SNMPVariable objects. """ ret = SNMPVariableList() for oid in oids: if isinstance(oid, tuple): oid, oid_index = oid ret.append(SNMPVariable(oid=oid, oid_index=oid_index)) elif oid == '.': ret.append(SNMPVariable('iso')) else: ret.append(SNMPVariable(oid=oid)) return ret
def test_variables_009_snmp_variable_numeric(): var = SNMPVariable('.1.3.6.1.2.1.1.1.0') assert var.oid == '.1.3.6.1.2.1.1.1.0' assert var.oid_index == '' assert var.value is None assert var.snmp_type is None
def test_variables_008_snmp_variable_doesnt_extract_oid_index(): var = SNMPVariable('.iso.org.dod.internet.mgmt.mib-2.system.sysDescr.0') assert var.oid == '.iso.org.dod.internet.mgmt.mib-2.system.sysDescr' assert var.oid_index == '0' assert var.value is None assert var.snmp_type is None
def test_variables_007_snmp_variable_long(): var = SNMPVariable('.iso.org.dod.internet.mgmt.mib-2.system.sysDescr', '0') assert var.oid == '.iso.org.dod.internet.mgmt.mib-2.system.sysDescr' assert var.oid_index == '0' assert var.value is None assert var.snmp_type is None
def test_variables_006_snmp_variable_extract_oid_index(): var = SNMPVariable('sysDescr.0') assert var.oid == 'sysDescr' assert var.oid_index == '0' assert var.value is None assert var.snmp_type is None
def test_variables_005_snmp_variable_repr_none(): var = SNMPVariable() assert var.__repr__() == ( "<SNMPVariable value=None (oid=None, oid_index=None, snmp_type=None)>")
def test_variables_000_snmp_variable_regular(): var = SNMPVariable('sysDescr', '0') assert var.oid == 'sysDescr' assert var.oid_index == '0'
def test_variables_002_snmp_variable_repr(): var = SNMPVariable('sysDescr', '0', 'my thingo', 'OCTETSTR') assert var.__repr__() == ( "<SNMPVariable value='my thingo' " "(oid='sysDescr', oid_index='0', snmp_type='OCTETSTR')>")
def test_variables_001_snmp_variable_value(): var = SNMPVariable('sysDescr', '0', 'my thingo') assert var.value == 'my thingo' assert var.oid == 'sysDescr' assert var.oid_index == '0'