|
| 1 | +#!/usr/bin/env python |
| 2 | +# |
| 3 | + |
| 4 | +""" |
| 5 | +get-ip.py: resolve given DNS names into IP addresses. The -s switch |
| 6 | +constains answers to only ones secured by DNSSEC. The -4 switch only |
| 7 | +returns IPv4 addresses, the -6 switch only IPv6 addresses. |
| 8 | +
|
| 9 | +An example run: |
| 10 | +$ python async-get-ip.py www.panix.com www.isoc.org www.verisignlabs.com |
| 11 | +
|
| 12 | +submitted query for www.panix.com |
| 13 | +submitted query for www.isoc.org |
| 14 | +submitted query for www.verisignlabs.com |
| 15 | +www.panix.com: IPv4 166.84.62.125 |
| 16 | +www.panix.com: IPv4 166.84.62.253 |
| 17 | +www.verisignlabs.com: IPv4 72.13.58.64 |
| 18 | +www.verisignlabs.com: IPv6 2620:74:13:4400::201 |
| 19 | +www.isoc.org: IPv4 212.110.167.157 |
| 20 | +www.isoc.org: IPv6 2001:41c8:20::19 |
| 21 | +
|
| 22 | +""" |
| 23 | + |
| 24 | +import getdns, sys, getopt |
| 25 | + |
| 26 | +desired_addr_type = None |
| 27 | + |
| 28 | +def cbk(type, result, userarg, tid): |
| 29 | + if type == getdns.CALLBACK_COMPLETE: |
| 30 | + status = result.status |
| 31 | + if status == getdns.RESPSTATUS_GOOD: |
| 32 | + for addr in result.just_address_answers: |
| 33 | + addr_type = addr['address_type'] |
| 34 | + addr_data = addr['address_data'] |
| 35 | + if (desired_addr_type == None) or (addr_type == desired_addr_type): |
| 36 | + print("{0}: {1} {2}".format(userarg, addr_type, addr_data)) |
| 37 | + elif status == getdns.RESPSTATUS_NO_SECURE_ANSWERS: |
| 38 | + print("{0}: No DNSSEC secured responses found".format(hostname)) |
| 39 | + else: |
| 40 | + print("{0}: getdns.address() returned error: {1}".format(hostname, status)) |
| 41 | + elif type == getdns.CALLBACK_CANCEL: |
| 42 | + print('Callback cancelled') |
| 43 | + elif type == getdns.CALLBACK_TIMEOUT: |
| 44 | + print('Query timed out') |
| 45 | + else: |
| 46 | + print('Unknown error') |
| 47 | + |
| 48 | + |
| 49 | +def usage(): |
| 50 | + print("""\ |
| 51 | +Usage: get-ip.py [-s] [-4|-6] <domain1> <domain2> ... |
| 52 | +
|
| 53 | + -s: only return DNSSEC secured answers |
| 54 | + -4: only return IPv4 address answers |
| 55 | + -6: only return IPv6 address answers |
| 56 | +
|
| 57 | +-4 and -6 are mutually exclusive. If both are specified, IPv6 wins. |
| 58 | +""") |
| 59 | + sys.exit(1) |
| 60 | + |
| 61 | +try: |
| 62 | + (options, args) = getopt.getopt(sys.argv[1:], 's46') |
| 63 | +except getopt.GetoptError: |
| 64 | + usage() |
| 65 | +else: |
| 66 | + if not args: |
| 67 | + usage() |
| 68 | + |
| 69 | +extensions = { "return_both_v4_and_v6" : getdns.EXTENSION_TRUE } |
| 70 | + |
| 71 | +for (opt, optval) in options: |
| 72 | + if opt == "-s": |
| 73 | + extensions["dnssec_return_only_secure"] = getdns.EXTENSION_TRUE |
| 74 | + elif opt == "-4": |
| 75 | + desired_addr_type = "IPv4" |
| 76 | + elif opt == "-6": |
| 77 | + desired_addr_type = "IPv6" |
| 78 | + |
| 79 | +ctx = getdns.UVContext() |
| 80 | +tids = [] |
| 81 | +for hostname in args: |
| 82 | + try: |
| 83 | + tids.append(ctx.address(name=hostname, extensions=extensions, callback='cbk', userarg=hostname)) |
| 84 | + print ('submitted query for {0}'.format(hostname)) |
| 85 | + except getdns.error as e: |
| 86 | + print(str(e)) |
| 87 | + break |
| 88 | +ctx.run() |
0 commit comments