title: using dig with python to list all host
body: <p>I'm very newbie with python and programming and I just can't find the way to do the code I need. So help, please :)</p>
  
  <p>Background:</p>
  
  <p>I'll be checking multiple different (linux) environments so I want to get the nameserver (ns) and domain information from resolv.conf and pass these values to dig. </p>
  
  <p>Thanks to this forum and google I was able to get the ns and domain info but struggling still how to pass the info to dig.</p>
  
  <p>Python version is 2.7.
  Linux is rhel6.x.</p>
  
  <p>Problem:</p>
  
  <p>How to format the dig command using return values for ns and domain and giving few parameters: </p>
  
  <pre><code>dig +search &lt;domain&gt;. axfr @&lt;ns&gt; +noall +answer 
  </code></pre>
  
  <p>The  and  are coming from the functions.</p>
  
  <p>What I want:</p>
  
  <ol>
  <li><p>read the ns and domain from /et/resolv.conf. Only first ns is needed 
  (this has been achieved)</p></li>
  <li><p>pass the ns and domain to dig (this is the part I just can't figure out). dig command is this: dig +search . axfr @ +noall +answer </p></li>
  <li><p>modify the output of dig so that the list would like this:
  host_ip1  host_name1
  host_ip2  host_name2
  ....</p></li>
  <li><p>would be nice if the output was sorted based on IP</p></li>
  </ol>
  
  <p>that's about it. 
  I know there are dns modules available to do the same, but they are no option for me since they are not available. </p>
  
  <p>So far this is what I have:</p>
  
  <pre><code># -*- coding: utf-8 -*-
  
  ### Functions start ###
  
  def get_ns():
      with open("/etc/resolv.conf","r") as readFile:
          #print [line.split(" ")[1].strip() for line in readFile.readlines() if line.startswith("nameserver")][0]
          x = [line.split(" ")[1].strip() for line in readFile.readlines() if line.startswith("nameserver")][0]
      readFile.close()
      return x
  
  
  def get_domain():
      with open("/etc/resolv.conf","r") as readFile:
          #print [line.split(" ")[1].strip() for line in readFile.readlines() if line.startswith("search")][0]
          y = [line.split(" ")[1].strip() for line in readFile.readlines() if line.startswith("search")][0]
      readFile.close()
      return y
  
  ### Functions end ###
  
  # just to test all is working:
  ns = get_ns()
  
  # to get the dig command:
  domain = get_domain()
  domain1 = domain + '.'
  ns1 = '@' + ns
  print ns1
  print domain1
  
  list = os.system("dig +search " + domain1 + " axfr " + ns1 + " +noall +answer")
  print list
  </code></pre>
  
  <p>Any help will be highly appreciated!</p>