[ale] Need help with a Python Script

Chuck Payne terrorpup at gmail.com
Mon Nov 28 18:09:26 EST 2011


On Fri, Nov 18, 2011 at 9:47 AM, Richard Bronosky <Richard at bronosky.com> wrote:
> Thanks for this. I'm forwarding this to my Dad as he is currently learning
> python also.
>
> On Nov 18, 2011 8:22 AM, "David Tomaschik" <david at systemoverlord.com> wrote:
>>
>> 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
>> _______________________________________________
>> Ale mailing list
>> Ale at ale.org
>> http://mail.ale.org/mailman/listinfo/ale
>> See JOBS, ANNOUNCE and SCHOOLS lists at
>> http://mail.ale.org/mailman/listinfo
>
> _______________________________________________
> Ale mailing list
> Ale at ale.org
> http://mail.ale.org/mailman/listinfo/ale
> See JOBS, ANNOUNCE and SCHOOLS lists at
> http://mail.ale.org/mailman/listinfo
>
>

Richard,

Thanks for iPython, that really help. I have finished my script.
That program really help me showed my mistakes.


=============================
#!/usr/bin/python

import string
import sys
import os
import smtplib

system_name = os.uname() [1]

SERVER = "localhost"

SUBJECT = "Message from  "+system_name
TO = "root at inferno.magidesign.com"
 FROM = "root@"+system_name
text = "System message from "+system_name
BODY = string.join((
"From: %s" % FROM,
"To: %s " % TO,
 "Subject: %s" % SUBJECT,
"",
 text
 ), "\r\n")
server = smtplib.SMTP(SERVER)
server.sendmail(FROM, [TO], BODY)
server.quit()

=============================

Thanks very much

-- 
(678) 636-9678
-----------------------------------------
Discover it! Enjoy it! Share it! openSUSE Linux.
-----------------------------------------
openSUSE -- en.opensuse.org/User:Terrorpup
openSUSE Ambassador/openSUSE Member
skype,twiiter,identica,friendfeed -- terrorpup
freenode(irc) --terrorpup/lupinstein
Register Linux Userid: 155363

Have you tried SUSE Studio? Need to create a Live CD,  an app you want
to package and distribute , or create your own linux distro. Give SUSE
Studio a try. www.susestudio.com.



More information about the Ale mailing list