Eugene O.
Eugene O.

Reputation: 11

DNS query to specific server using only stock python2.7 libraries

I need to test DNS responses from specific server (i.e. 8.8.8.8 or 4.2.2.2) in an environment that has Python 2.7 and libraries selenium (along with Chrome), requests and pysftp. I can not install any additional packages. I provide script as a text that is then being run by the remote system.

All I need is to query specific DNS server and verify that its response matches expected address.

Any suggestions on how to do it?

Upvotes: 0

Views: 342

Answers (2)

Patrick Mevzek
Patrick Mevzek

Reputation: 12595

With requests you can do HTTP/HTTPS queries.

Newer protocols exist such as DoH which is DNS over HTTPS.

If the server you want to query has such an endpoint, or any equivalent HTTPS endpoints speaking some sorts of DNS, then you can query it.

Fortunately for you, Google Public DNS has such an endpoint, see https://developers.google.com/speed/public-dns/docs/doh/

The JSON version at https://developers.google.com/speed/public-dns/docs/doh/json is easier to work with manually (otherwise you have to encode DNS packets content in base64 which is more work, and same for decoding the reply):

In [1]: import requests

In [2]: r=requests.get('https://dns.google/resolve?name=www.example.com&type=AAAA').json()

In [3]: print r
{u'Status': 0, u'AD': True, u'Question': [{u'type': 28, u'name': u'www.example.com.'}], u'CD': False, u'RD': True, u'RA': True, u'Answer': [{u'data': u'2606:2800:220:1:248:1893:25c8:1946', u'type': 28, u'name': u'www.example.com.', u'TTL': 10740}], u'TC': False}

In [4]: print r['Answer'][0]['data']
2606:2800:220:1:248:1893:25c8:1946

Of course some error checking will be needed. dns.google is 8.8.8.8 of course.

PS: business constraints such as "I can not install any additional packages." are silly and you should fight them. Who would accept to go to a garage and hear the mechanic say "I can not use any tool except a wrench", or going to a hospital to have surgery and hear the surgeon say "I can only use scissors to do a heart transplant". Said differently: one has to be able to use the tools needed to do its job (in your case, the dnspython library)

PS2: do not think that only Google provides free public DNS services; many other organizations do and only bad things can happen when people happen to centralize everything at one place, even if you love Google. You have as well the well-known 1.1.1.1 and 9.9.9.9 and you can find out at https://en.wikipedia.org/wiki/Public_recursive_name_server a list of many public DNS operators offering DoH endpoints.

Upvotes: 3

Eugene O.
Eugene O.

Reputation: 11

Found a dns client that relies on built-in modules only, this does the trick - https://github.com/ValeryTyumen/DNS-Client

Upvotes: 0

Related Questions