Exemple #1
0
    def snmpwalk(self, mib):
        # Walks specified mib
        ips = self.ips
        # Get a list of communities, starting with any that are known to
        # work on this host.
        communities = self.network.communities.copy()
        if self.community:
            # Means we have a functional community string. Use that first.
            communities.append(self.community)
        communities.reverse()

        def scanAllCommunities(ip):
            for community in communities:
                results = scan(ip, community)
                if results:
                    return results
            return False

        def scan(ip, community):
            session = self.snmpInit(ip, community)
            try:
                responses = session.walk(mib)
                self.community = community
                self.setmgmntip(ip, True)
                print('Response on', ip, 'with', community)
                return responses
            except easysnmp.exceptions.EasySNMPNoSuchNameError:
                # Probably means that you're hitting the wrong kind of device.
                self.community = None
                self.setmgmntip(ip, False)
                raise
            except easysnmp.exceptions.EasySNMPTimeoutError:
                # Either the community string is wrong, or the address is dead.
                print('No response on', ip, 'with', community)
                self.community = None
                self.setmgmntip(ip, False)
                pass
            return False

        # First, we try using known-good settings for communicating with this
        # host.
        if self.mgmntip:
            if self.community:
                results = scan(self.mgmntip, self.community)
                if results:
                    return results
            results = scanAllCommunities(self.mgmntip)
            if results:
                return results
        # If we have no known-good settings, we just iterate over everything.
        for ip in ips:
            if not Toolbox.ipInNetworks(ip, self.network.inaccessiblenets):
                results = scanAllCommunities(ip)
                if results:
                    return results
        return False