def write_data_to_file(self, line, output_list, flag):
     try:
         with open(self.output_file, flag) as file:
             file.write(line)
             try:
                 ip_addr(self.host.ip)
                 output_list.count_of_good_hosts += 1
             except ValueError:
                 pass
         return True
     except IOError:
         print("Can't write to output file, IO error")
         exit(1)
Beispiel #2
0
 def folder_generation(self):
     ftp = ftplib.FTP(cfg.ftp_addr, cfg.ftp_user, cfg.ftp_password)
     try:
         ip_addr(self.ip)
         try:
             ftp.cwd(cfg.mikrotik_name)
         except ftplib.error_perm as err:
             if "550" in str(err):
                 ftp.mkd(cfg.mikrotik_name)
                 print("Directory was not found and was created.")
     except ValueError:
         print(self.ip + " is not valid ip address")
         return False
     return True
Beispiel #3
0
def check_if_trusted(ip, trusted_list):
    try:
        _ip = ip_addr(ip)
        for trusted in trusted_list:
            if _ip in ip_net(trusted):
                return True
        else:
            return False
    except:
        return False
def get_reservation(module, client, vlan_id, ip_address):
    """
    Get the reservation status of an IP address

    :arg module: The Ansible module instance
    :arg client: The CC API client instance
    :arg vlan_id: The UUID of the VLAN to check
    :arg ip_address: The IP address to check
    :returns: True/False on the reservation status of the IP address
    """

    try:
        reservations = client.list_reserved_ip(vlan_id=vlan_id,
                                               version=ip_address.version)
        for reserved_ip in reservations:
            if ip_addr(unicode(reserved_ip.get('ipAddress'))) == ip_address:
                return True
    except (AttributeError, NTTMCPAPIException) as e:
        module.fail_json(
            msg='Error getting existing reservations - {0}'.format(e))
    return False
Beispiel #5
0
def reserved_check(ip):
    return ip_addr(ip).is_reserved
Beispiel #6
0
def multicast_check(ip):
    return ip_addr(ip).is_multicast
Beispiel #7
0
def private_check(ip):
    return ip_addr(ip).is_private
def main():
    """
    Main function

    :returns: Public IP address reservation information
    """
    module = AnsibleModule(argument_spec=dict(
        auth=dict(type='dict'),
        region=dict(default='na', type='str'),
        datacenter=dict(required=True, type='str'),
        description=dict(required=False, type='str'),
        network_domain=dict(required=True, type='str'),
        vlan=dict(required=True, type='str'),
        ip_address=dict(required=True, type='str'),
        state=dict(default='present', choices=['present', 'absent'])),
                           supports_check_mode=True)
    try:
        credentials = get_credentials(module)
    except ImportError as e:
        module.fail_json(msg='{0}'.format(e))
    vlan = {}
    network_domain_name = module.params.get('network_domain')
    vlan_name = module.params.get('vlan')
    datacenter = module.params.get('datacenter')
    description = module.params.get('description')
    state = module.params.get('state')
    ip_address = module.params.get('ip_address')

    # Check Imports
    if not HAS_IPADDRESS:
        module.fail_json(msg='Missing Python module: ipaddress')

    # Check the region supplied is valid
    regions = get_regions()
    if module.params.get('region') not in regions:
        module.fail_json(
            msg='Invalid region. Regions must be one of {0}'.format(regions))

    if credentials is False:
        module.fail_json(msg='Error: Could not load the user credentials')

    client = NTTMCPClient(credentials, module.params.get('region'))

    # Check the IP address is valid
    try:
        ip_address_obj = ip_addr(unicode(ip_address))
    except AddressValueError:
        module.fail_json(
            msg='Invalid IPv4 or IPv6 address: {0}'.format(ip_address))

    # Get the CND
    try:
        network = client.get_network_domain_by_name(name=network_domain_name,
                                                    datacenter=datacenter)
        network_domain_id = network.get('id')
    except (KeyError, IndexError, AttributeError, NTTMCPAPIException):
        module.fail_json(msg='Could not find the Cloud Network Domain: {0}'.
                         format(network_domain_name))

    # Get a list of existing VLANs and check if the new name already exists
    try:
        vlan = client.get_vlan_by_name(name=vlan_name,
                                       datacenter=datacenter,
                                       network_domain_id=network_domain_id)
        if not vlan:
            module.fail_json(msg='A valid VLAN is required')
    except NTTMCPAPIException as e:
        module.fail_json(msg='Failed to get a list of VLANs - {0}'.format(e))

    # Check if the IP address is already reserved
    is_reserved = get_reservation(module, client, vlan.get('id'),
                                  ip_address_obj)
    if not is_reserved and state == 'absent':
        module.exit_json(msg='No IPv{0} reservation found for {1}'.format(
            ip_address_obj.version, str(ip_address_obj)))
    elif is_reserved and state == 'present':
        module.exit_json(
            msg='An existing IPv{0} reservation was found for {1}'.format(
                ip_address_obj.version, str(ip_address_obj)))

    if state == 'present':
        # Implement Check Mode
        if module.check_mode:
            module.exit_json(
                msg='The IPv{0} address {1} will be reserved'.format(
                    ip_address_obj.version,
                    str(ip_address_obj),
                ))
        reserve_ip(module, client, vlan.get('id'), ip_address_obj, description)
    elif state == 'absent':
        # Implement Check Mode
        if module.check_mode:
            module.exit_json(
                msg='The IPv{0} address {1} will be unreserved'.format(
                    ip_address_obj.version,
                    str(ip_address_obj),
                ))
        unreserve_ip(module, client, vlan.get('id'), ip_address_obj)