[ale] Need help with a Python Script

David Tomaschik david at systemoverlord.com
Fri Nov 18 08:21:43 EST 2011


X = "SomeValue","SomeOtherValue" creates a tuple.  It does NOT do string
concatenation.  + will do concatenation, and is fine to use for one-time
concatenation.  (''.join(list) is better for large quantities.)

Rather than os.getenv('HOSTNAME') consider using socket.gethostname().
It doesn't require that your runtime environment have the HOSTNAME
variable set correctly.  (Which it often is not from cron or other
system-level processes.)

Other than that, see Richard's email.

David


On 11/17/2011 09:47 PM, Chuck Payne wrote:
> Guys,
> 
> I need a mentor.  I am trying to teach myself Python. I need to create
> a few script to monitor my servers.  But I am having issue with the
> e-mail part of my script
> 
> What I want it to do it send me alert with the host name in the
> Subject and Body, but I am either getting syntax error or when it does
> work I get None.  How can I pass the hostname?
> 
> 
> Here my script
> 
> =============start script=============
> 
> #!/usr/bin/python
> 
> import os, smtplib
> system_name = os.getenv('HOSTNAME')
> 
> 
> SERVER = localhost
> 
> FROM = "me at abc.com"
> TO = ["you at nbc.com"]
> 
> SUBJECT = "Mail from",os.getenv('HOSTNAME')
SUBJECT = "Mail from "+os.getenv('HOSTNAME')
> 
> TEXT = "This message is from",os.getenv('HOSTNAME')
TEXT = "This message is from ",os.getenv('HOSTNAME')
> 
> message = """\
> From: %s
> To: %s
> Subject: %s
> 
> %s
> """ % (FROM, ", ".join(TO), SUBJECT, TEXT)
> 
> # Send message
> 
> server = smtplib.STMP(SERVER)
> server.sendmail(FROM, TO, message)
> server.quit()
> 
> =============end script=============


-- 
David Tomaschik, RHCE, LPIC-1
System Administrator/Open Source Advocate
OpenPGP: 0x5DEA789B
http://systemoverlord.com
david at systemoverlord.com


More information about the Ale mailing list