471,119 Members | 1,326 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,119 software developers and data experts.

replacing text inplace

Hey All.

I'm learning some python with the seemingly simple task of updating a firewall config file with the new IP address when my dhcpd server hands one out. Yeah, I know it's dangerous to edit such a file "in place" but this is just an exercise at this point. I would not mind using file handles except they seem to add complexity.

The only apparent problem I have with my script so far is that it's adding lots of blank lines to the file when it updates the IP addresses.

There is only one dotted quad that needs changing in a file full of dotted quads and the dhcp lease lasts for months usually so I have no qualms about putting the old IP address in the file, i.e. I won't need to manually update it very often. The address apears on lines that looks like this:

block in from 71.146.250.258/32 to any group 150

So "71.146.250.258" needs to change to "71.146.250.223" or something similar.

Is there a saner, cleaner way to do this that won't add new, blank lines?
Most of the examples on the Intertubes are about multiple files and STDIN.
This is just one file and it needs one string changed on two or three lines.
The script runs from cron, not interactively.

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

import sys
import string
import os
import fileinput
import re

oldip = "71.146.250.258"

ifcfg_lines = os.popen("/sbin/ifconfig eth0").readlines()
newip = string.split(ifcfg_lines[1])[1][-11:]

if newip == oldip:

print "nevermind"

else:

for line in fileinput.FileInput("/etc/ipf.conf",inplace=1):
line = line.replace(oldip,newip)
print line

--
"Outside of a dog, a book is a man's best friend. Inside of a dog, it is too dark to read."
-- Groucho Marx

Jun 27 '08 #1
1 1458
On 2008-04-20, Matt Herzog <ms*@blisses.orgwrote:
I'm learning some python with the seemingly simple task of
updating a firewall config file with the new IP address when
my dhcpd server hands one out. Yeah, I know it's dangerous to
edit such a file "in place"
I don't see how what you're doing is editing a file "in place".
but this is just an exercise at this point. I would not mind
using file handles except they seem to add complexity.
Do you mean file objects?
The only apparent problem I have with my script so far is that
it's adding lots of blank lines to the file when it updates
the IP addresses.
When you do this:

for line in inputfile:

Each instance of 'line' has a newline at the end of it.

When you do this:

print line

The print operation adds another newline. Try it this way:

print line,

The comma tells print not to append a newline after it has
printed "line".
So "71.146.250.258" needs to change to "71.146.250.223" or something similar.

Is there a saner, cleaner way to do this that won't add new,
blank lines?
sed -i s/71.146.250.258/71.146.250.223/g filename

I suppose one probably should escape the dots in the input
regex...

--
Grant Edwards grante Yow! With YOU, I can be
at MYSELF ... We don't NEED
visi.com Dan Rather ...
Jun 27 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Anna Hutter | last post: by
reply views Thread by Roger | last post: by
reply views Thread by Gabriel Genellina | last post: by
reply views Thread by mvsmsh | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.