parsing a dns configuration file with python
Question: Not sure this question has it's place, but i'll give it a shot anyway.
Basically what I need is to parse a conf file.

It would look like this :

    
    
     1. #local-data: "some.dns.url IN MX 192.168.80.45"
     2. local-data: "some.other.dns.url IN A 192.168.60.1"
     3. local-data: "router.home.somewhere IN A 192.168.20.1"
     4. *a linebreak*
     5. local-data-ptr: "192.168.80.45 some.dns.url"
     6. local-data-ptr: "192.168.60.1 some.other.dns.url"
     7. # some other random comment
     8. local-data-ptr: "192.168.20.1 router.home.somewhere"
    

What i'd need to get off of it is : The address : "some.other.dns.url" on the
**2** nd line what is after the "IN" : that can be MX, A, some others. The IP
: in that case 192.168.60.1, but that could be anything else.

I don't necessarily need the complete script. Hell I don't even really need
one, that's not my goal posting here. I would like to know the best approach
for such a case. Most of the time I tend to get out of situations like that,
but this time this seems a bit tricky as :

  * I can't base myself on the size of the line.

  * I can't base myself on "dots" `(.)` for the address, as one could have 2 dots, and another one have 3.

  * I have part of the line (`"IN"`) that is just disturbing and not of some use.

What i've done so far :

    
    
    dnsconf = open('theconffile.conf', 'r')
    dnsconf = dnsconf.readlines()
    x = []
        for line in dnsconf:
            cont = re.findall('\"(.+?)\"', line)
            if len(line) > 1 and line[:1] is not '#':
               x.append(cont)
               print cont
    

`cont` basically contains what is inside the double quotes. "some.dns.url IN
MX 192.168.80.45" for exemple.

I feel like this is a good start but I fail to see how to continue from there.
I am not good with regexes and I feel like what I need are actually
regexes...so i'm a little bit stuck here.

Can anyone put me on the right track for this problem?


Answer: This ignores the lines beginning with #, decode lines beginning 'local-data:',
and parses out the hostname, DNS type and IP address to a list:

    
    
    import re
    
    dnsconf = open('dns.txt', 'rt')
    
    x = []
    for line in dnsconf:
        cont = re.findall('^local-data:\s+\"(\S+)\s+IN\s+(\S+)\s+(\S+)\"', line)
        if cont:
            x.append(cont)
            print cont
    

This allows spaces to be repeated within the strings by using \s+.

Output for your data is:

    
    
    [('some.other.dns.url', 'A', '192.168.60.1')]
    [('router.home.somewhere', 'A', '192.168.20.1')]
    

Note unless you have a reason to need all the text in memory there is no need
to use readlines(). Also for Windows-robustness, using file open mode "rt"
doesn't do any harm on unixy systems.

