python
这里用到了四个python 模块 :
import time (时间模块) import re (正则模块) import socket (监测端口模块)import yagmail (发送邮件模块)
代码如下:
import time
import re
import socket
import yagmail
def sendmail(subject,contents):
yag = yagmail.SMTP(user='xxxxxxxxx@qq.com',password='xxxxxxxxx',host='smtp.qq.com')
yag.send(to='xxxxxxxxx@qq.com',subject=subject, contents=contents)
yag.close()
hosts = ['x.x.x.x']
while True:
time.sleep(15)
for host in hosts:
ip = re.compile('(.*?):(.*)').search(host).group(1)
port = re.compile('(.*?):(.*)').search(host).group(2)
server = socket.socket()
server.settimeout(1)
res = server.connect_ex((ip, int(port)))
if res == 0:
print('%s---%s:连接成功' % (ip, port))
else:
print('%s---%s:连接失败' % (ip, port))
sendmail('警告!','你的%s端口所对应的服务宕掉了' %port)