思路: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)
補充一種更嚴謹的辦法,使用第三方指令namp,并把下載下傳好的namp目錄加入系統環境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