linux定时监控网络连接,利用python发送邮件
2016-04-12 14:56
926 查看
服务器A需要调用服务器B上面的服务,但是经常遇到连接超时的问题,需要监控一下网络状态:
shell脚本:
#!/bin/sh
PYTHON_PATH=/usr/bin
ping -c 5 www.suiyuan521.com
if [ "$?" != "0" ]; then
$PYTHON_PATH/python send_email.py "I can not connect to abc.com"
fi
python发送邮件脚本:
# !/usr/bin/python
"""just a send email python script"""
#-*- coding: utf8 -*-
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
from email.Header import Header
from email.MIMEImage import MIMEImage
import smtplib
import sys
import datetime
import mimetypes
import os
"""send_email function"""
def send_email(subject, body):
sender = 'suiyuan@521.com'
file_name = './abc.txt'
msgRoot = MIMEMultipart()
msgRoot["Accept-Charset"] = "UTF-8"
msgRoot["Accept-Language"] = "zh-CN"
msgRoot['Subject'] = Header(subject,"UTF-8")
msgRoot['From'] = sender
receivers = ['suiyuan521@love.com']
msgRoot['To'] = ','.join(receivers)
msgRoot.preamble = '%s is a MIME message' % (subject)
msgAlternative = MIMEMultipart("alternative")
msgRoot.attach(msgAlternative)
if not body:
body = "随缘吧APP不错,给你带来缘分"
msgText = MIMEText(body)
msgText.set_charset("UTF-8")
msgText = MIMEText(body, 'html', 'UTF-8')
msgText.set_charset("UTF-8")
msgAlternative.attach(msgText)
// 发送附件
ctype,encoding = mimetypes.guess_type(file_name)
if ctype is None or encoding is not None:
ctype='application/octet-stream'
maintype,subtype = ctype.split('/',1)
file_msg=MIMEImage(open(file_name,'rb').read(),subtype)
basename = os.path.basename(file_name)
file_msg.add_header('Content-Disposition','attachment', filename = basename)
msgAlternative.attach(file_msg)
s = smtplib.SMTP('proxy-in.suiyuan.com') // 设置邮件服务器地址
s.sendmail(sender, receivers, msgRoot.as_string())
s.quit()
"""main function"""
if __name__ == '__main__':
body = sys.argv[1]
now_time = datetime.datetime.now()
yes_time = now_time + datetime.timedelta(days=-1)
yes_time_nyr = yes_time.strftime('%Y-%m-%d')
subject = "随缘吧助您监控网络状态"
send_email(subject,body)
然后将shell脚本加入crontab定时任务就可以了。
*/10 * * * * cd /home/suiyuan/app/monitor; sh monitorNet.sh
shell脚本:
#!/bin/sh
PYTHON_PATH=/usr/bin
ping -c 5 www.suiyuan521.com
if [ "$?" != "0" ]; then
$PYTHON_PATH/python send_email.py "I can not connect to abc.com"
fi
python发送邮件脚本:
# !/usr/bin/python
"""just a send email python script"""
#-*- coding: utf8 -*-
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
from email.Header import Header
from email.MIMEImage import MIMEImage
import smtplib
import sys
import datetime
import mimetypes
import os
"""send_email function"""
def send_email(subject, body):
sender = 'suiyuan@521.com'
file_name = './abc.txt'
msgRoot = MIMEMultipart()
msgRoot["Accept-Charset"] = "UTF-8"
msgRoot["Accept-Language"] = "zh-CN"
msgRoot['Subject'] = Header(subject,"UTF-8")
msgRoot['From'] = sender
receivers = ['suiyuan521@love.com']
msgRoot['To'] = ','.join(receivers)
msgRoot.preamble = '%s is a MIME message' % (subject)
msgAlternative = MIMEMultipart("alternative")
msgRoot.attach(msgAlternative)
if not body:
body = "随缘吧APP不错,给你带来缘分"
msgText = MIMEText(body)
msgText.set_charset("UTF-8")
msgText = MIMEText(body, 'html', 'UTF-8')
msgText.set_charset("UTF-8")
msgAlternative.attach(msgText)
// 发送附件
ctype,encoding = mimetypes.guess_type(file_name)
if ctype is None or encoding is not None:
ctype='application/octet-stream'
maintype,subtype = ctype.split('/',1)
file_msg=MIMEImage(open(file_name,'rb').read(),subtype)
basename = os.path.basename(file_name)
file_msg.add_header('Content-Disposition','attachment', filename = basename)
msgAlternative.attach(file_msg)
s = smtplib.SMTP('proxy-in.suiyuan.com') // 设置邮件服务器地址
s.sendmail(sender, receivers, msgRoot.as_string())
s.quit()
"""main function"""
if __name__ == '__main__':
body = sys.argv[1]
now_time = datetime.datetime.now()
yes_time = now_time + datetime.timedelta(days=-1)
yes_time_nyr = yes_time.strftime('%Y-%m-%d')
subject = "随缘吧助您监控网络状态"
send_email(subject,body)
然后将shell脚本加入crontab定时任务就可以了。
*/10 * * * * cd /home/suiyuan/app/monitor; sh monitorNet.sh
相关文章推荐
- linux网络编程之socket(TCP)
- 网络虚拟化技术
- 实例讲解虚拟机3种网络模式(桥接、nat、Host-only)
- opencv神经网络训练时的目标矩阵构建方法
- android:WebView在没有网络情况下,点击当前页面内链接不跳转并弹出提示框
- 使用httpclient4.4爬取知乎答案
- HttpClient4.x 使用cookie保持会话
- php开发http json接口
- Android Volley 网络并发 监听网络队列完成
- 当你在浏览器地址栏输入一个URL后回车,将会发生的事情?
- Android: WebView加载网页的几种方式及网络异常处理
- http 请求头
- 使用域账号统一管理cisco网络设备
- DL:卷积神经网络(CNN)的一些学习网址
- angularJS里的$http.post传数据到php接收问题
- golang http服务器跨域问题解决
- 神经网络第一部分:背景Neural Networks, Part 1: Background
- 神经网络第二部分:神经元Neural Networks, Part 2: The Neuron
- 神经网络第三部分:网络Neural Networks, Part 3: The Network
- 神经网络第三部分:网络Neural Networks, Part 3: The Network