Python System Administration

Quinn Dunkan quinn at retch.ugcs.caltech.edu
Tue Feb 5 01:28:30 EST 2002


On Sat, 02 Feb 2002 18:01:12 GMT, Andrew Replogle <replogle2 at cox.net> wrote:
>
>"Andrew Replogle" <replogle2 at cox.net> wrote in message
>news:LwK68.7172$Xk4.381742 at news1.east.cox.net...
>>    Anyone know of any books out there for python system administration or
>> should I just stick to perl for these tasks?
>>
>>
>Well, for example, I'd like to write a python script to tail a ftp log file
>& when a connection from a customer is made and the file transmits
>succesfully to email them a confirmation. I am usually very efficient if I
>have just a small example or concept of how to do this and just wanted to
>know if anyone had any suggestions on such a book. Thats all. :)

Well, dunno about any books, but here's a quick-n-dirty something for the
above:

tail -f ftp.log | grep 'connect from customer' | mail_line.py >errlog

mail_line.py:

#!/usr/local/bin/python
import sys, re
def main():
    s = sys.stdin.readline()
    customer = re.search(r'extract (customer)', s).group(1)
    customer_email = lookup_in_some_database(customer)
    p = os.popen('/usr/lib/sendmail %s' % customer_email, 'w')
    p.write(congratulatory_message)
    p.close()

while 1:
    try:
        main()
    except:
        import traceback
        traceback.print_exc()


You could probably do it all in shell with some 'sed' and 'read' hackery, but
anything more complicated than a pipeline in shell is what I use python for :)

Umm, stdio buffering is probably going to screw up the above.  Dumb substdio.


Sysadmin stuff in python is much like in C, since python simply wraps a lot of
libc calls, e.g. getpwnam(), os.system(), etc.  Of course you don't have to
worry about memory management and all the other C details.  Check out
the docs for the os and posix modules.



More information about the Python-list mailing list