Пример #1
0
    def walk(self, query="1.3.6.1.2.1.1.1.0"):
        """Performs an SNMP walk operation.

        :param query: root OID for walk
        :returns: A list of (response_oid, value) pairs.

        """
        result = []
        root_oid = OID(query)
        current_oid = root_oid

        while 1:
            response = self.handle.sgetnext(current_oid)
            if response is None:
                break
            response_oid, value = response.items()[0]
            if value is None:
                break

            current_oid = OID(response_oid)
            if (not root_oid.is_a_prefix_of(current_oid)
                    or current_oid == root_oid):
                break

            if isinstance(value, tuple):
                value = str(OID(value))[1:]
            result.append((str(current_oid)[1:], value))

        return result
Пример #2
0
    def bulkwalk(self, query="1.3.6.1.2.1.1.1.0", strip_prefix=False):
        """Performs an SNMP walk on the host, using GETBULK requests.

        Will raise an UnsupportedSnmpVersionError if the current
        version is anything other than 2c.

        :param query: root OID for walk
        :param strip_prefix: A boolean. Set to True to strip the query prefix
                             from the OIDs in the response. Default is False.
        :returns: A list of (response_oid, value) pairs,
                  where the query prefix has been stripped from the
                  response_oids.

        """
        if str(self.version) != "2c":
            raise errors.UnsupportedSnmpVersionError(
                "Cannot use BULKGET in SNMP version " + self.version)
        result = []
        root_oid = OID(query)
        current_oid = root_oid

        while 1:
            response = self.handle.sgetbulk(self.NON_REPEATERS,
                                            self.MAX_REPETITIONS,
                                            [current_oid])
            if response is None:
                break
            for response_oid, value in response:
                if value is None:
                    return result

                current_oid = OID(response_oid)
                if (not root_oid.is_a_prefix_of(current_oid)
                        or current_oid == root_oid):
                    return result

                if isinstance(value, tuple):
                    value = str(OID(value))[1:]
                result.append((str(current_oid)[1:], value))

        return result