[Tutor] updating Unix config file
msh at blisses.org
Mon Oct 19 18:10:08 CEST 2009
The below script seems to work well enough to use but I'm wondering if I'm doing the file edit stuff in a "kosher" manner. I was just reading the Lutz O'Reilly "Learning" book and remembered that strings are immutable. So how was I able to change the strings for my dotted quad? I did not explicitly open a separate file in /tmp and then overwrite the ipf.conf file which seemed like the safer way. Yet somehow the below syntax works.
Also I need to figure out how to update the LASTKNOWN variable after my IP changes.
Thanks for any advice.
# import fileinput
SENDMAIL = "/usr/sbin/sendmail"
CURRENT = socket.getaddrinfo(socket.gethostname(), None)
LASTKNOWN = '220.127.116.11'
if CURRENT == LASTKNOWN:
cf = open("/etc/ipf.conf", "r")
lns = cf.readlines()
lns = "".join(lns)
# close it so that we can open for writing later
lns = re.sub(LASTKNOWN, CURRENT, lns)
cf = open("/etc/ipf.conf", "w")
subprocess.call('/sbin/ipf -Fa -f /etc/ipf.conf', shell=True)
subprocess.call('/sbin/ipnat -CF -f /etc/ipnat.conf', shell=True)
ptchew = subprocess.Popen("%s -t" % SENDMAIL, "w")
ptchew.write("To: msh at blisses.org\n")
ptchew.write("Subject: YOUR IP ADDRESS HAS CHANGED\n")
ptchew.write("\n") # blank line separating headers from body
ptchew.write("Your IP address has changed to: ")
ptchew = p.close()
The test of a first-rate intelligence is the ability to hold two opposed ideas in the mind at the same time, and still retain the ability to function.
-- F. Scott Fitzgerald
More information about the Tutor