python3.6 모니터링 서버 온라인, 오프라인 자동 메일 발송
2217 단어 놀다
# -*- coding=utf-8 -*-
import os
import sys
import smtplib
from email.mime.text import MIMEText
from email.utils import formataddr
class server:
def __init__(self):
self.iplist = list()
self.my_sender = '' #
self.my_pass = '' # ,POP3
self.my_user = '' #
self.down_ip = '
'
self.up_ip = '
'
def read_ip(self):
f = open(r'server_ip.txt') # IP ip
self.lines = f.readlines()
def mail(self):
try:
msg = MIMEText(self.down_ip,'plain','utf-8')
msg['From'] = formataddr(["manunkind",self.my_sender])
msg['To'] = formataddr(["manunkind",self.my_user])
msg['Subject'] = "server down"
server = smtplib.SMTP_SSL("smtp.qq.com", 465)
server.login(self.my_sender, self.my_pass)
server.sendmail(self.my_sender,[self.my_user,],msg.as_string())
server.quit()
print("Mail sent successfully")
except Exception:
print("Mail delivery failed")
def ping_server(self):
for ip in self.lines:
if '.'in ip:
backinfo = os.system('ping -n 5 -w 30 %s'%ip)
if backinfo:
self.down_ip = self.down_ip + ip
else:
self.up_ip = self.up_ip + ip
else:
print('
'+ip)
self.down_ip = '
'+ self.down_ip+ip
self.up_ip = '
'+ self.up_ip+ip
if '.' in self.down_ip:
self.mail()
print(self.up_ip)
if __name__ == '__main__':
sIP = server()
sIP.read_ip()
sIP.ping_server()