コード例 #1
0
def main():
    """
    - This script will:
      - load a file with a configuration to be deployed to a network device
      - identify the IPv4 addresses that will be configured on interfaces
      - search in the DNA Center database if these IPV4 addresses are configured on any interfaces
      - find if any clients are using the IPv4 addresses
    - Determine if deploying the configuration file will create an IP duplicate
    """

    # configuration template file name
    config_file = 'configuration_template.txt'

    # open file with the template
    cli_file = open(config_file, 'r')

    # read the file
    cli_config = cli_file.read()
    print('\n The CLI template:\n')
    print(cli_config)

    ipv4_address_list = utils.identify_ipv4_address(cli_config)
    print('\nThese valid IPv4 addresses will be configured:\n')
    print(ipv4_address_list)

    # get the DNA Center Auth token

    dnac_token = dnac_apis.get_dnac_jwt_token(DNAC_AUTH)
    print('\nThe DNA Center token is: ', dnac_token, '\n')

    # check each address against network devices and clients database
    # initialize duplicate_ip

    duplicate_ip = False
    for ipv4_address in ipv4_address_list:

        # check against network devices interfaces

        try:
            device_info = dnac_apis.check_ipv4_network_interface(ipv4_address, dnac_token)
            duplicate_ip = True
            if len(device_info) == 2:
                print('The IPv4 address ', ipv4_address, ' is used on this device ', device_info[0], ' interface ', device_info[1])
            else:
                print('The IPv4 address ', ipv4_address, ' is used on this device ', device_info[0])
        except:
            pass

        # check against any hosts

        try:
            client_info = dnac_apis.get_client_info(ipv4_address, dnac_token)
            if client_info is not None:
                duplicate_ip = True
                print('The IPv4 address ', ipv4_address, ' is used by a client')
        except:
            pass

    if duplicate_ip:
        print('\nDeploying the template ', config_file, ' will create IP duplicates')
    else:
        print('\nDeploying the template ', config_file, ' will not create IP duplicates')

    # end of the application run

    print('\n\nEnd of Application Run')
コード例 #2
0
def main():
    """
    This sample script will:
     - validate the CLI template file name exists
     - open the file
     - select the IPv4 addresses to be configured on interfaces
     - validate if the selected IPv4 addresses are valid IPv4 addresses
     - verify if the IPv4 addresses are in use by a network device
     - verify if the IPv4 addresses are in use by a client
     - verify if the IPv4 addresses are reachable
     - deploying the configuration file will create an IPv4 address conflict if any of the above steps fail for one IPv4 address
    """

    print('\n\nStart of Application "ip_conflict_prevention.py" Run')

    # define the template config file name
    config_file = 'configuration_template.txt'

    # enter and validate the file exists
    if not os.path.isfile(config_file):
        print('File not found')

    # open file with the template
    cli_file = open(config_file, 'r')

    # read the file
    cli_config = cli_file.read()
    print('\nThe CLI template:\n')
    print(cli_config)

    ipv4_address_list = utils.identify_ipv4_address(cli_config)
    print('\nThe CLI template will configure these valid IPv4 addresses:')
    print(ipv4_address_list)

    # get the DNA Center Auth token

    dnac_token = dnac_apis.get_dnac_jwt_token(DNAC_AUTH)

    # check each address against network devices and clients database

    duplicate_ip = False
    for ipv4_address in ipv4_address_list:

        # check against network devices interfaces

        device_info = dnac_apis.check_ipv4_network_interface(
            ipv4_address, dnac_token)
        if device_info[0] == 'Found':
            duplicate_ip = True
            print('\nThe IPv4 address ', ipv4_address,
                  ' is used by this device ', device_info[1], ', ',
                  device_info[2])
        else:
            print('\nThe IPv4 address ', ipv4_address,
                  ' is not used by any network devices')

            # if IP address no used by network devices continue to check against any hosts

            try:
                client_info = dnac_apis.get_client_info(
                    ipv4_address, dnac_token)
                if client_info:
                    duplicate_ip = True
                    print('The IPv4 address ', ipv4_address,
                          ' is used by a client')
                else:
                    print('The IPv4 address ', ipv4_address,
                          ' is not used by a client')

                    # if IP address not used by a client, continue with the reachability test

                    reachable = utils.ping_return(ipv4_address)
                    if reachable == 'Success':
                        duplicate_ip = True
                        print('The IPv4 address ', ipv4_address,
                              ' is reachable')
                    else:
                        print('The IPv4 address ', ipv4_address,
                              ' is not reachable')
            except:
                pass

    if duplicate_ip:
        print('\nDeploying the template ', config_file,
              ' will create an IP address conflict')
    else:
        print('\nDeploying the template ', config_file,
              ' will not create an IP address conflict')

    # end of the application run
    print('\n\nEnd of Application "ip_conflict_prevention.py" Run')