2011-04-18 11:32:19 +02:00
import time
import subprocess
2018-04-12 16:27:41 +02:00
# import StringIO
2011-04-18 11:32:19 +02:00
import re
2018-04-12 16:27:41 +02:00
# from wol import *
# import os
# import signal
2011-04-18 11:32:19 +02:00
import smtplib
2019-07-10 10:34:42 +02:00
from email . mime . text import MIMEText
from html . parser import HTMLParser
2011-04-18 11:32:19 +02:00
2018-04-12 16:27:41 +02:00
def sendTextMail ( strFrom , to , strSubject , text ) :
# from = "SimpaCluster <guillaume.raffy@univ-rennes1.fr>"
2011-04-18 11:32:19 +02:00
mail = MIMEText ( text )
mail [ ' From ' ] = strFrom
mail [ ' Subject ' ] = strSubject
mail [ ' To ' ] = to
smtp = smtplib . SMTP ( ' smtp.univ-rennes1.fr ' , 25 )
2018-04-12 16:27:41 +02:00
# smtp.connect()
# smtp.login('guillaume.raffy@univ-rennes1.fr', 'password')
2011-04-18 11:32:19 +02:00
smtp . sendmail ( strFrom , [ to ] , mail . as_string ( ) )
smtp . close ( )
2018-04-12 16:27:41 +02:00
class Error ( Exception ) :
def __init__ ( self , strMessage ) :
self . m_strMessage = strMessage
2012-02-07 15:14:48 +01:00
def getHostName ( ) :
2018-04-12 16:27:41 +02:00
( returnCode , stdout , stderr ) = executeProgram ( [ ' hostname ' , ' -s ' ] )
if returnCode != 0 :
raise Error ( stderr )
strHostName = re . sub ( r " \ n " , " " , stdout )
return strHostName
def log ( message ) :
2019-07-10 10:34:42 +02:00
print ( time . asctime ( time . localtime ( ) ) + ' : ' + message )
2018-04-12 16:27:41 +02:00
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()
2019-07-10 10:34:42 +02:00
result = ( popen . returncode , stdout . decode ( ) , stderr )
2018-04-12 16:27:41 +02:00
# 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 , executable = ' /bin/bash ' ) # bufsize=1 seems to prevent deadlocks that happen 50% the time
# if we don't specify the optional executable argument, then the default non interactive shell will be used. On debian, the default non-interactive shell is dash, which doesn't understand the keyword 'source' that we use in many places
stdout , stderr = popen . communicate ( )
# popen.wait()
2019-07-10 10:34:42 +02:00
result = ( popen . returncode , stdout . decode ( ) , stderr . decode ( ) )
2018-04-12 16:27:41 +02:00
return result
2018-09-25 15:48:47 +02:00
def executeCommandOn ( target_machine_fqdn , command , user = None ) :
2018-04-12 16:27:41 +02:00
"""
execute command on a local or remote machine ( using ssh then )
2018-09-25 15:48:47 +02:00
: param str user : if not None , the user that should be used to execute the command ( instead of the current user )
2018-04-12 16:27:41 +02:00
"""
2018-04-16 11:53:40 +02:00
print ( " comparing %s and %s " % ( getHostName ( ) , target_machine_fqdn . split ( ' . ' ) [ 0 ] ) )
if getHostName ( ) == target_machine_fqdn . split ( ' . ' ) [ 0 ] :
2018-09-25 15:48:47 +02:00
if user is not None :
# su -c "ls -l /tmp" graffy
return executeCommand ( " su -c ' %s ' %s " % ( command , user ) )
else :
return executeCommand ( command )
2018-04-12 16:27:41 +02:00
else :
2018-09-25 15:48:47 +02:00
if user is not None :
target = ' %s @ %s ' % ( user , target_machine_fqdn )
2018-06-20 10:04:29 +02:00
else :
target = target_machine_fqdn
2018-09-25 15:48:47 +02:00
return executeProgram ( [ ' ssh ' , target , " %s " % command ] )
2011-04-18 11:32:19 +02:00
def getUpsStatus ( ) :
2018-04-12 16:27:41 +02:00
class MyHTMLParser ( HTMLParser ) :
def __init__ ( self ) :
HTMLParser . __init__ ( self )
self . TokenList = [ ]
def handle_data ( self , data ) :
data = data . strip ( )
if data and len ( data ) > 0 :
self . TokenList . append ( data )
# print data
def GetTokenList ( self ) :
return self . TokenList
2019-07-10 10:34:42 +02:00
from urllib . request import urlopen
2018-04-12 16:27:41 +02:00
try :
url = ' http://Net Vision:public@129.20.27.119/PageMonComprehensive.html '
2019-07-10 10:34:42 +02:00
f = urlopen ( url )
2018-04-12 16:27:41 +02:00
res = f . read ( )
f . close ( )
except :
2019-07-10 10:34:42 +02:00
print ( " bad read " )
2018-04-12 16:27:41 +02:00
return
h = MyHTMLParser ( )
h . feed ( res )
tokensList = h . GetTokenList ( ) # @UnusedVariable
2011-04-18 11:32:19 +02:00
if __name__ == ' __main__ ' :
2018-04-12 16:27:41 +02:00
from SimpaDbUtil import wakeUp
"""
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')