通过检测目标服务器的端口状态,可以实现简单ping所不能实现的指定服务的监测,然后脚本可以实现短信或微信通知。
下面是一个检查邮件服务器的实例:https://github.com/zpblog/windows-port-checking
::邮件服务器工作状态监测通知脚本
::zpblog.cn
@echo off & setlocal enabledelayedexpansion
rem 要检测的IP和端口
set server_ip=192.168.1.8,218.218.218.218
set serverport=25,110
rem 模块化调用
set count=0
call :check
call :notice
::****其他代码略****
:check
rem ※端口检测模块--PortQry※
for /f "tokens=1,* delims=," %%i in ("!server_ip!") do (
for %%a in (!serverport!) do (
echo 正在检测 %%i 的 %%a 端口...
rem 这是端口的检测代码:
"C:\PortQryV2\PortQry.exe" -n %%i -e %%a | find ": LISTENING" >nul && (
echo 【成功】:可以连接到 %%i:%%a
) || (
echo 【失败】:无法连通 %%i:%%a
set status=!status! 【失败】:无法连通 %%i:%%a
set /a count+=1
)
echo=
)
set server_ip=%%j
goto check
)
goto :eof
:notice
rem ※判断是否通知--自己发挥吧※
if %count% GEQ 1 (
call :msg
call :mail
) else (
echo 一切正常
)
goto :eof
:mail
rem ※邮件通知模块--sendMail※
"C:\WINDOWS\system32\cmd.exe" /c C:\sendEmail\sendEmail.exe -f [email protected] -t [email protected] -u Mail Server Fault %DATE:~0,10% %TIME% -m %status% -s smtp.qq.com:587 -xu [email protected] -xp asdfghjasdfghj
goto :eof
:msg
rem ※弹框通知模块--msg※
msg %username% /time 1800 "邮件服务器出现故障,请立即协助通知系统管理员处理。联系电话:1234567890"
goto :eof
::****其他代码略****代码比较简单,我相信大家应该能看懂。
里面用到两个EXE程序需要自行下载一下:sendEmail、PortQry
这边重点说一下,如何微信通知(短信通知也类似)。
微信通知:其实是通过微信中一个功能叫“QQ邮箱提醒”(我->设置->通用->功能)。然后通过发邮件到这个绑定的微信的QQ邮箱中,微信就会弹出消息提醒。是不是很Easy。你还可以指定只通知某一类的邮件 (*^__^*) 嘻嘻……
发表评论:
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。
相关文章
转:如何让 Windows 命令行程序像 Linux & 命令那样在后台运行2023-02-24
转:Linux安装rinetd实现TCP/UDP端口转发2021-03-19
转:Linux站点备份打包脚本2020-04-05
windows端口转发(映射)2020-02-20
Python3串口16进制读写通讯2017-10-13
域账号弱口令批量检查-PowerShell2016-09-22
Bat选择性拷贝文件夹里文件2016-09-13
Bat脚本转换成EXE可执行文件2016-08-29
Windows批量建文件夹和寻找目录下空文件夹2016-08-13
Outlook POP模式发件收到退信: 530 Authentication required2016-07-25