|
|
本帖最后由 tonyma 于 2019-10-15 21:01 编辑
用python吧,readlines() 开到机器邮箱通知,
1、首先要contab定时执行.sh脚本并输出返回的结果在一个.txt里面(代码:/bin/bash /root/oci.sh >/var/tmp/test.txt 2>&1)
2、python 代码读取.txt文件,readlines(),判断返回结果行数组中的关键字error(一般没库存和其他错误内容都是返回一个error)
3、oci.sh (
第一行: oci compute instance launch --availability-domain ULJk:AP-SEOUL-1-AD-1 --image(这里是oci 命令)
第二行: python jsontest.py
)
- # -*- coding: UTF-8 -*-
- import json
- import io
- import smtplib
- from email.mime.text import MIMEText
- def success_mail():
- mail_host = 'smtp.qq.com' #发送发服务器地址
- mail_user = '[email protected]'#发送方邮箱
- mail_pass = '###' #发送方密码
- sender = '[email protected]'
- receivers = ['[email protected]']
- message = MIMEText('successfully open the machine','plain','utf-8')#发送的内容
- message['Subject'] = 'congratulations!'
- message['From'] = sender
- message['To'] = receivers[0]
- try:
- smtpObj = smtplib.SMTP_SSL("smtp.qq.com",465)
- smtpObj.login(mail_user,mail_pass)
- smtpObj.sendmail(
- sender,receivers,message.as_string())
- smtpObj.quit()
- print('success')
- except smtplib.SMTPException as e:
- print('error',e)
- def fail_mail():
- mail_host = 'smtp.qq.com'#发送发服务器地址
- mail_user = '[email protected]'#发送方邮箱
- mail_pass = '###' #发送方密码
- sender = '[email protected]'
- receivers = ['[email protected]']#接收方
- message = MIMEText('fail to open the machine','plain','utf-8') #发送的内容
- message['Subject'] = 'fail!'
- message['From'] = sender
- message['To'] = receivers[0]
- try:
- smtpObj = smtplib.SMTP_SSL("smtp.qq.com",465)
- smtpObj.login(mail_user,mail_pass)
- smtpObj.sendmail(
- sender,receivers,message.as_string())
- smtpObj.quit()
- print('success')
- except smtplib.SMTPException as e:
- print('error',e)
- f = open('/var/tmp/test.txt','r')
- lines=f.readlines()
- if 'Error' not in lines[2]:
- success_mail()
复制代码 |
|