批处理实现的网络巡检脚本(PING与端口扫描)

所属分类: 脚本专栏 / DOS/BAT 阅读数: 2140
收藏 0 赞 0 分享

check.bat 内容(脚本内容可能会应为排版问题不能直接复制粘帖运行,可自行修改):

复制代码 代码如下:

@echo off
rem checklist.csv字段为: IP,名称,描述,TCP应用端口,UDP应用端口
rem checklist.csv中请使用,@,替换,, 并不要使用()进行内容描述
set e_file=error.log
set e_count=0
for /f "skip=1 tokens=1,2,3,4,5* delims=," %%i in (checklist.csv) do call :docheck %%i @%%j @%%k @%%l @%%m
call :report
goto :eof
:docheck
set ip=%1
set name=%2
set name=%name:@=%
set comment=%3
set comment=%comment:@=%
set tcp=%4
set tcp=%tcp:@=%
set udp=%5
set udp=%udp:@=%
ping %ip% -n 3 -w 300 >nul 2>&1
if NOT %ERRORLEVEL%==0 (echo %time% %ip%[%name%,%comment%] ping error >>%e_file% & set /a e_count=%e_count%+1) else call :portcheck
call :report
goto :eof
:portcheck
if not -%tcp%==- for /f "tokens=1,2,3,4,5,6,7,8,9,10* delims=-" %%i in ("%tcp%") do call :tcpcheck %%i %%j %%k %%l %%m %%n %%o %%p %%q
if not -%udp%==- for /f "tokens=1,2,3,4,5,6,7,8,9,10* delims=-" %%i in ("%udp%") do call :udpcheck %%i %%j %%k %%l %%m %%n %%o %%p %%q
goto :eof
:tcpcheck
if not -%1==- call :tcpquery %1
if not -%2==- call :tcpquery %2
if not -%3==- call :tcpquery %3
if not -%4==- call :tcpquery %4
if not -%5==- call :tcpquery %5
if not -%6==- call :tcpquery %6
if not -%7==- call :tcpquery %7
if not -%8==- call :tcpquery %8
if not -%9==- call :tcpquery %9
goto :eof
:udpcheck
if not -%1==- call :udpquery %1
if not -%2==- call :udpquery %2
if not -%3==- call :udpquery %3
if not -%4==- call :udpquery %4
if not -%5==- call :udpquery %5
if not -%6==- call :udpquery %6
if not -%7==- call :udpquery %7
if not -%8==- call :udpquery %8
if not -%9==- call :udpquery %9
goto :eof
:tcpquery
portqry -n %ip% -e %1 -p tcp || (echo %time% %ip%[%name%,%comment%] tcp port %1 query error >>%e_file% & set /a e_count=%e_count%+1)
goto :eof
:udpquery
portqry -n %ip% -e %1 -p udp || (echo %time% %ip%[%name%,%comment%] udp port %1 query error >>%e_file% & set /a e_count=%e_count%+1)
goto :eof
:report
echo %e_count%
rem do whatever you want. such as send error.log by mail
goto :eof

checklist.csv样例:
IP,名称,描述,TCP应用端口,UDP应用端口 '两个逗号之间要加入@,描述不能加入括号(),请用[]替代
192.168.0.1,@,@,@,
192.168.0.3,@,@,@,
192.168.0.9,FTPSERVER,FTP服务器,135-445-21,
192.168.0.10,TESTSERVER,TESTSERVER,135-445-21,53

更多精彩内容其他人还在看

DOS 命令字典

DOS 命令字典
收藏 0 赞 0 分享

用replace实现替换正在使用的文件

用replace实现替换正在使用的文件
收藏 0 赞 0 分享

黑客基础之DOS (最齐全)超强推荐

黑客基础之DOS (最齐全)超强推荐
收藏 0 赞 0 分享

bat运行时不弹出那个黑框框的完美解决方案

bat运行时不弹出那个黑框框的完美解决方案
收藏 0 赞 0 分享

收藏的比较完整的批处理教程第1/2页

收藏的比较完整的批处理教程
收藏 0 赞 0 分享

批处理版的SC

批处理版的SC
收藏 0 赞 0 分享

BAT文件语法和技巧(bat文件的编写及使用)

批处理文件是一个文本文件,这个文件的每一行都是一条DOS命令(大部分时候就好象我们在DOS提示符下执行的命令行一样),你可以使用DOS下的Edit或者Windows的记事本(notepad)等任何文本文件编辑工具创建和修改批处理文件
收藏 0 赞 0 分享

用 Net 命令使局域网文件批量同步更新

用 Net 命令使局域网文件批量同步更新
收藏 0 赞 0 分享

如何用DOS命令删除顽固文件夹啊

如何用DOS命令删除顽固文件夹啊
收藏 0 赞 0 分享

dos下删除文件夹和文件的方法

dos下删除文件夹和文件的方法
收藏 0 赞 0 分享
查看更多