File: get-general.py

package info (click to toggle)
python-getdns 1.0.0~b1-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 448 kB
  • sloc: ansic: 3,538; python: 607; makefile: 130
file content (53 lines) | stat: -rwxr-xr-x 1,494 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/usr/bin/env python
#
# Given a DNS name and type, return the records in the DNS answer
# section only, excluding any RRSIG records.
#

import getdns, pprint, sys

extensions = { "dnssec_return_status" : getdns.EXTENSION_TRUE }

def get_rrtype(qtype):
    try:
        rrtype = eval("getdns.RRTYPE_%s" % qtype.upper())
    except AttributeError:
        print("Unknown DNS record type: {0}".format(qtype))
        sys.exit(1)
    else:
        return rrtype


def print_answer(r):
    pprint.pprint(r.replies_tree[0]['answer'])
    return


if __name__ == '__main__':

    qname, qtype = sys.argv[1:]
    rrtype = get_rrtype(qtype)

    ctx = getdns.Context()
    try:
        results = ctx.general(name=qname, request_type=rrtype,
                              extensions=extensions)
    except getdns.error as e:
        print(str(e))
        sys.exit(1)

    status = results.status

    if status == getdns.RESPSTATUS_GOOD:
        for reply in results.replies_tree:
            answers = reply['answer']           # list of 1 here
            for answer in answers:
                if answer['type'] != getdns.RRTYPE_RRSIG:
                    pprint.pprint(answer)
    elif status == getdns.RESPSTATUS_NO_NAME:
        print("{0}, {1}: no such name".format(qname, qtype))
    elif status == getdns.RESPSTATUS_ALL_TIMEOUT:
        print("{0}, {1}: query timed out".format(qname, qtype))
    else:
        print("{0}, {1}: unknown return code: {2}".format(qname, qtype, results.status))