[Tutor] updating Unix config file

Matt Herzog msh at blisses.org
Mon Oct 19 18:10:08 CEST 2009

Hi All.

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 socket
# import fileinput
import subprocess
import string
import re

SENDMAIL = "/usr/sbin/sendmail"
CURRENT = socket.getaddrinfo(socket.gethostname(), None)[0][4][0]

    print 'Nevermind.'

    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 mailing list