检测服务器是否宕机
需求:ping一台需要被检测的服务器,如果丢包率为100%,则表示机器出问题了,随后发送报警邮件(首先需要一个邮箱账号,并开启smtp服务,报警邮件将由该邮箱发出)
创建发送邮件的Python脚本:
#!/usr/bin/python
#coding:utf-8
import smtplib
from email.mime.text import MIMEText
import sys
#发信地址
mail_user = 'asnfy2019@163.com'
#发信地址的SMTP授权密码
mail_pass = 'xxxxxxxx'
def send_mail(to_list,subject,content):
me = "邮件报警"+"<"+mail_user+">"
msg = MIMEText(content, 'plain', 'utf-8')
msg['Subject'] = subject
msg['From'] = me
msg['to'] = to_list
try:
#定义网易163邮箱提供的SMTP服务地址
s = smtplib.SMTP("smtp.163.com", 25)
s.login(ma