#!/usr/bin/python import pexpect import time import os SENDMAIL = "/usr/sbin/sendmail" # sendmail location command = { "comando 1" : "rsync -alpurvz user@host:dir/from1 dir/to1", "comando 2" : "rsync -alpurvz user@host:dir/from2 dir/to2", }; chowndir = { "owner1" : "dir1", "owner2" : "dir2", }; def start_mychown(k,v): try: print "eseguo chown su " + v os.system('chown -R apache:apache ' + v) except Exception, e: manda_notifica_errore(k,v,str(e)) def start_sshfs(k,v): try: print "eseguo " + v # mando la notifica di inizio rsync manda_notifica(k,v,"inizio") sshfs = pexpect.spawn(v, [], 10000) sshfs.expect(".*assword: ") time.sleep (0.1) sshfs.sendline ("") time.sleep (10) sshfs.expect (pexpect.EOF) # mando la notifica di fine rsync manda_notifica(k,v,"fine") except Exception, e: manda_notifica_errore(k,v,str(e)) def manda_notifica_errore(subj,rsyn,tipo): p = os.popen("%s -t" % SENDMAIL, "w") p.write("From: from@email.com\n") p.write("To: to@email.com\n") p.write("Subject: ERRORE rsync "+subj+"\n") p.write("\n") # blank line separating headers from body p.write("ERRORE rsync "+subj+" con comando ["+rsyn+"] alle\n") p.write(time.strftime("%d-%m-%Y %H:%M:%S")) p.write("\n") # blank line separating headers from body p.write(tipo) p.write("\n") # blank line separating headers from body sts = p.close() def manda_notifica(subj,rsyn,tipo): p = os.popen("%s -t" % SENDMAIL, "w") p.write("From: from@email.com\n") p.write("To: to@email.com\n") p.write("Subject: rsync "+subj+"\n") p.write("\n") # blank line separating headers from body p.write("rsync "+subj+" con comando ["+rsyn+"] "+tipo+" alle\n") p.write(time.strftime("%d-%m-%Y %H:%M:%S")) p.write("\n") # blank line separating headers from body sts = p.close() def main (command): for k,v in command.items(): start_sshfs(k,v) for k,v in chowndir.items(): start_mychown(k,v) if __name__ == '__main__': main (command)