1

I'm trying to use DNS Python to lookup the SOA for a FQDN. From there, I am trying to isolate and extract the dns server name for the SOA, but I'm unable to find the correct object.

This is where I am at in my troubleshooting. I feel like i cam getting close with answer.response.additional, but it is still not the standalone server name.

Any advice on this? Thanks!

Code:

        import dns.resolver

        FQDN = 'testhostname.awesomedomain.com'

        answer = dns.resolver.query(FQDN, 'SOA', raise_on_no_answer=False)
        
        if answer.rrset is None:
            print(answer.response.authority[0].to_text())
            soa_data = answer.response.authority[0].to_text()
        else:
            print(answer)
            soa_data = answer
        
        rr = answer.response.authority[0]
        print(f"RR is {rr}")
        print(f"DataType is {rr.rdtype}")
        print(f"DNS SOA Type is {dns.rdatatype.SOA}")
        print(f"Target 1 is {rr.target}")
        print(f"Target 2 is {answer.canonical_name}")
        print(f"Target 3 is {answer.qname}")
        print(f"Target 4 is {answer.rdclass}")
        print(f"Target 5 is {answer.response}")
        print(f"Target 6 is {answer.response.additional}")
        print(f"Target 7 is {answer.response.additional.name}")

nuprap
  • 385
  • 5
  • 22
  • What do you mean by "lookup the SOA for a FQDN"? A SOA record is a required part of every DNS zone. Zones more or less correspond with *domain* names, not *host* names (FQDN is a hostname). – VPfB Jan 19 '21 at 15:22
  • I want to scrape the DNS Server that is hosting the SOA record for a domain/zone from a FQDN. – nuprap Jan 19 '21 at 20:02
  • What is the specific library that you're importing with `import dns.resolver`? – ChrisGPT was on strike Jan 20 '21 at 21:06

1 Answers1

0

Here is the method how would an admin get the information from the command line. Hope it helps, but you have to make the script yourself.

Step 1. Query your DNS server (or any recursive DNS server that you are allowed to use) for the A (IPv4, default) or AAAA (IPv6) record for the given hostname. In the following example I used the www.ibm.com.

CLI command: dig www.ibm.com.

The answer (with some lines deleted for brevity)

;; QUESTION SECTION:
;www.ibm.com.                   IN      A

;; ANSWER SECTION:
www.ibm.com.            3599    IN      CNAME   www.ibm.com.cs186.net.
www.ibm.com.cs186.net.  43199   IN      CNAME   outer-ccdn-dual.ibmcom.edgekey.net.
outer-ccdn-dual.ibmcom.edgekey.net. 21600 IN CNAME outer-ccdn-dual.ibmcom.edgekey.net.globalredir.akadns.net.
outer-ccdn-dual.ibmcom.edgekey.net.globalredir.akadns.net. 900 IN CNAME e2874.dscx.akamaiedge.net.
e2874.dscx.akamaiedge.net. 20   IN      A       92.123.17.145

;; AUTHORITY SECTION:
dscx.akamaiedge.net.    4000    IN      NS      n2dscx.akamaiedge.net.
dscx.akamaiedge.net.    4000    IN      NS      n6dscx.akamaiedge.net.

;; ADDITIONAL SECTION:
n2dscx.akamaiedge.net.  4000    IN      A       23.212.110.6
n6dscx.akamaiedge.net.  4000    IN      A       2.23.97.125

We see, the name is an alias (CNAME) of an alias etc. and it resolves to the e2874.dscx.akamaiedge.net. FQDN. We don't care about the IP address this time. We are interested in the AUTHORITY SECTION. There is the zone/domain where that FQDN belongs: dscx.akamaiedge.net..

Step 2: Now ask for the SOA record for the domain from step 1.

CLI command: dig -t soa dscx.akamaiedge.net.

;; QUESTION SECTION:
;dscx.akamaiedge.net.           IN      SOA

;; ANSWER SECTION:
dscx.akamaiedge.net.    1000    IN      SOA     n0dscx.akamaiedge.net. hostmaster.akamai.com. 1611089512 1000 1000 1000 1800

And there is your SOA.

Note that the retrieved SOA happens to differ from the SOA for the ibm.com. domain, because the name is aliased. I hope I did correctly understand your comment about what SOA you are looking for.

VPfB
  • 14,927
  • 6
  • 41
  • 75
  • Thanks. This is the procedure that I'm hoping to follow, but I'm hoping to get the result using Python3. – nuprap Jan 19 '21 at 22:49