I have trawled this site for years and have always found my answer but today I was unsuccessful. I am tasked with writing a python script that can search, add and delete host from a dhcp.conf file. i.e.
host testnode{
option host-name "testnode";
option root-path "0.0.0.0:/foo/bar/foobar/testnode";
option subnet-mask 0.0.0.0;
option routers 0.0.0.0;
hardware ethernet 00:00:00:00:00:00;
fixed-address 0.0.0.0;
}
In the above format. I can search the dhcp.conf file using re.search(str, line) to find testnode, but how do I get me code to print out every line from testnode until the ending "}"?
This is the code I have up to now.
#!/usr/bin/env python
import re
infile = open('/etc/dhcp/dhcpd.conf', 'r')
str = raw_input('Enter hostname to search: ');
def search_host( str ):
for line in infile:
if re.search(str, line):
print line
while (line != '^}'):
line = next(infile)
print line
search_host(str);
re.search will stop at testnode then code prints out every line in the dhcp.conf file until it hits the end. How to tell the while loop to stop once it hits the "}" at the end of the host entry.
Thanks
line != '^}'is looking for a line of text which exactly contains the charactesr^and}. it's not a regex, just a plain string comparison, therefore the regex metachars are NOT metachars, just plain text throwing off your matches.