Writing Network Tools with Python (Part 1: DNS Resolution)

In this article, we'll be taking a look at a few ways in which Python can make your life easier. All of the examples in this part will pertain to DNS.

In this article, we'll be taking a look at a few ways in which Python can make your life easier.  All of the examples in this part will pertain to DNS.

Environment:
  • Python 2.7
  • dnspython==1.16.0

Resolve A Records (FQDN or Host to IPv4 Address)

from dns import resolver

results = resolver.query('0.pool.ntp.org', 'A')
for result in results:
    print(result)

103.105.51.156
138.197.114.122
45.33.103.94
71.255.99.178

Resolve AAAA Records (FQDN or Host to IPv6 Address)

from dns import resolver

results = resolver.query('cnn.com', 'AAAA')
for result in results:
    print(result)
2a04:4e42::323
2a04:4e42:200::323
2a04:4e42:400::323
2a04:4e42:600::323

Resolve PTR Records (Reverse Lookup; IPv4 to FQDN)

from dns import reversename, resolver

results = resolver.query(reversename.from_address('10.1.1.100'), 'PTR')
for result in results:
    print(result)
gitlab.example.com.

Resolve MX Records (Mail Servers for Domain)

from dns import resolver

results = resolver.query('gmail.com', 'MX')
for result in results:
    print(result)
30 alt3.gmail-smtp-in.l.google.com.
40 alt4.gmail-smtp-in.l.google.com.
5 gmail-smtp-in.l.google.com.
10 alt1.gmail-smtp-in.l.google.com.
20 alt2.gmail-smtp-in.l.google.com.

Check if Domain is a Super or Sub Domain

from dns import name

print(name.from_text('www.tyler-wright.com').is_subdomain(name.from_text('tyler-wright.com')))
True
from dns import name

print(name.from_text('www.tyler-wright.com').is_superdomain(name.from_text('tyler-wright.com')))
False

Related Articles


Share Tweet Send
0 Comments
Loading...
You've successfully subscribed to Tyler Wright
Great! Next, complete checkout for full access to Tyler Wright
Welcome back! You've successfully signed in
Success! Your account is fully activated, you now have access to all content.