Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # need pip install dnspython
- import dns.name, dns.query, dns.resolver
- import socket
- def wait_for_dns(new_domain: str) -> typing.List[str]:
- """
- Waits until a dns record becomes awailable, resolves it and returns the corresponding ip addresses.
- NOTE: Amazon route53 will report status as INSYNC even though the changes have not propogated to
- the authorative nameserver.
- :param new_domain: The domain to query for
- :return: A list of corresponding ip addresses
- """
- authority_nameservers = \
- dns.query.udp(dns.message.make_query(new_domain, dns.rdatatype.NS), PUBLIC_NAMESERVER).authority
- if len(authority_nameservers) == 0:
- raise ValueError("Could not find authorative nameserver, aborting")
- # authority_ns.to_string() will return something like this:
- # 'aws.g.is. 899 IN SOA ns-1834.awsdns-37.co.uk. awsdns-hostmaster.amazon.com. 1 7200 900 1209600 86400'
- authority_ns = authority_nameservers[0].to_text().split()[4].rstrip('.')
- domain_resolver = dns.resolver.Resolver(configure=False)
- print("Using %s as authorative nameserver" % (authority_ns,))
- domain_resolver.nameservers = [socket.gethostbyname(authority_ns)]
- while True:
- try:
- result = domain_resolver.query(new_domain, 'A')
- print()
- break
- except (dns.resolver.NXDOMAIN, dns.resolver.NoAnswer):
- sys.stdout.write('.')
- sys.stdout.flush()
- time.sleep(1)
- return [r.to_text() for r in result]
Add Comment
Please, Sign In to add comment