def build_varlist(oids): """ Prepare the variable binding list which will be used by the C interface. :param oids: an individual or list of strings or tuples representing one or more OIDs :return: a tuple containing where the first item is a list of SNMPVariable objects or an individual SNMPVariable and a boolean indicating whether or not the first tuple item is a list or single item """ if isinstance(oids, list): is_list = True else: is_list = False oids = [oids] varlist = SNMPVariableList() for oid in oids: # OIDs specified as a tuple (e.g. ('sysContact', 0)) if isinstance(oid, tuple): oid, oid_index = oid varlist.append(SNMPVariable(oid, oid_index)) # OID . is specified (which we convert to iso) elif oid == '.': varlist.append(SNMPVariable('iso')) # OIDs specified as a string (e.g. 'sysContact.0') else: varlist.append(SNMPVariable(oid)) return varlist, is_list
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 """ varlist = SNMPVariableList() for oid_value in oid_values: if len(oid_value) == 2: oid, value = oid_value snmp_type = None # TODO: Determine the best way to test this else: oid, value, snmp_type = oid_value # OIDs specified as a tuple (e.g. ('sysContact', 0)) if isinstance(oid, tuple): oid, oid_index = oid varlist.append(SNMPVariable(oid, oid_index, value, snmp_type)) # OIDs specefied as a string (e.g. 'sysContact.0') else: varlist.append( SNMPVariable(oid, value=value, snmp_type=snmp_type)) # Perform the set operation and return whether or not it worked success = interface.set(self, varlist) 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 """ varlist = SNMPVariableList() # OIDs specified as a tuple (e.g. ('sysContact', 0)) if isinstance(oid, tuple): oid, oid_index = oid varlist.append(SNMPVariable(oid, oid_index, value, snmp_type)) # OIDs specefied as a string (e.g. 'sysContact.0') else: varlist.append(SNMPVariable(oid, value=value, snmp_type=snmp_type)) # Perform the set operation and return whether or not it worked success = interface.set(self, varlist) return bool(success)
def test_snmp_variable_repr_binary(): var = SNMPVariable( "sysDescr", "0", ub(chr(20)) + "my thingo" + ub(chr(155)), "OCTETSTR" ) assert var.__repr__() == ( "<SNMPVariable value='my thingo (contains binary)' " "(oid='sysDescr', oid_index='0', snmp_type='OCTETSTR')>" )
def test_snmp_variable_repr_binary_only(): var = SNMPVariable( 'sysDescr', '0', ub(chr(20)) + ub(chr(155)), 'OCTETSTR' ) assert var.__repr__() == ( "<SNMPVariable value='(contains binary)' " "(oid='sysDescr', oid_index='0', snmp_type='OCTETSTR')>" )
def test_snmp_variable_value(): var = SNMPVariable('sysDescr', '0', 'my thingo') assert var.value == 'my thingo' assert var.oid == 'sysDescr' assert var.oid_index == '0'
def test_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_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_snmp_variable_regular(): var = SNMPVariable('sysDescr', '0') assert var.oid == 'sysDescr' assert var.oid_index == '0'
def test_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_snmp_variable_repr_none(): var = SNMPVariable() assert var.__repr__() == ( "<SNMPVariable value=None (oid=None, oid_index=None, snmp_type=None)>")
def test_snmp_variable_repr_binary_only(): var = SNMPVariable('sysDescr', '0', ub(chr(20)) + ub(chr(155)), 'OCTETSTR') assert var.__repr__() == ( "<SNMPVariable value='(contains binary)' " "(oid='sysDescr', oid_index='0', snmp_type='OCTETSTR')>")
def test_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_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_snmp_variable_regular(): var = SNMPVariable("sysDescr", "0") assert var.oid == "sysDescr" assert var.oid_index == "0"
def test_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_snmp_variable_value(): var = SNMPVariable("sysDescr", "0", "my thingo") assert var.value == "my thingo" assert var.oid == "sysDescr" assert var.oid_index == "0"
def test_snmp_variable_repr_none(): var = SNMPVariable() assert var.__repr__() == ( "<SNMPVariable value=None (oid=None, oid_index=None, snmp_type=None)>" )
def test_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_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')>" )