Python 3 regex woes (parsing ISC DHCPD config)

Jason Bailey jbailey at emerytelcom.com
Mon Jan 12 13:20:35 EST 2015


Hi all,

I'm working on a Python _3_ project that will be used to parse ISC DHCPD 
configuration files for statistics and alarming purposes (IP address 
pools, etc). Anyway, I'm hung up on this one section and was hoping 
someone could provide me with some insight.

My script first reads the DHCPD configuration file into memory - 
variable "filebody". It then utilizes the re module to find the 
configuration details for the wanted "shared network".

The config file might look something like this:

######################################

shared-network My-Network-MOHE {
   subnet 192.168.0.0 netmask 255.255.248.0 {
     option routers 192.168.0.1;
     option tftp-server-name "192.168.90.12";
     pool {
       deny dynamic bootp clients;
       range 192.168.0.20 192.168.7.254;
     }
   }
}

shared-network My-Network-CDCO {
   subnet 192.168.8.0 netmask 255.255.248.0 {
     option routers 10.101.8.1;
     option tftp-server-name "192.168.90.12";
     pool {
       deny dynamic bootp clients;
       range 192.168.8.20 192.168.15.254;
     }
   }
}

shared-network My-Network-FECO {
   subnet 192.168.16.0 netmask 255.255.248.0 {
     option routers 192.168.16.1;
     option tftp-server-name "192.168.90.12";
     pool {
       deny dynamic bootp clients;
       range 192.168.16.20 192.168.23.254;
     }
   }
}

######################################

Suppose I'm trying to grab the shared network called "My-Network-FECO" 
from the above config file stored in the variable 'filebody'.

First I have my variable 'shared_network' which contains the string 
"My-Network-FECO".

I compile my regex:
m = re.compile(r"^(shared\-network (" + re.escape(shared_network) + r") 
\{((\n|.|\r\n)*?)(^\}))", re.MULTILINE|re.UNICODE)

I search for regex matches in my config file:
m.search(filebody)

Unfortunately, I get no matches. From output on the command line, I can 
see that Python is adding extra backslashes to my re.compile string. I 
have added the raw 'r' in front of the strings to prevent it, but to no 
avail.

Thoughts on this?

Thanks





More information about the Python-list mailing list