cocluto/Util.py

80 lines
2.3 KiB
Python
Raw Normal View History

import time
import subprocess
import StringIO
import re
from wol import *
import os
import signal
import smtplib
from email.MIMEText import MIMEText
def sendTextMail(strFrom, to, strSubject, text ):
#from = "SimpaCluster <guillaume.raffy@univ-rennes1.fr>"
mail = MIMEText(text)
mail['From'] = strFrom
mail['Subject'] = strSubject
mail['To'] = to
smtp = smtplib.SMTP('smtp.univ-rennes1.fr', 25)
#smtp.connect()
#smtp.login('guillaume.raffy@univ-rennes1.fr', 'password')
smtp.sendmail(strFrom, [to], mail.as_string())
smtp.close()
class Error( Exception ):
def __init__( self, strMessage ):
self.m_strMessage = strMessage
def getHostName():
(returnCode, stdout, stderr) = executeProgram(['hostname', '-s'])
if returnCode != 0:
raise Error(stderr)
strHostName = re.sub(r"\n", "", stdout)
return strHostName
def log( message ):
print time.asctime(time.localtime())+' : '+ message
def executeProgram( astrArguments ):
#log('executeProgram : program [%s]' % (','.join(astrArguments)))
popen = subprocess.Popen( astrArguments, bufsize=1, shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE ) # bufsize=1 seems to prevent deadlocks that happen 50% the time
stdout, stderr = popen.communicate()
#popen.wait()
result = (popen.returncode, stdout, stderr)
#log('executeProgram : command %s popen.pid = %d' % (astrArguments[0], popen.pid))
#os.kill(popen.pid, signal.SIGTERM)
return result
def executeCommand( command ):
"""
executes the shell command such as 'set x=1; myprog $x'
"""
popen = subprocess.Popen( [ command ], bufsize=1, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE ) # bufsize=1 seems to prevent deadlocks that happen 50% the time
stdout, stderr = popen.communicate()
#popen.wait()
result = (popen.returncode, stdout, stderr)
return result
def getUpsStatus():
try:
url = 'http://Net Vision:public@129.20.27.119/PageMonComprehensive.html'
f = urllib.urlopen(url)
res = f.read()
f.close()
except:
print "bad read"
return
h = MyHTMLParser()
h.feed(res)
tokensList = h.GetTokenList()
if __name__ == '__main__':
"""
for i in range(30):
machineName = 'simpatix%d' % (i+10)
print 'lom ip of %s is %s' % (machineName, getLightOutManagementIpAddress(machineName))
"""
wakeUp('simpatix21')
#print putToSleep('simpatix13')
#print isNonRespondingMachineSleeping('simpatix13')