Skip to content

Instantly share code, notes, and snippets.

@tuxmartin
Last active May 18, 2017 11:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tuxmartin/e4f0cfbc97ef056b30b7b0f65b6d3785 to your computer and use it in GitHub Desktop.
Save tuxmartin/e4f0cfbc97ef056b30b7b0f65b6d3785 to your computer and use it in GitHub Desktop.
dnsquery
try:
import socket
import dns.resolver
import argparse
parser = argparse.ArgumentParser(description='dns query')
parser.add_argument('--server', action="store", type=str, required=False, dest='server', help='DNS server (default 8.8.8.8)', default='8.8.8.8')
parser.add_argument('--record', action="store", type=str, required=True, help='record type to find', dest='record')
parser.add_argument('--find', action="store", type=str, required=True, dest='find', help='')
results = parser.parse_args()
# Set the DNS Server
resolver = dns.resolver.Resolver()
resolver.nameservers=[socket.gethostbyname(results.server)]
if (results.record == "MX"):
answers = dns.resolver.query(results.find, 'MX')
for rdata in answers:
print 'Host', rdata.exchange, 'has preference', rdata.preference
if (results.record == "CNAME"):
for rdata in dns.resolver.query(results.find, 'CNAME'):
print rdata.target
if (results.record == "A"):
for rdata in dns.resolver.query(results.find, 'A'):
print rdata
if (results.record == "AAAA"):
for rdata in dns.resolver.query(results.find, 'AAAA'):
print rdata
if (results.record == "PTR"):
n = dns.reversename.from_address(results.find)
print n
if (results.record == "TXT"):
for rdata in dns.resolver.query(results.find, 'TXT'):
print rdata
exit(0)
except Exception as e:
exit(1)
apt-get install build-essential
apt-get install python-dnspython
cython --embed dnsquery.py
gcc dnsquery.c -fPIC -O3 -I/usr/include/python2.7 -lpython2.7 -o dnsquery
strip dnsquery

martin@martin ~/PycharmProjects/test $ du -sh dnsquery
36K	dnsquery
martin@martin ~/PycharmProjects/test $ 
martin@martin ~/PycharmProjects/test $ ldd dnsquery
	linux-vdso.so.1 =>  (0x00007ffd863fe000)
	libpython2.7.so.1.0 => /usr/lib/x86_64-linux-gnu/libpython2.7.so.1.0 (0x00007fb9d09c8000)
	libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fb9d05ff000)
	libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007fb9d03e1000)
	libz.so.1 => /lib/x86_64-linux-gnu/libz.so.1 (0x00007fb9d01c7000)
	libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007fb9cffc3000)
	libutil.so.1 => /lib/x86_64-linux-gnu/libutil.so.1 (0x00007fb9cfdbf000)
	libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007fb9cfab6000)
	/lib64/ld-linux-x86-64.so.2 (0x00005609447e9000)
martin@martin ~/PycharmProjects/test $ 
martin@martin ~/PycharmProjects/test $ ./dnsquery -h
usage: dnsquery [-h] [--server SERVER] --record RECORD --find FIND

dns query

optional arguments:
  -h, --help       show this help message and exit
  --server SERVER  DNS server (default 8.8.8.8)
  --record RECORD  record type to find
  --find FIND
martin@martin ~/PycharmProjects/test $ ./dnsquery --record A --find mail.vancl.eu
martin@martin ~/PycharmProjects/test $ echo $?
1
martin@martin ~/PycharmProjects/test $ ./dnsquery --record CNAME --find mail.vancl.eu
ghs.google.com.
martin@martin ~/PycharmProjects/test $ echo $?
0
martin@martin ~/PycharmProjects/test $ ./dnsquery --record PTR --find 10.123.1.147
147.1.123.10.in-addr.arpa.
martin@martin ~/PycharmProjects/test $ echo $?
0
martin@martin ~/PycharmProjects/test $ ./dnsquery --record AAAA --find google.cz
2a00:1450:4014:80d::2003
martin@martin ~/PycharmProjects/test $ ./dnsquery --record A --find google.cz
172.217.23.227
martin@martin ~/PycharmProjects/test $ ./dnsquery --record TXT --find google.cz
"v=spf1 -all"
martin@martin ~/PycharmProjects/test $
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment