Example #1
0
def add_host(zone, name, ttl, ip, nameserver='127.0.0.1', replace=True,
             **kwargs):
    '''
    Add, replace, or update the A and PTR (reverse) records for a host.

    CLI Example:

    .. code-block:: bash

        salt ns1 ddns.add_host example.com host1 60 10.1.1.1
    '''
    res = update(zone, name, ttl, 'A', ip, nameserver, replace, **kwargs)
    if res is False:
        return False

    fqdn = '{0}.{1}.'.format(name, zone)
    parts = ip.split('.')[::-1]
    popped = []

    # Iterate over possible reverse zones
    while len(parts) > 1:
        p = parts.pop(0)
        popped.append(p)
        zone = '{0}.{1}'.format('.'.join(parts), 'in-addr.arpa.')
        name = '.'.join(popped)
        ptr = update(zone, name, ttl, 'PTR', fqdn, nameserver, replace,
                     **kwargs)
        if ptr:
            return True
    return res
Example #2
0
def add_host(zone, name, ttl, ip, nameserver='127.0.0.1', replace=True):
	"""
	zone: is either the domain name
	name: the client hostname
	ttl:  time to live
	ip:   the client ip
	nameserver: dns server ip
	"""

	# We update the forward zone first

	# This is the update() prototype
	# update(zone, name, ttl, rdtype, data, nameserver='127.0.0.1', replace=False)
	log.info("add_host first update()")
	res = update(zone, name, ttl, 'A', ip, nameserver, replace)
	if res is False:
		return False

	fqdn = '{0}.{1}.'.format(name, zone)
	zone = "in-addr.arpa."
	parts = ip.split('.')
	log.info("log parts")
	log.info(parts)
	# popped is a list to store the reversed IP for reverse zone
	popped = []
	reverse_ip = ".".join(parts[::-1])
	data = '{0}.{1}'.format(reverse_ip, zone)
	log.info("data:" + data)

	ptr = update('100.168.192.in-addr.arpa', parts[3], 600, 'PTR', reverse_ip, nameserver,replace)
	"""
Example #3
0
def add_host(zone, name, ttl, ip, nameserver='127.0.0.1', replace=True):
    """
	zone: is either the domain name
	name: the client hostname
	ttl:  time to live
	ip:   the client ip
	nameserver: dns server ip
	"""

    # We update the forward zone first

    # This is the update() prototype
    # update(zone, name, ttl, rdtype, data, nameserver='127.0.0.1', replace=False)
    log.info("add_host first update()")
    res = update(zone, name, ttl, 'A', ip, nameserver, replace)
    if res is False:
        return False

    fqdn = '{0}.{1}.'.format(name, zone)
    zone = "in-addr.arpa."
    parts = ip.split('.')
    log.info("log parts")
    log.info(parts)
    # popped is a list to store the reversed IP for reverse zone
    popped = []
    reverse_ip = ".".join(parts[::-1])
    data = '{0}.{1}'.format(reverse_ip, zone)
    log.info("data:" + data)

    ptr = update('100.168.192.in-addr.arpa', parts[3], 600, 'PTR', reverse_ip,
                 nameserver, replace)
    """
Example #4
0
def add_host(zone,
             name,
             ttl,
             ip,
             nameserver="127.0.0.1",
             replace=True,
             timeout=5,
             port=53,
             **kwargs):
    """
    Add, replace, or update the A and PTR (reverse) records for a host.

    CLI Example:

    .. code-block:: bash

        salt ns1 ddns.add_host example.com host1 60 10.1.1.1
    """
    res = update(zone, name, ttl, "A", ip, nameserver, timeout, replace, port,
                 **kwargs)
    if res is False:
        return False

    fqdn = "{0}.{1}.".format(name, zone)
    parts = ip.split(".")[::-1]
    popped = []

    # Iterate over possible reverse zones
    while len(parts) > 1:
        p = parts.pop(0)
        popped.append(p)
        zone = "{0}.{1}".format(".".join(parts), "in-addr.arpa.")
        name = ".".join(popped)
        ptr = update(zone, name, ttl, "PTR", fqdn, nameserver, timeout,
                     replace, port, **kwargs)
        if ptr:
            return True
    return res
Example #5
0
def add_host(zone, name, ttl, ip, nameserver='127.0.0.1', replace=True):
	"""
	zone: is either the domain name
	name: the client hostname
	ttl:  time to live
	ip:   the client ip
	nameserver: dns server ip
	"""

	# We update the forward zone first

	# This is the update() prototype
	# update(zone, name, ttl, rdtype, data, nameserver='127.0.0.1', replace=False)
	log.info("add_host first update()")
	res = update(zone, name, ttl, 'A', ip, nameserver, replace)
	if res is False:
		return False

	fqdn = '{0}.{1}.'.format(name, zone)
	zone = "in-addr.arpa."
	parts = ip.split('.')
	log.info("log parts")
	log.info(parts)
	# popped is a list to store the reversed IP for reverse zone
	popped = []
	reverse_ip = ".".join(parts[::-1])
	data = '{0}.{1}'.format(reverse_ip, zone)
	log.info("data:" + data)

	#ptr = update('100.168.192.in-addr.arpa', parts[3], 600, 'PTR', reverse_ip, nameserver,replace)

	# Iterate over possible reverse zones
	while len(parts) > 1:
		p = parts.pop(0)
		popped.append(p)
		zone = '{0}.{1}'.format(p, zone)
		#name = ip.replace('{0}.'.format('.'.join(popped)), '', 1)
		name = ".".join(parts[::-1])

		'''
		log.info("in add_host, calling update()")
		log.info("zone:" + zone + " name:" + name + " data:" + fqdn)
		ptr = update(zone, name, ttl, 'PRT', fqdn, nameserver, replace)
		if ptr:
			log.info(ptr)
			return True
		'''
		# copy from update()

		dns_update = dns.update.Update(zone, keyring=keyring)
		log.info("dns update request")
		#log.info(dns_update)

		request = dns.message.make_query(fqdn, rdtype)
		answer = dns.query.udp(request, nameserver)

		rdtype = dns.rdatatype.from_text('PTR')
		rdata = dns.rdata.from_text(dns.rdataclass.IN, rdtype, data)

		dns_update.replace(data, ttl, 'PTR')

		answer = dns.query.udp(dns_update, nameserver)

		if answer.rcode() > 0:
			log.error("ddns failes:" + zone)
			continue

		log.info(answer)
	return res
Example #6
0
def main():
    options = dns.get_arguments()

    dns.config()

    if options.create is '' and options.delete is '' and options.update is '' and options.reset is '' and options.list is '':
        cprint(
            'Not enough arguments. Requires [--create, --update, --delete, --reset, --list]',
            'yellow')
        exit()

    if options.create is not '':
        if options.host is '':
            cprint('Host is required', 'red')
            exit()

        if options.ipaddress is '':
            cprint('IP Address is required', 'red')
            exit()

        record = options.record
        host = options.host
        ipaddress = options.ipaddress
        dns.create(record, host, ipaddress)

    if options.delete is not '':
        if options.host is '':
            cprint('Host is required', 'yellow')
            exit()

        host = options.host
        dns.delete(host)

    if options.update is None:
        if options.host is '' or options.ipaddress is '':
            cprint('Host and ipaddress required', 'yellow')
            exit()

        record = options.record
        host = options.host
        ipaddress = options.ipaddress
        dns.update(host, ipaddress, record)

    if options.reset is not '':
        os.remove('config.json')
        dns.config()

    if options.list is None:
        domains = dns.get_domains()
        for i in domains:
            try:
                items = dns.get_domain_items(i)
                cprint(i, 'green')
                for json_dict in items:
                    name = ''
                    data = ''
                    base = False
                    for key, value in json_dict.iteritems():
                        #print("key: {key} | value: {value}".format(key=key, value=value))
                        if base == False:
                            if key == 'name':
                                name = value
                            #cprint(value, 'green')
                            if key == 'data':
                                data = value
                    cprint('%-35s %s' % (name, data), 'green')
                    #cprint("[{name} \t => \t {data}]".format(name = name, data = data), 'green')
            except:
                pass