思路:1.把ping不通的排除以节省脚本运行时间,不考虑那些特殊情况
2.记录telnet命令的执行时间,时间不超过2S的视为连接上,有点简单粗暴但暂时想不出更好的办法
脚本如下,只能用于不严谨的场合:
@echo off&setlocal ENABLEDELAYEDEXPANSION
for /l %%i in (1,1,255) do call :step %%i
echo 完毕!
pause
:step
ping /n 1 223.95.165.%1 || goto :EOF
set _time_start=!time!
set /a second_start=!_time_start:~6,2!
set /a minute_start=!_time_start:~3,2!
set /a hour_start=!_time_start:~0,2!
set /a second_start=!hour_start!*3600+!minute_start!*60+!second_start!
echo q|telnet -e 'q' 223.95.165.%1 554
set _time_end=!time!
set /a second_end=!_time_end:~6,2!
set /a minute_end=!_time_end:~3,2!
set /a hour_end=!_time_end:~0,2!
if !hour_end! lss !hour_start! ( set /a hour_end=!hour_end!+24 )
set /a second_end=!hour_end!*3600+!minute_end!*60+!second_end!
set /a time_spent=!second_end! - !second_start!
if !time_spent! leq 2 ( echo 223.95.165.%1 554>>C:/result.txt)
补充一种更严谨的办法,使用第三方命令nmap,并把下载好的nmap目录加入系统环境path中以便cmd识别此命令
命令行包下载地址:https://nmap.org/download.html
脚本简单多了,如下:
@echo off&setlocal ENABLEDELAYEDEXPANSION
for /l %%i in (1,1,255) do call :step %%i
echo 完毕!
pause
:step
nmap -sT -p 554 223.151.22.%1|findstr "open"&&echo 223.151.22.%1 554 port is open>>C:/result.txt||echo 223.151.22.%1 554 port is not open