Last active
January 22, 2016 18:44
-
-
Save coffeemancy/5bd230521625c333ec41 to your computer and use it in GitHub Desktop.
wait for DNS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
try: | |
import dns.exception | |
import dns.resolver | |
except ImportError as ex: | |
raise SystemExit('You need to pip install dnspython') | |
import signal | |
import sys | |
timeout_seconds = 300 | |
class TimeoutError(BaseException): | |
pass | |
def handler(signum, frame): | |
raise TimeoutError('{}\n{}'.format(signum, frame)) | |
signal.signal(signal.SIGALRM, handler) | |
signal.alarm(timeout_seconds) | |
dns_ex = None | |
if __name__ == '__main__': | |
fqdn = sys.argv[1] | |
print 'Waiting up to {0} seconds for {1}'.format(timeout_seconds, fqdn) | |
while True: | |
try: | |
ans = dns.resolver.query(fqdn) | |
break | |
except dns.resolver.NXDOMAIN as ex: | |
pass | |
except TimeoutError as ex: | |
raise SystemExit('Timed out waiting for DNS') | |
signal.alarm(0) | |
print 'DNS: {}'.format(ans.rrset) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment