#!/usr/bin/env python
# Version = 3.8.1
# -*- coding: utf-8 -*-
# Author: lumia98@vip.qq.com
import random, string
import time
import smtplib
import socket
from smtplib import SMTP_SSL
from imaplib import IMAP4
from email.mime.text import MIMEText
from email.header import Header
# 发送QQ邮箱验证码,参数为收件箱地址和随机验证码
def send_email(receover, ecode):
send_auther = "<xx@qq.com>" # 好博客可以随便取
text = f"<br/> 服务器端口不通,服务器ip和端口为: <span stype='color: red; font-size:20px;'>{ecode}</span>, 请及时查看服务器"
content = text
# 实例化邮件对象,指定邮件的关键信息
message = MIMEText(content, 'html', 'utf-8')
# 指定邮件标题
message['subject'] = Header('服务器端口不通', 'utf-8')
message['From'] = send_auther # 指定发件人信息
message['To'] = receover # 收件人email地址
# 登录地址及账号
smtp_server = 'smtp.qq.com'
smtp_port = 465
smtpobj = smtplib.SMTP_SSL(smtp_server, smtp_port) # 登录QQ邮件服务器
# 通过你的邮箱账号获取QQ给的授权码
smtpobj.login(user='QQ账号', password='腾讯授权码')
# 发送信息
smtpobj.sendmail(send_auther, receover, str(message))
smtpobj.quit() # 退出
# telnet 端口的IP地址
def CheckPort(server_ip, port):
sk = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sk.settimeout(5) # 设置超时时间
try:
sk.connect((server_ip, port))
except Exception:
server_info = (server_ip, port)
#print(server_info)
send_email('接收邮箱账号', server_info)
sk.close()
# 服务器地址和端口
ipaddr_237 = {'192.168.21.237': [8090, 8009, 8443]}
ipaddr_238 = {'192.168.21.238': [8081, 10000]}
ipaddr_240 = {'192.168.21.240': [1433, 8080]}
ipaddr_241 = {'192.168.21.241': [8080, 88]}
ipaddr_242 = {'192.168.21.242': [6008, 5084, 8081]}
#
ip_list = [ipaddr_237, ipaddr_238, ipaddr_240, ipaddr_241, ipaddr_242]
# 方式一
def nostop(ip_lists):
# 一直循环telnet列表里ip端口
while 1:
for first in ip_lists:
for k, y in first.items():
for port in y:
#print(k, port)
CheckPort(k, port)
time.sleep(45)
if __name__ == "__main__":
nostop(ip_list)
```
Python探测端口存活脚本且发邮件
最新推荐文章于 2024-05-29 21:47:01 发布