File: get-srv.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 (30 lines) | stat: -rwxr-xr-x 891 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
#!/usr/bin/env python
#

"""
Lookup an SRV record and printout all the SRV priority, weight, targets, 
and associated IP addresses of the targets.
"""

import getdns, pprint, sys, time

srvname = sys.argv[1]

ctx = getdns.Context()
try:
    results = ctx.service(name=srvname)
except getdns.error as e:
    print(str(e))
    sys.exit(1)

if results.status == getdns.RESPSTATUS_GOOD:
    for reply in results.replies_tree:
        for a in reply["answer"]:
            rrname  = a["name"]
            rrtype  = a["type"]
            if rrtype == getdns.RRTYPE_SRV:
                rdata   = a["rdata"]
                prio, weight, port, target = rdata['priority'], rdata['weight'], rdata['port'], rdata['target']
                print("SRV {0} --> {1} {2} {3} {4}".format(rrname, prio, weight, port, target))
else:
    print("getdns.service() returned an error: {0}".format(results.status))