示例#1
0
    def test_walk(self):
        response_1 = readbytes('walk_response_1.hex')
        response_2 = readbytes('walk_response_2.hex')
        response_3 = readbytes('walk_response_3.hex')

        num_call = 0

        def mocked_responses(*args, **kwargs):
            nonlocal num_call
            num_call += 1
            if num_call == 1:
                return response_1
            elif num_call == 2:
                return response_2
            elif num_call == 3:
                return response_3
            else:
                raise AssertionError('Expected no more than 3 calls!')

        expected = [
            VarBind(ObjectIdentifier.from_string('1.3.6.1.2.1.2.2.1.5.1'),
                    10000000),
            VarBind(ObjectIdentifier.from_string('1.3.6.1.2.1.2.2.1.5.13'),
                    4294967295)
        ]

        with patch('puresnmp.send') as mck:
            mck.side_effect = mocked_responses
            result = list(walk('::1', 'public', '1.3.6.1.2.1.2.2.1.5'))
        self.assertEqual(result, expected)
示例#2
0
 def getIfNames(ip, community):
     for row in walk(ip, community, IfNames):
         n = "%s, %s" % row
         n = n.split(',')
         name = n[1].replace("b'", '')
         name = name.replace("'", '').strip()
         interfaces.append(name)
示例#3
0
 def getOutOctects(ip, community):
     i = 0
     for row in walk(ip, community, If_OutOctets):
         o = "%s, %s" % row
         o = o.split(',')
         outOctect = o[1].strip()
         outOctects[i] = outOctect
         i = i + 1
示例#4
0
 def getInOctects(ip, community):
     i = 0
     for row in walk(ip, community, If_InOctets):
         o = "%s, %s" % row
         o = o.split(',')
         inOctect = o[1].strip()
         inOctects[i] = inOctect
         i = i + 1
示例#5
0
 def test_walk_multiple_return_binds(self):
     """
     A "WALK" response should only return one varbind.
     """
     data = readbytes('get_sysoid_01_error.hex')
     with patch('puresnmp.send') as mck:
         mck.return_value = data
         with self.assertRaisesRegex(SnmpError, 'varbind'):
             next(walk('::1', 'private', '1.2.3'))
示例#6
0
    def test_walk(self):
        response_1 = readbytes('walk_response_1.hex')
        response_2 = readbytes('walk_response_2.hex')
        response_3 = readbytes('walk_response_3.hex')

        expected = [
            VarBind(ObjectIdentifier.from_string('1.3.6.1.2.1.2.2.1.5.1'),
                    10000000),
            VarBind(ObjectIdentifier.from_string('1.3.6.1.2.1.2.2.1.5.13'),
                    4294967295)
        ]

        with patch('puresnmp.send') as mck:
            mck.side_effect = [response_1, response_2, response_3]
            result = list(walk('::1', 'public', '1.3.6.1.2.1.2.2.1.5'))
        self.assertEqual(result, expected)
示例#7
0
    def getStatuses(ip, community):
        j = 0
        for row in walk(ip, community, IfStatuses):
            t = "%s, %r" % row
            t = t.split(',')

            if "1" in t[1]:
                status = "UP"

            elif "2" in t[1]:
                status = "DOWN"

            elif "3" in t[1]:
                status = "TESTING"

            statuses[j] = status
            j = j + 1
示例#8
0
文件: test_v1.py 项目: b2vn/puresnmp
    def test_walk(self):
        responses = readbytes_multiple('apiv1/walk_response.hex')
        self.transport.send.side_effect = responses
        result = snmp.walk('127.0.0.1', 'private', '1.3.6.1.2.1.2.2.1.1')
        result = list(result)
        expected = [
            VarBind(oid=OID('1.3.6.1.2.1.2.2.1.1.1'), value=1),
            VarBind(oid=OID('1.3.6.1.2.1.2.2.1.1.12'), value=12)
        ]
        six.assertCountEqual(self, result, expected)
        self.assertEqual(self.transport.send.call_count, 3)

        expected_types = [
            (ObjectIdentifier, int),
            (ObjectIdentifier, int),
        ]
        returned_values = [(row.oid, row.value) for row in result]
        assert_of_types(returned_values, expected_types)
示例#9
0
def cisco_sg300(IP, COMMUNITY):
    from puresnmp import walk
    from puresnmp import get
    from puresnmp import table
    import pandas as pd
    oid_name = "1.3.6.1.4.1.9.9.23.1.3.4.0"
    oid_if = "1.3.6.1.4.1.9.9.23.1.1.1.1"
    oid_cdp = "1.3.6.1.4.1.9.9.23.1.2.1"
    oid_if_remote = "1.3.6.1.4.1.9.9.23.1.2.1.1.7"
    oid_ip_remote = "1.3.6.1.4.1.9.9.23.1.2.1.1.4"
    oid_name_remote = "1.3.6.1.4.1.9.9.23.1.2.1.1.6"
    oid_model_remote = "1.3.6.1.4.1.9.9.23.1.2.1.1.8"
    try:
        local_name = get(IP, COMMUNITY, oid_name)
        local_name = local_name.decode()
        interfaces = table(IP, COMMUNITY, oid_if)
        interfaces = interfaces[0]
        remote = []
        local_if = []
        for row in walk(IP, COMMUNITY, oid_name_remote):
            interface = str(row[0])
            interface = interface.split(".")
            port = interfaces[interface[14]]
            remote.append(row[1].decode())
            local_if.append(port.decode())
        df_result = pd.DataFrame({
            'local': local_name,
            'local_if': local_if,
            'remote': remote
        })
        ## interface remota
        result_remote_if = table(IP, COMMUNITY, oid_if_remote)
        result_remote_if = result_remote_if[0]
        del result_remote_if['0']
        remote_if = list(result_remote_if.values())
        remote_if = [i.decode() for i in remote_if]
        df_result['remote_if'] = remote_if
        ### ips remote
        ips = table(IP, COMMUNITY, oid_ip_remote)
        ips = ips[0]
        del ips['0']
        ips = list(ips.values())
        ips = [i.hex() for i in ips]
        ips = [iphex(i) for i in ips]
        df_result['remote_ip'] = ips
        ### remote model
        model_remote = table(IP, COMMUNITY, oid_model_remote)
        model_remote = model_remote[0]
        del model_remote['0']
        model_remote = list(model_remote.values())
        model_remote = [i.decode() for i in model_remote]
        df_result['remote_model'] = model_remote
        return df_result
    except:
        print("error : " + IP)
        column_names = [
            "local", "local_if", "remote", "remote_if", "remote_ip",
            "remote_model"
        ]
        df_result = pd.DataFrame(columns=column_names)
        return df_result
示例#10
0
    def __process_methods(method, common_parameters, datatype_config):
        response = None

        if method == "get":
            oid = datatype_config["oid"]
            response = puresnmp.get(**common_parameters, oid=oid)
        elif method == "multiget":
            oids = datatype_config["oid"]
            oids = oids if isinstance(oids, list) else list(oids)
            response = puresnmp.multiget(**common_parameters, oids=oids)
        elif method == "getnext":
            oid = datatype_config["oid"]
            master_response = puresnmp.getnext(**common_parameters, oid=oid)
            response = {master_response.oid: master_response.value}
        elif method == "multigetnext":
            oids = datatype_config["oid"]
            oids = oids if isinstance(oids, list) else list(oids)
            master_response = puresnmp.multigetnext(**common_parameters,
                                                    oids=oids)
            response = {
                binded_var.oid: binded_var.value
                for binded_var in master_response
            }
        elif method == "walk":
            oid = datatype_config["oid"]
            response = {
                binded_var.oid: binded_var.value
                for binded_var in list(
                    puresnmp.walk(**common_parameters, oid=oid))
            }
        elif method == "multiwalk":
            oids = datatype_config["oid"]
            oids = oids if isinstance(oids, list) else list(oids)
            response = {
                binded_var.oid: binded_var.value
                for binded_var in list(
                    puresnmp.multiwalk(**common_parameters, oids=oids))
            }
        elif method == "set":
            oid = datatype_config["oid"]
            value = datatype_config["value"]
            response = puresnmp.set(**common_parameters, oid=oid, value=value)
        elif method == "multiset":
            mappings = datatype_config["mappings"]
            response = puresnmp.multiset(**common_parameters,
                                         mappings=mappings)
        elif method == "bulkget":
            scalar_oids = datatype_config.get("scalarOid", [])
            scalar_oids = scalar_oids if isinstance(
                scalar_oids, list) else list(scalar_oids)
            repeating_oids = datatype_config.get("repeatingOid", [])
            repeating_oids = repeating_oids if isinstance(
                repeating_oids, list) else list(repeating_oids)
            max_list_size = datatype_config.get("maxListSize", 1)
            response = puresnmp.bulkget(**common_parameters,
                                        scalar_oids=scalar_oids,
                                        repeating_oids=repeating_oids,
                                        max_list_size=max_list_size)._asdict()
        elif method == "bulkwalk":
            oids = datatype_config["oid"]
            oids = oids if isinstance(oids, list) else list(oids)
            bulk_size = datatype_config.get("bulkSize", 10)
            response = {
                binded_var.oid: binded_var.value
                for binded_var in list(
                    puresnmp.bulkwalk(
                        **common_parameters, bulk_size=bulk_size, oids=oids))
            }
        elif method == "table":
            oid = datatype_config["oid"]
            del common_parameters["timeout"]
            num_base_nodes = datatype_config.get("numBaseNodes", 0)
            response = puresnmp.table(**common_parameters,
                                      oid=oid,
                                      num_base_nodes=num_base_nodes)
        elif method == "bulktable":
            oid = datatype_config["oid"]
            num_base_nodes = datatype_config.get("numBaseNodes", 0)
            bulk_size = datatype_config.get("bulkSize", 10)
            response = puresnmp.bulktable(**common_parameters,
                                          oid=oid,
                                          num_base_nodes=num_base_nodes,
                                          bulk_size=bulk_size)
        else:
            log.error("Method \"%s\" - Not found", str(method))
        return response