コード例 #1
0
def draw():
    """ Select a device/interface and demonstrate."""
    print(plain(doc(interface_configuration)))
    foundInterface = False

    G = nx.Graph()

    for device_name in sorted(inventory_connected()):
        G.add_node(device_name)
        print("%s:" % device_name)
        mgmt_name = management_interface(device_name)
        for interface_name in sorted(interface_names(device_name)):
            # Choose interface on 'data plane' not 'control plane'.
            if interface_name == mgmt_name:
                continue
            else:
                match(device_name, interface_name)
                foundInterface = True
                demonstrate(device_name, interface_name)
                to_interface = interface_configuration(device_name, interface_name)[1].split(" ")[1]
                print("interface:%s" % to_interface)
                G.add_edge(device_name, to_interface)
        if not foundInterface:
            print("There are no suitable network interfaces for this device.")

    print(G.nodes())
    print(G.edges())
    # nx.draw(G)
    nx.draw_networkx(G, with_labels=True)
    plt.show()
コード例 #2
0
def cdp_neighbors_json(device_name):
    response = odl_http_get(
        url_neighbors, {"node-id": device_name}, "application/json", expected_status_code=(200, 404)
    )
    j = json.loads(response.text)
    container = j["cdp"]["nodes"]["node"][0]
    mgmt_name = management_interface(device_name)
    neighbor_set = set()
    if "neighbors" in container:
        neighbors = container["neighbors"]
        #       print(json.dumps(neighbors, indent=2))
        if "summaries" in neighbors:
            summary = neighbors["summaries"]["summary"]
            for device_info in summary:
                neighbor_id = device_info["device-id"]
                neighbor_interface = device_info["cdp-neighbor"][0]["port-id"]
                device_interface = device_info["interface-name"]
                if device_interface in mgmt_name:
                    continue
                neighbor_info = (
                    device_name,
                    device_interface.encode("utf8"),
                    neighbor_id.encode("utf8"),
                    neighbor_interface.encode("utf8"),
                )
                neighbor_set.add(neighbor_info)
            return neighbor_set
コード例 #3
0
def main():
    ''' Select a device and demonstrate.'''
    print(plain(doc(acl_apply_packet_filter)))
    inventory = inventory_acl()
    if not inventory:
        print('There are no ACL capable devices to examine. Demonstration cancelled.')
    else:
        for device_name in inventory:
            mgmt_name = management_interface(device_name)
            acl_names = acl_list(device_name)
            if not acl_names:
                print('Skip device with no ACLs: ', device_name)
                continue
            else:
                random.shuffle(acl_names)
                acl_name = acl_names[0]
            for ic in interface_configuration_tuple(device_name):
                if ic.name == mgmt_name:
                    continue
                print('Consider %s %s in=%s, out=%s' % (device_name, ic.name, ic.packet_filter_inbound, ic.packet_filter_outbound))
                if not ic.packet_filter_outbound:
                    demonstrate(device_name, ic.name, 'outbound', acl_name)
                    return EX_OK
                if not ic.packet_filter_inbound:
                    demonstrate(device_name, ic.name, 'inbound', acl_name)
                    return EX_OK
        print('There are no network interfaces available to apply an ACL. Demonstration cancelled.')
    return EX_TEMPFAIL
コード例 #4
0
def main():
    ''' Select a device and demonstrate.'''
    print(plain(doc(acl_apply_packet_filter)))
    inventory = inventory_acl()
    if not inventory:
        print(
            'There are no ACL capable devices to examine. Demonstration cancelled.'
        )
    else:
        for device_name in inventory:
            mgmt_name = management_interface(device_name)
            acl_names = acl_list(device_name)
            if not acl_names:
                print('Skip device with no ACLs: ', device_name)
                continue
            else:
                random.shuffle(acl_names)
                acl_name = acl_names[0]
            for ic in interface_configuration_tuple(device_name):
                if ic.name == mgmt_name:
                    continue
                print('Consider %s %s in=%s, out=%s' %
                      (device_name, ic.name, ic.packet_filter_inbound,
                       ic.packet_filter_outbound))
                if not ic.packet_filter_outbound:
                    demonstrate(device_name, ic.name, 'outbound', acl_name)
                    return os.EX_OK
                if not ic.packet_filter_inbound:
                    demonstrate(device_name, ic.name, 'inbound', acl_name)
                    return os.EX_OK
        print(
            'There are no network interfaces available to apply an ACL. Demonstration cancelled.'
        )
    return os.EX_TEMPFAIL
コード例 #5
0
def link_info():
    #def main():
    ii = 0
    link_info_dic = {}
    for device_name in inventory_connected():

        response = odl_http_get(url_neighbors, {'node-id': device_name},
                                'application/json',
                                expected_status_code=(200, 404))
        l = json.loads(response.text)
        container_l = l["cdp"]["nodes"]["node"][0]
        mgmt_name = management_interface(device_name)

        if "neighbors" in container_l:
            neighbors = container_l["neighbors"]
            #           print(json.dumps(neighbors, indent=2))
            if "summaries" in neighbors:
                summary = neighbors["summaries"]["summary"]
                for device_info in summary:
                    neighbor_id = device_info["device-id"]
                    if neighbor_id == "kcy":
                        neighbor_id = 'iosxrv-1'
                    elif neighbor_id == "lax":
                        neighbor_id = 'iosxrv-2'
                    elif neighbor_id == "min":
                        neighbor_id = 'iosxrv-3'
                    elif neighbor_id == "por":
                        neighbor_id = 'iosxrv-4'
                    elif neighbor_id == "san":
                        neighbor_id = 'iosxrv-5'
                    elif neighbor_id == "sea":
                        neighbor_id = 'iosxrv-6'
                    elif neighbor_id == "sfc":
                        neighbor_id = 'iosxrv-7'
                    else:
                        neighbor_id = 'iosxrv-8'
                    device_interface = device_info["interface-name"]
                    if device_interface in mgmt_name:
                        continue
                    link_info = (device_name, neighbor_id.encode('utf8'))
                    link_info_re = (neighbor_id.encode('utf8'), device_name)
                    jj = 0
                    flag = 1
                    while ii > jj:
                        if link_info_dic[jj] == link_info or link_info_dic[
                                jj] == link_info_re:
                            flag = 0
                            jj = jj + 1
                        else:
                            jj = jj + 1
                    if flag == 1:
                        link_info_dic[ii] = link_info
                        ii = ii + 1
                    else:
                        jj = 0


#                   return link_info_set
    return link_info_dic
コード例 #6
0
def link_info():
    # def main():
    ii = 0
    link_info_dic = {}
    for device_name in inventory_connected():

        response = odl_http_get(
            url_neighbors, {"node-id": device_name}, "application/json", expected_status_code=(200, 404)
        )
        l = json.loads(response.text)
        container_l = l["cdp"]["nodes"]["node"][0]
        mgmt_name = management_interface(device_name)

        if "neighbors" in container_l:
            neighbors = container_l["neighbors"]
            #           print(json.dumps(neighbors, indent=2))
            if "summaries" in neighbors:
                summary = neighbors["summaries"]["summary"]
                for device_info in summary:
                    neighbor_id = device_info["device-id"]
                    if neighbor_id == "kcy":
                        neighbor_id = "iosxrv-1"
                    elif neighbor_id == "lax":
                        neighbor_id = "iosxrv-2"
                    elif neighbor_id == "min":
                        neighbor_id = "iosxrv-3"
                    elif neighbor_id == "por":
                        neighbor_id = "iosxrv-4"
                    elif neighbor_id == "san":
                        neighbor_id = "iosxrv-5"
                    elif neighbor_id == "sea":
                        neighbor_id = "iosxrv-6"
                    elif neighbor_id == "sfc":
                        neighbor_id = "iosxrv-7"
                    else:
                        neighbor_id = "iosxrv-8"
                    device_interface = device_info["interface-name"]
                    if device_interface in mgmt_name:
                        continue
                    link_info = (device_name, neighbor_id.encode("utf8"))
                    link_info_re = (neighbor_id.encode("utf8"), device_name)
                    jj = 0
                    flag = 1
                    while ii > jj:
                        if link_info_dic[jj] == link_info or link_info_dic[jj] == link_info_re:
                            flag = 0
                            jj = jj + 1
                        else:
                            jj = jj + 1
                    if flag == 1:
                        link_info_dic[ii] = link_info
                        ii = ii + 1
                    else:
                        jj = 0

    #                   return link_info_set
    return link_info_dic
コード例 #7
0
def main():
    ''' Select a device/interface and demonstrate.'''
    print(plain(doc(interface_configuration_tuple)))
    for device_name in inventory_connected():
        mgmt_name = management_interface(device_name)
        for interface_name in interface_names(device_name):
            # Choose interface on 'data plane' not 'control plane'.
            if interface_name == mgmt_name:
                continue
            return demonstrate(device_name, interface_name)
    print("There are no suitable network devices. Demonstration cancelled.")
コード例 #8
0
def main():
    ''' Select a device/interface and demonstrate.'''
    print(plain(doc(interface_configuration)))
    for device_name in inventory_connected():
        mgmt_name = management_interface(device_name)
        for interface_name in interface_names(device_name):
            # Choose interface on 'data plane' not 'control plane'.
            if interface_name == mgmt_name:
                continue
            return demonstrate(device_name, interface_name)
    print("There are no suitable network devices. Demonstration cancelled.")
コード例 #9
0
def main():
    print(plain(doc(interface_configuration_update)))
    for device_name in inventory_connected():
        mgmt_name = management_interface(device_name)
        for interface_name in interface_names(device_name):
            # Avoid modifying the management interface.
            if interface_name == mgmt_name:
                continue
            demonstrate(device_name, interface_name)
            return EX_OK
    print("There are no suitable network devices. Demonstration cancelled.")
    return EX_TEMPFAIL
def main():
    print(plain(doc(interface_configuration_update)))
    for device_name in inventory_connected():
        mgmt_name = management_interface(device_name)
        for interface_name in interface_names(device_name):
            # Avoid modifying the management interface.
            if interface_name == mgmt_name:
                continue
            demonstrate(device_name, interface_name)
            return os.EX_OK
    print("There are no suitable network devices. Demonstration cancelled.")
    return os.EX_TEMPFAIL
コード例 #11
0
ファイル: sample.py プロジェクト: CTOCHN/cosc-learning-labs
def sample_interface():
    'Return a tuple of (device_name, interface_name) if possible; otherwise raise LookupError.'
    for device_name in inventory_connected():
        print device_name
        management_name = management_interface(device_name)
        print management_name
        if management_name == None:
            continue # Uncertain which interface is for management.
        for interface_name in interface_names(device_name):
            print interface_name
            if interface_name == management_name:
                continue
            return (device_name, interface_name)
    raise LookupError("Unable to find a suitable sample device and interface in the inventory.")
コード例 #12
0
def main():
    ''' Select a device/interface and demonstrate.'''
    print(plain(doc(interface_configuration)))
    foundInterface = False
    for device_name in sorted(inventory_connected()):
        print("%s:" % device_name)
        mgmt_name = management_interface(device_name)
        for interface_name in sorted(interface_names(device_name)):
            # Choose interface on 'data plane' not 'control plane'.
            if interface_name == mgmt_name:
                continue
            else:
                foundInterface = True
                demonstrate(device_name, interface_name)
        if not foundInterface:
            print("There are no suitable network interfaces for this device.")
コード例 #13
0
ファイル: sample.py プロジェクト: kjarrad/cosc-learning-labs
def sample_interface():
    'Return a tuple of (device_name, interface_name) if possible; otherwise raise LookupError.'
    for device_name in inventory_connected():
        print device_name
        management_name = management_interface(device_name)
        print management_name
        if management_name == None:
            continue  # Uncertain which interface is for management.
        for interface_name in interface_names(device_name):
            print interface_name
            if interface_name == management_name:
                continue
            return (device_name, interface_name)
    raise LookupError(
        "Unable to find a suitable sample device and interface in the inventory."
    )
コード例 #14
0
def sample_destination(device_name):
    """ Determine a suitable destination for a static route from the specified network device. """
    # Implementation: use 'next subnet' of any data interface on the specified device.
    mgmt_name = management_interface(device_name)
    interface_list = interface_names(device_name)
    for interface_name in interface_list:
        if interface_name == mgmt_name:
            # Do not configure static routes on the control plane.
            continue
        config = interface_configuration_tuple(device_name, interface_name)
        if config.address is None:
            # Skip network interface with unassigned IP address.
            continue
        interface_network = to_ip_network(config.address, config.netmask)
        destination_network = next_subnet(interface_network)
        return destination_network
    return None
コード例 #15
0
def sample_destination(device_name):
    """ Determine a suitable destination for a static route from the specified network device. """
    # Implementation: use 'next subnet' of any data interface on the specified device.
    mgmt_name = management_interface(device_name)
    interface_list = interface_names(device_name)
    for interface_name in interface_list:
        if interface_name == mgmt_name:
            # Do not configure static routes on the control plane.             
            continue
        config = interface_configuration_tuple(device_name, interface_name)
        if config.address is None:
            # Skip network interface with unassigned IP address.             
            continue
        interface_network = to_ip_network(config.address, config.netmask)
        destination_network = next_subnet(interface_network)
        return destination_network
    return None
コード例 #16
0
def main():
    print(plain(doc(interface_configuration_update)))
    for device_name in inventory_connected():
        mgmt_name = management_interface(device_name)
        for interface_name in interface_names(device_name):
            # Choose interface on 'data plane' not 'control plane'.
            if interface_name == mgmt_name:
                continue
            interface_config = interface_configuration(device_name, interface_name)
            if not interface_config.address:
                continue
            if not interface_config.shutdown:
                continue
            demonstrate(device_name, interface_config)
            return os.EX_OK
    print("There are no suitable network devices/interfaces. Demonstration cancelled.")
    return os.EX_TEMPFAIL
コード例 #17
0
def demonstrate(device_name):
    ''' Apply function 'static_route_create' to the specified device for a new destination.'''
    mgmt_name = management_interface(device_name)
    interface_list = interface_names(device_name)
    for interface_name in interface_list:
        if interface_name == mgmt_name:
            # Do not configure static routes on the control plane.             
            continue
        config = interface_configuration_tuple(device_name, interface_name)
        if config.address is None:
            # Skip network interface with unassigned IP address.             
            continue
        print()
        interface_network = to_ip_network(config.address, config.netmask)
        next_hop_address = match(device_name, interface_network)
        if next_hop_address is None:
            print('No end-point for %s/%s/%s/%s' % (device_name, interface_name, config.address, config.netmask))
            continue
        destination_network = static_route_fixture.new_destination(device_name, interface_network)
        print('static_route_create(%s, %s, %s)' % (device_name, destination_network, next_hop_address))
        static_route_create(device_name, destination_network, next_hop_address)
        return True
    return False
コード例 #18
0
def cdp_neighbors_json(device_name):
    response = odl_http_get(url_neighbors, {'node-id': device_name},
                            'application/json',
                            expected_status_code=(200, 404))
    j = json.loads(response.text)
    container = j["cdp"]["nodes"]["node"][0]
    mgmt_name = management_interface(device_name)
    neighbor_set = set()
    if "neighbors" in container:
        neighbors = container["neighbors"]
        #       print(json.dumps(neighbors, indent=2))
        if "summaries" in neighbors:
            summary = neighbors["summaries"]["summary"]
            for device_info in summary:
                neighbor_id = device_info["device-id"]
                neighbor_interface = device_info["cdp-neighbor"][0]["port-id"]
                device_interface = device_info["interface-name"]
                if device_interface in mgmt_name:
                    continue
                neighbor_info = (device_name, device_interface.encode('utf8'),
                                 neighbor_id.encode('utf8'),
                                 neighbor_interface.encode('utf8'))
                neighbor_set.add(neighbor_info)
            return neighbor_set
コード例 #19
0
def demonstrate(device_name):
    ''' Apply function 'management_interface' to the specified device.'''
    print('management_interface(' + device_name, end='): ')
    print(management_interface(device_name))
コード例 #20
0
def demonstrate(device_name):
    """ Apply function 'management_interface' to the specified device."""
    print("management_interface(" + device_name, end="): ")
    print(management_interface(device_name))
コード例 #21
0
def demonstrate(device_name):
    ''' Apply function 'management_interface' to the specified device.'''
    print('management_interface(' + device_name, end='): ')
    print(management_interface(device_name))