Here is a problem which is annoying me during these last two hours. I have a template file with multiple lines and in some lines some words have to be changed by some others. Here is how my template looks like:
subnet {{ MY_SUBNET }} netmask {{ MY_NETMASK }} {}
subnet {{ MY_SUBNET }} netmask {{ MY_NETMASK }}
{
option domain-name-servers {{ MY_DOMAIN_IP }};
option domain-name {{ MY_DOMAIN_NAME }};
option routers {{ MY_GATEWAY }};
option broadcast-address {{ MY_BROADCAST }};
Here is the code I am using:
f = open(DHCPD_PATH, 'w')
g = open(TEMPLATE_PATH, 'r')
patterns = {
'{{ MAC_ADDRESS }}' : mac,
'{{ IP_ADDRESS }}' : ip,
'{{ MY_IP }}' : MY_IP,
'{{ MY_DOMAIN_IP }}' : MY_DOMAIN_IP,
'{{ MY_DOMAIN_NAME }}' : MY_DOMAIN_NAME,
'{{ MY_NETMASK }}' : MY_NETMASK,
'{{ MY_GATEWAY }}' : MY_GATEWAY,
'{{ MY_SUBNET }}' : MY_SUBNET,
'{{ MY_BROADCAST }}' : MY_BROADCAST,
}
content = g.read()
for i,j in patterns.iteritems():
content = content.replace(i,j)
f.write(content)
f.close()
g.close()
Here is the file I get:
subnet 192.168.10.0 netmask {{ MY_NETMASK }} {}
subnet 192.168.10.0 netmask 255.255.255.0
{
option domain-name-servers 192.168.10.10;
option domain-name "localnet.lan";
option routers 192.168.10.1;
option broadcast-address 192.168.10.255;
default-lease-time 600;
max-lease-time 7200;
filename "pxelinux.0";
next-server 192.168.10.3;
I can't understand why is this {{ MY_NETMASK }} remaining whereas one of it has been correctly replaced and every others template-patterns get also correctly replaced.
Can anyone give me a hint on this one? Or at least explain me how to correct it?
Many thanks
print i, content.count(i)as the first line within your for loop and see what numbers it returns. Probably some encoding problem with spaces?