approach to writing functions

Bart Nessux bart_nessux at hotmail.com
Tue Feb 10 14:44:56 CET 2004


Paul Prescod wrote:
> If I were you I would post one of these scripts and see if the function 
> and OO fans can make it easier to read or maintain by using structured 
> programming.

OK, here is a script that works w/o OO or functions. It's very useful... 
never fails. It helps sys admins track machines that have mobile users 
who are using DHCP.

#!/usr/bin/python
#
# Tested on Linux Machines
# Run as cron as root
# May work on OS X too
#
# Dec 29, 2003 works on Mac OSX 10.3 like this:
# chown 'root' && chgrp 'wheel' && chmod '755'
# place in /usr/bin
#

from email.MIMEText import MIMEText
import smtplib
import os

u = "User"		#Change This to user's name.
f = "admin at XXX.edu"
t = "admin at XXX.edu"

fp0 = os.popen("/sbin/ifconfig en0 inet", "r")
fp1 = os.popen("/usr/bin/uptime", "r")
fp2 = os.popen("/usr/bin/uname -a", "r")
fp3 = os.popen("/usr/bin/wc -l /etc/passwd", "r")
msg = MIMEText("-- IFCONFIG --\n\n" + fp0.read() + "\n-- UPTIME --\n\n" 
+  fp1.read() + "\n-- UNAME --\n\n" + fp2.read() + "\n-- PASSWD LC 
--\n\n" + fp3.read())
fp0.close()
fp1.close()
fp2.close()
fp3.close()

msg["Subject"] = "%s's ifconfig Report" % u
msg["From"] = f
msg["To"] = t

h = "smtp.vt.edu"
s = smtplib.SMTP(h)
s.sendmail(f, t, msg.as_string())
s.quit()




More information about the Python-list mailing list