# /usr/bin/env python2
# coding: utf-8
"""
Solution from:
<name 1> (<matriculation 1>)
<name 2> (<matriculation 2>)
"""
def ex06_task():
"""
Your task is to use DNSSEC to walk the `example.com.` DNS zone. For each domain encountered, print the third level label.
You do not need to import any modules. You can query any name server with the
`query_nameserver(ip, port, domain, rdtype)`
function defined below. The exact specification for the `rdtype` parameter can found in the dnspython documentation.
A third level label looks like this:
XXX.example.com
A correct solution will be obvious.
"""
# HINT:
# You can extract the relevant part of the responses like this (for any valid i)
# query_nameserver( ... )[i].next.to_text()
# TODO implement
domain = 'example.com.'
while 1:
domain = query_nameserver('139.19.117.15', 53535, domain, 'NSEC')[0].next.to_text()
if domain == 'example.com.':
break
print domain.split('.')[0]
pass
########################################################################################################################
# DO NOT MODIFY ANYTHING BELOW THIS LINE
########################################################################################################################
def query_nameserver(ip, port, domain, rdtype):
"""
Queries the name server at ip:port for a certain domain and resource type.
:param ip: str, IPv4 in dotted notation of the name server
:param port: int, Port of the name server
:param domain: str, The domain you want the resource records from.
:param rdtype: int or str, The type of resource records. Examples: 1, 'A'
:return: List of answers with matching resource record.
"""
import dns.resolver
r = dns.resolver.Resolver()
r.nameservers = [ ip ]
r.port = port
try:
tmp = r.query(domain, rdtype=rdtype, raise_on_no_answer=False)
return [x for x in tmp.rrset] + [x for x in tmp.response.authority]
except (dns.resolver.NXDOMAIN, TypeError) as e:
return []
if __name__ == '__main__':
ex06_task()