0

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 :)

Background:

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.

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.

Python version is 2.7. Linux is rhel6.x.

Problem:

How to format the dig command using return values for ns and domain and giving few parameters:

dig +search <domain>. axfr @<ns> +noall +answer 

The and are coming from the functions.

What I want:

  1. read the ns and domain from /et/resolv.conf. Only first ns is needed (this has been achieved)

  2. 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

  3. modify the output of dig so that the list would like this: host_ip1 host_name1 host_ip2 host_name2 ....

  4. would be nice if the output was sorted based on IP

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.

So far this is what I have:

# -*- 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

Any help will be highly appreciated!

sami
  • 1
  • 3

0 Answers0