|
无聊给外包公司写了个邮件接口,外包人员可通过邮件对本公司内网服务器进行一些检查及操作,保留了三个较普遍的需求.
各位不要见笑
- #!/usr/bin/python
- import sys
- import email
- import pxssh
- import string
- import poplib
- import smtplib
- import threading
- import subprocess
- SERVER = 'demo'
- USER = 'demo'
- PASSWD = 'demo'
- MAX_THREAD = 100
- sem=threading.BoundedSemaphore(MAX_THREAD)
- class GetMail(object):
- def __init__(self, popServer, user, passwd):
- self.popServer = popServer
- self.user = user
- self.passwd = passwd
- def doit(self):
- result = { 'Msgs':0 }
- popServer = poplib.POP3_SSL(self.popServer)
- popServer.user(self.user)
- popServer.pass_(self.passwd)
- result['Msgs'] = popServer.stat()[0]
- if result['Msgs'] == 0:
- return result
- sys.exit()
- message = popServer.retr(result['Msgs'])[1]
- mail = email.message_from_string(string.join(message,'\n'))
- result['Subject'] = email.Header.decode_header(mail['subject'])[0][0]
- result['From'] = email.Header.decode_header(mail['from'])[0][0]
- result['To'] = email.Header.decode_header(mail['To'])[0][0]
- result['Body'] = mail.get_payload(decode=True)
- popServer.dele(result['Msgs'])
- popServer.quit()
- return result
- class SendMail:
- def __init__(self, smtpServer, user, passwd):
- self.smtpServer = smtpServer
- self.user = user
- self.passwd = passwd
- def doit(self, fromAddr, toAddr, subject, msg):
- smtpServer = smtplib.SMTP(self.smtpServer)
- smtpServer.ehlo()
- smtpServer.starttls()
- smtpServer.ehlo()
- smtpServer.login(self.user,self.passwd)
- body=string.join((
- "FROM: %s" %fromAddr,
- "TO: %s" %toAddr,
- "Subject: %s" %subject,
- "",
- msg),"\n")
- smtpServer.sendmail(fromAddr,toAddr,body)
- smtpServer.quit()
- def iLOCheck(ip,l):
- '''Check ilo connectivity'''
- ILOPING = '/home/wangruoyan/ping.sh '
- ret = subprocess.call("%s %s" % (ILOPING, ip),shell=True,stdout=open('/dev/null', 'w'),stderr=subprocess.STDOUT)
- if ret == 0:
- truth[ip] = 'Alive'
- elif ret == 1:
- truth[ip] = 'Dead'
- else:
- truth[ip] = 'Error'
- sem.release()
- def iLOConfig(ip ,l):
- '''Config server ilo'''
- result = 'Configuration done.'
- REMOTEUSER = 'user'
- REMOTPASSWD = 'password'
- GETSCRIPT = 'wget -O /tmp/ilodrac-all.sh ftp://10.23.247.252/home/bakup/auto-scripts/ilodrac-all.sh > /dev/null'
- RUNSCRIPT = 'sh /tmp/ilodrac-all.sh'
- try:
- ssh = pxssh.pxssh()
- ssh.login(ip, REMOTEUSER, REMOTPASSWD)
- ssh.prompt()
- ssh.sendline(GETSCRIPT)
- ssh.prompt()
- ssh.sendline(RUNSCRIPT)
- ssh.prompt(timeout=200)
- ssh.logout()
- except pxssh.ExceptionPxssh, err:
- result= str(err)
- truth[ip] = result
- sem.release()
- def NodeCheck(ip,l):
- '''Check node connectivity'''
- ret = subprocess.call("ping -c 4 %s" % (ip),shell=True,stdout=open('/dev/null', 'w'),stderr=subprocess.STDOUT)
- if ret == 0:
- truth[ip] = 'Alive'
- elif ret == 1:
- truth[ip] = 'Dead'
- else:
- truth[ip] = 'Error'
- sem.release()
- if __name__ == "__main__":
- while (True):
- ErrString = '''Error request types,only provide:
- \tILOCHECK,NODECHECK,ILOCONFIG'''
- result = ''
- todo = list()
- truth = dict()
- Get = GetMail(SERVER, USER, PASSWD)
- Info = Get.doit()
- if Info['Msgs'] == 0:
- sys.exit()
- if Info['Subject'] == 'ILOCHECK':
- for ip in Info['Body'].split('\n'):
- sem.acquire()
- th = threading.Thread(target=iLOCheck, args=(ip,'J'))
- todo.append(th)
- th.start()
- for t in todo:
- t.join()
- for key in truth.keys():
- result = result + "%s\t\t%s\n" % (key, truth[key])
- elif Info['Subject'] == 'NODECHECK':
- for ip in Info['Body'].split('\n'):
- sem.acquire()
- th = threading.Thread(target=NodeCheck, args=(ip,'J'))
- todo.append(th)
- th.start()
- for t in todo:
- t.join()
- for key in truth.keys():
- result = result + "%s\t\t%s\n" % (key, truth[key])
- elif Info['Subject'] == 'ILOCONFIG':
- for ip in Info['Body'].split('\n'):
- sem.acquire()
- th = threading.Thread(target=iLOConfig, args=(ip,'J'))
- todo.append(th)
- th.start()
- for t in todo:
- t.join()
- for key in truth.keys():
- result = result + "%s\t\t%s\n" % (key, truth[key])
- else:
- result = ErrString
- truth.clear()
- Info['From'], Info['To'] = Info['To'], Info['From']
- Send = SendMail(SERVER, USER, PASSWD)
- Send.doit(Info['From'], Info['To'], 'Autoreply: '+Info['Subject'], result)
- if Info['Msgs'] == 1:
- break
复制代码 |
|