示例#1
0
def _napalm_ipv4_converter(hostname: str(),
                           plateform: str(),
                           cmd_output: json,
                           *,
                           filters=dict()) -> ListIPV4Interface:

    ipv4_addresses_lst = ListIPV4Interface(hostname=hostname,
                                           ipv4_addresses_lst=list())

    for interface_name in cmd_output.get('get_interfaces_ip'):
        if _generic_interface_filter(
                interface_name=_mapping_interface_name(interface_name),
                plateform=plateform,
                filters=filters):
            for ip_addr in cmd_output.get('get_interfaces_ip').get(
                    interface_name).get('ipv4'):

                ipv4_addresses_lst.ipv4_addresses_lst.append(
                    IPV4Interface(
                        interface_name=_mapping_interface_name(interface_name),
                        ip_address_with_mask=ip_addr,
                        netmask=cmd_output.get('get_interfaces_ip').get(
                            interface_name).get('ipv4').get(ip_addr).get(
                                'prefix_length')))

    print(ipv4_addresses_lst)
    return ipv4_addresses_lst
示例#2
0
def _ios_ipv4_converter(hostname: str(),
                        plateform: str(),
                        cmd_output: list,
                        *,
                        filters=dict()) -> ListIPV4Interface:

    ipv4_addresses_lst = ListIPV4Interface(hostname=hostname,
                                           ipv4_addresses_lst=list())

    for interface in cmd_output:

        # In textfsm -> IPADDR & MASK are list
        # Value List IPADDR (\S+?)
        # Value List MASK (\d*)
        for index, ip_addr in enumerate(interface[3]):
            if _generic_interface_filter(
                    interface_name=_mapping_interface_name(interface[0]),
                    plateform=plateform,
                    filters=filters):

                ipv4_addresses_lst.ipv4_addresses_lst.append(
                    IPV4Interface(interface_name=_mapping_interface_name(
                        interface[0]),
                                  ip_address_with_mask=ip_addr,
                                  netmask=interface[4][index]))

    return ipv4_addresses_lst
示例#3
0
def _cumulus_ipv4_converter(hostname: str(),
                            plateform: str(),
                            cmd_output: json,
                            *,
                            filters=dict()) -> ListIPV4Interface:

    ipv4_addresses_lst = ListIPV4Interface(hostname=hostname,
                                           ipv4_addresses_lst=list())

    for interface_name, facts in cmd_output.items():
        # v0 = vrrp / vrr / hsrp / HA ip address
        # bridge is a Linux bridge
        if _generic_interface_filter(
                interface_name=_mapping_interface_name(interface_name),
                plateform=plateform,
                filters=filters):

            for ip_address_in_interface in facts.get("iface_obj").get(
                    'ip_address').get('allentries'):

                if "/128" not in ip_address_in_interface and "/64" not in ip_address_in_interface and \
                        "/48" not in ip_address_in_interface and "::" not in ip_address_in_interface and \
                        "127.0.0.1" not in ip_address_in_interface and "::1/128" not in ip_address_in_interface:

                    ipv4_obj = IPV4Interface(
                        interface_name=_mapping_interface_name(interface_name),
                        ip_address_with_mask=ip_address_in_interface)

                    ipv4_addresses_lst.ipv4_addresses_lst.append(ipv4_obj)

    return ipv4_addresses_lst
示例#4
0
def _arista_ipv4_converter(hostname: str(),
                           plateform: str(),
                           cmd_output: json,
                           *,
                           filters=dict()) -> ListIPV4Interface:

    ipv4_addresses_lst = ListIPV4Interface(hostname=hostname,
                                           ipv4_addresses_lst=list())

    for interface_name, facts in cmd_output.get('interfaces').items():
        if _generic_interface_filter(
                interface_name=_mapping_interface_name(interface_name),
                plateform=plateform,
                filters=filters):
            ip_address = facts.get('interfaceAddress').get('primaryIp').get(
                'address', NOT_SET)

            if ip_address != NOT_SET and "/128" not in ip_address and "/64" not in ip_address and \
                    "/48" not in ip_address and "::" not in ip_address and "127.0.0.1" not in ip_address and \
                    "::1/128" not in ip_address:

                ipv4_addresses_lst.ipv4_addresses_lst.append(
                    IPV4Interface(
                        interface_name=_mapping_interface_name(interface_name),
                        ip_address_with_mask=ip_address,
                        netmask=facts.get('interfaceAddress').get(
                            'primaryIp').get('maskLen', NOT_SET)))

            if len(
                    facts.get('interfaceAddress').get(
                        'secondaryIpsOrderedList')) > 0:

                for ip_addr in facts.get('interfaceAddress').get(
                        'secondaryIpsOrderedList'):
                    secondary_ip = ip_addr.get('address', NOT_SET)

                    if secondary_ip != NOT_SET and "/128" not in secondary_ip and "/64" not in secondary_ip and \
                            "/48" not in secondary_ip and "::" not in secondary_ip and "127.0.0.1" not in secondary_ip and \
                            "::1/128" not in secondary_ip:

                        ipv4_addresses_lst.ipv4_addresses_lst.append(
                            IPV4Interface(
                                interface_name=_mapping_interface_name(
                                    interface_name),
                                ip_address_with_mask=secondary_ip,
                                netmask=ip_addr.get('maskLen', NOT_SET)))

    return ipv4_addresses_lst
示例#5
0
def create_an_ipv4_object_manually(context) -> None:
    """
    Create a IPV4 object manually

    :param context:
    :return None:
    """

    ipv4_addresses_lst = ListIPV4Interface(hostname="leaf01",
                                           ipv4_addresses_lst=list())

    ipv4_addresses_lst.ipv4_addresses_lst.append(
        IPV4Interface(interface_name="eth0",
                      ip_address_with_mask="10.0.4.201",
                      netmask="255.255.255.0"))

    context.object_01 = ipv4_addresses_lst
示例#6
0
def _extreme_vsp_ipv4_converter(hostname: str(),
                                plateform: str(),
                                cmd_output: dict,
                                *,
                                filters=dict()) -> IPV4Interface:

    ipv4_addresses_lst = ListIPV4Interface(hostname=hostname,
                                           ipv4_addresses_lst=list())

    for vrf in cmd_output:

        for interface in cmd_output.get(vrf):
            if _generic_interface_filter(
                    interface_name=_mapping_interface_name(interface[0]),
                    plateform=plateform,
                    filters=filters):
                ipv4_addresses_lst.ipv4_addresses_lst.append(
                    IPV4Interface(interface_name=_mapping_interface_name(
                        interface[0]),
                                  ip_address_with_mask=interface[1],
                                  netmask=interface[2]))

    return ipv4_addresses_lst
示例#7
0
def retrieve_ipv4_for_host(hostname: str, groups,
                           ipv4_yaml_data: dict) -> ListIPV4Interface:

    ip_addresses_lst = ListIPV4Interface(hostname=hostname,
                                         ipv4_addresses_lst=list())

    # Retrieve data in "all:"
    if YAML_ALL_GROUPS_KEY in ipv4_yaml_data.keys():
        for ip_infos in ipv4_yaml_data.get(YAML_ALL_GROUPS_KEY, NOT_SET):

            ipv4_obj = IPV4Interface(
                interface_name=_mapping_interface_name(
                    ip_infos.get('interface_name', NOT_SET)),
                ip_address_with_mask=ip_infos.get('ip_address', NOT_SET),
                netmask=ip_infos.get('netmask', NOT_SET),
            )

            ip_addresses_lst.ipv4_addresses_lst.append(ipv4_obj)

    # Retrieve data in "groups:"
    if YAML_GROUPS_KEY in ipv4_yaml_data.keys():
        for value_key_groups in ipv4_yaml_data.get(YAML_GROUPS_KEY,
                                                   NOT_SET).keys():
            for host_group in groups:
                if "," in value_key_groups:
                    if host_group in value_key_groups.split(","):
                        for ip_infos in ipv4_yaml_data.get(
                                YAML_GROUPS_KEY,
                                NOT_SET).get(value_key_groups):

                            ipv4_obj = IPV4Interface(
                                interface_name=_mapping_interface_name(
                                    ip_infos.get('interface_name', NOT_SET)),
                                ip_address_with_mask=ip_infos.get(
                                    'ip_address', NOT_SET),
                                netmask=ip_infos.get('netmask', NOT_SET),
                            )

                            ip_addresses_lst.ipv4_addresses_lst.append(
                                ipv4_obj)

                else:
                    if host_group == value_key_groups:
                        for ip_infos in ipv4_yaml_data.get(
                                YAML_GROUPS_KEY,
                                NOT_SET).get(value_key_groups):

                            ipv4_obj = IPV4Interface(
                                interface_name=_mapping_interface_name(
                                    ip_infos.get('interface_name', NOT_SET)),
                                ip_address_with_mask=ip_infos.get(
                                    'ip_address', NOT_SET),
                                netmask=ip_infos.get('netmask', NOT_SET),
                            )

                            ip_addresses_lst.ipv4_addresses_lst.append(
                                ipv4_obj)

    # Retrieve data in "devices:"
    if YAML_DEVICES_KEY in ipv4_yaml_data.keys():
        for value_key_devices in ipv4_yaml_data.get(YAML_DEVICES_KEY,
                                                    NOT_SET).keys():
            if "," in value_key_devices:
                if hostname in value_key_devices.split(","):
                    for ip_infos in ipv4_yaml_data.get(YAML_DEVICES_KEY,
                                                       NOT_SET).get(
                                                           value_key_devices,
                                                           NOT_SET):

                        ipv4_obj = IPV4Interface(
                            interface_name=_mapping_interface_name(
                                ip_infos.get('interface_name', NOT_SET)),
                            ip_address_with_mask=ip_infos.get(
                                'ip_address', NOT_SET),
                            netmask=ip_infos.get('netmask', NOT_SET),
                        )

                        ip_addresses_lst.ipv4_addresses_lst.append(ipv4_obj)

            else:
                if hostname == value_key_devices:

                    for ip_infos in ipv4_yaml_data.get(
                            YAML_GROUPS_KEY, NOT_SET).get(value_key_devices):

                        ipv4_obj = IPV4Interface(
                            interface_name=_mapping_interface_name(
                                ip_infos.get('interface_name', NOT_SET)),
                            ip_address_with_mask=ip_infos.get(
                                'ip_address', NOT_SET),
                            netmask=ip_infos.get('netmask', NOT_SET),
                        )

                        ip_addresses_lst.ipv4_addresses_lst.append(ipv4_obj)

    # Retrieve data per device
    if hostname in ipv4_yaml_data.keys():

        for ip_infos in ipv4_yaml_data.get(hostname, NOT_SET):
            ipv4_obj = IPV4Interface(
                interface_name=_mapping_interface_name(
                    ip_infos.get('interface_name', NOT_SET)),
                ip_address_with_mask=ip_infos.get('ip_address', NOT_SET),
                netmask=ip_infos.get('netmask', NOT_SET),
            )

            ip_addresses_lst.ipv4_addresses_lst.append(ipv4_obj)

    return ip_addresses_lst
示例#8
0
def _juniper_ipv4_converter(hostname: str(),
                            plateform: str(),
                            cmd_output: dict,
                            *,
                            filters=dict()) -> ListIPV4Interface:

    ipv4_addresses_lst = ListIPV4Interface(hostname=hostname,
                                           ipv4_addresses_lst=list())

    for key in cmd_output.get('interface-information')[0].keys():
        if key != "attributes":
            for interface in cmd_output.get('interface-information')[0].get(
                    key):
                if "logical-interface" in interface.keys():
                    for logic_interface in interface.get("logical-interface"):
                        if _generic_interface_filter(
                                interface_name=_mapping_interface_name(
                                    logic_interface.get("name")[0].get(
                                        "data", NOT_SET)),
                                plateform=plateform,
                                filters=filters):

                            if "address-family" in logic_interface.keys():
                                if "address-family-name" in logic_interface.get(
                                        "address-family")[0]:
                                    if logic_interface.get("address-family")[0].get("address-family-name")[0].get("data") == "inet" and \
                                            logic_interface.get("address-family")[0].get("interface-address") is not None:

                                        for interface_ip in logic_interface.get(
                                                "address-family")[0].get(
                                                    "interface-address"):

                                            ip_addr = interface_ip.get(
                                                "ifa-local")[0].get(
                                                    "data", NOT_SET)

                                            if ip_addr != NOT_SET and \
                                                    "/128" not in ip_addr and \
                                                    "/64" not in ip_addr and \
                                                    "/48" not in ip_addr and \
                                                    "::" not in ip_addr and \
                                                    "127.0.0.1" not in ip_addr and \
                                                    "::1/128" not in ip_addr and \
                                                    "128.0.0." not in ip_addr and \
                                                    ".32768" not in logic_interface.get("name")[0].get("data", NOT_SET):

                                                if "lo" in logic_interface.get(
                                                        "name")[0].get(
                                                            "data", NOT_SET):
                                                    ipv4_addresses_lst.ipv4_addresses_lst.append(
                                                        IPV4Interface(
                                                            interface_name=
                                                            _mapping_interface_name(
                                                                logic_interface
                                                                .get("name")
                                                                [0].get(
                                                                    "data",
                                                                    NOT_SET)),
                                                            ip_address_with_mask
                                                            =ip_addr,
                                                            netmask=
                                                            "255.255.255.255"))
                                                else:
                                                    ipv4_addresses_lst.ipv4_addresses_lst.append(
                                                        IPV4Interface(
                                                            interface_name=
                                                            _mapping_interface_name(
                                                                logic_interface
                                                                .get("name")
                                                                [0].get(
                                                                    "data",
                                                                    NOT_SET)),
                                                            ip_address_with_mask
                                                            =ip_addr,
                                                        ))

    return ipv4_addresses_lst
示例#9
0
def _nexus_ipv4_converter(hostname: str(),
                          plateform: str(),
                          cmd_outputs: list,
                          *,
                          filters=dict()) -> ListIPV4Interface:

    if cmd_outputs is None:
        return False

    ipv4_addresses_lst = ListIPV4Interface(hostname=hostname,
                                           ipv4_addresses_lst=list())

    for cmd_output in cmd_outputs:

        if cmd_output is not None and 'TABLE_intf' in cmd_output.keys():

            if isinstance(cmd_output.get('TABLE_intf'), list):
                for interface in cmd_output.get('TABLE_intf'):
                    if _generic_interface_filter(
                            interface_name=_mapping_interface_name(
                                interface.get('ROW_intf').get('intf-name')),
                            plateform=plateform,
                            filters=filters):
                        if isinstance(interface, dict):

                            ip_address = interface.get('ROW_intf').get(
                                'prefix', NOT_SET)

                            if ip_address != NOT_SET and "/128" not in ip_address and "/64" not in ip_address and \
                                    "/48" not in ip_address and "::" not in ip_address and "127.0.0.1" not in ip_address and \
                                    "::1/128" not in ip_address:

                                ipv4_obj = IPV4Interface(
                                    interface_name=_mapping_interface_name(
                                        interface.get('ROW_intf').get(
                                            'intf-name')),
                                    ip_address_with_mask=ip_address,
                                    netmask=interface.get('ROW_intf').get(
                                        'masklen'))

                                ipv4_addresses_lst.ipv4_addresses_lst.append(
                                    ipv4_obj)

                            if 'TABLE_secondary_address' in interface.get(
                                    'ROW_intf').keys():
                                ipv4_addresses_lst.ipv4_addresses_lst.append(
                                    IPV4Interface(
                                        interface_name=_mapping_interface_name(
                                            interface.get('ROW_intf').get(
                                                'intf-name')),
                                        ip_address_with_mask=interface.
                                        get('ROW_intf').get(
                                            'TABLE_secondary_address').get(
                                                'ROW_secondary_address').get(
                                                    'prefix1'),
                                        netmask=interface.get('ROW_intf').get(
                                            'TABLE_secondary_address').get(
                                                'ROW_secondary_address').get(
                                                    'masklen1')))

            elif isinstance(cmd_output.get('TABLE_intf'), dict):
                for interface, facts in cmd_output.get('TABLE_intf').items():
                    if _generic_interface_filter(
                            interface_name=_mapping_interface_name(
                                facts.get('intf-name')),
                            plateform=plateform,
                            filters=filters):
                        ip_address = facts.get('prefix', NOT_SET)

                        if ip_address != NOT_SET and "/128" not in ip_address and "/64" not in ip_address and \
                                "/48" not in ip_address and "::" not in ip_address and "127.0.0.1" not in ip_address and \
                                "::1/128" not in ip_address:
                            ipv4_obj = IPV4Interface(
                                interface_name=_mapping_interface_name(
                                    facts.get('intf-name')),
                                ip_address_with_mask=ip_address,
                                netmask=facts.get('masklen'))

                            ipv4_addresses_lst.ipv4_addresses_lst.append(
                                ipv4_obj)

                        if 'TABLE_secondary_address' in facts.keys():
                            ipv4_addresses_lst.ipv4_addresses_lst.append(
                                IPV4Interface(
                                    interface_name=_mapping_interface_name(
                                        facts.get('intf-name')),
                                    ip_address_with_mask=facts.get(
                                        'TABLE_secondary_address').get(
                                            'ROW_secondary_address').get(
                                                'prefix1'),
                                    netmask=facts.get(
                                        'TABLE_secondary_address').get(
                                            'ROW_secondary_address').get(
                                                'masklen1')))

    return ipv4_addresses_lst