Python - DNS lookup from SQL database
<p>I was hoping you could point me in the right direction about a little problem I'm having.</p>

<p>I want to create a basic web app that performs DNS queries to validate whether a specific set of DNS records are active or not. The DNS records are all A records and are hosted on an SQL-based CMDB. </p>

<p>I'm a fan of python and would prefer to use it but I have no idea where to start. The use case I'm looking at is as follows:</p>

<blockquote>
  <ul>
  <li>Browse to <a href="http://hostname/pythonDnsTool.html" rel="nofollow">http://hostname/pythonDnsTool.html</a></li>
  <li>Click [RUN QUERY] button</li>
  <li>Script queries the <strong>[DnsARecord]</strong> table to validate whether a list of dnsNames exist or not</li>
  <li>Upon completion <a href="http://hostname/pythonDnsTool.html" rel="nofollow">http://hostname/pythonDnsTool.html</a> returns a list of queried domains with a YES or NO</li>
  </ul>
</blockquote>

<p>Any advice or guidance would be greatly appreciated.</p>

<p>Many thanks,</p>

<p>Westie5017</p>

<p>There are a few tools, which one is the best depends on your needs.</p>

<p><strong>1]</strong> A great python tool that does what you ask and a lot more is <strong>scapy</strong>. 
For example:</p>

<pre><code>&gt;&gt;&gt; packet = DNSQR()
&gt;&gt;&gt; packet.show()
###[ DNS Question Record ]###
  qname= '.'
  qtype= A
  qclass= IN
</code></pre>

<p>As you can see it gives you the ability to craft your packets in high level.
For example if you wanted to send a simple DNS query and get an answer back you could do something like that:</p>

<pre><code>sr1(IP(dst="some_ip")/UDP()/DNS(rd=1,qd=DNSQR(qname="")))
</code></pre>

<p>But again this is only a minimal example of the things you can do. 
For more information take a look at:
<a href="http://www.secdev.org/projects/scapy/doc/usage.html" rel="nofollow">here</a> and <a href="http://www.secdev.org/projects/scapy/demo.html" rel="nofollow">here</a> (scapy's general documentation)</p>

<p>and specifically for <strong>DNS</strong> take a look at <a href="https://thepacketgeek.com/scapy-p-09-scapy-and-dns/" rel="nofollow">here</a> where you can find a lot of examples.</p>

<p><strong>2]</strong> Alternatively you can use the built-in <code>socket</code> module:</p>

<pre><code>import socket
info = socket.getaddrinfo('a_domain', a_port)
socket.gethostbyname('a_domain') # but this will give only the ip
</code></pre>

<p><strong>3]</strong> Finally you can use <a href="http://www.dnspython.org/examples.html" rel="nofollow">dnspython</a>.</p>

python sql-server dns