背景:在window server系统中的进行监控udp端口的时候发现, 每次进程重启后端口都会发生变化,于是尝试放弃监控端口改用监控进程名的想法。
bat
::Final interpretation is owned by chenglee ::Thankyou ::温馨提示:如果放在win启动计划中,如需要就开启第六行代码, ::作用是检测完马上自动关闭检测脚本待下次运行, 如果不开启就是检测完停留等待手动关闭, ::作用:窗口叠加 ::@echo off<nul 3>nul @echo off&title Checking For System, Do Not Switch Off, Thankyou... set num=QQ.exe for /f "tokens=1 delims=: " %%a in (‘tasklist‘) do ( if "%%a"=="%num%" goto en ) if not "%%a"=="%num%" goto en1 :en echo.Checking For local%NUM%Process is Opening... pause exit :en1 echo.Checking For local%NUM%Process is Not Opening... echo.send mail for chenglee python mail.py pause exit
第8行:设置进程名
第9行:
‘tasklist‘,列出本系统所在运行的所有进程
‘tokens‘,需要扫描的列(我这里扫第1列)
第10行:判断扫描列表中的内容是否与我第8行设定的num一致, 一致的话跑下面的:en段
第12行:判断扫描列表中的内容是否与我第8行设定的num一致, 不一致的话跑下面的:en1段
第23行:已经发现程序不在了, 调用python脚本进程邮件提醒
mail.py
import smtplib from email.mime.text import MIMEText from email.utils import formataddr my_sender=‘[email protected]‘ my_pass = ‘wkzbchff‘ my_user=‘[email protected]‘ my_context=‘测试监控进程操作‘ my_title=‘来自服务器的信息‘ my_chengname=‘chenglee监控‘ def mail(): ret=True try: msg=MIMEText(my_context,‘plain‘,‘utf-8‘) msg[‘From‘]=formataddr([my_chengname,my_sender]) msg[‘To‘]=formataddr(["收件人昵称",my_user]) msg[‘Subject‘]=my_title server=smtplib.SMTP_SSL("smtp.qq.com", 465) server.login(my_sender, my_pass) server.sendmail(my_sender,[my_user,],msg.as_string()) server.quit() except Exception: ret=False return ret ret=mail() if ret: print("Mail send is ok ...") else: print("Mail send is error ...")
完毕!
原文地址:https://www.cnblogs.com/chenglee/p/8427588.html
时间: 2024-07-28 23:44:16