1. 程式人生 > >windows批處理批量判斷遠端埠是否開放的簡單粗糙辦法

windows批處理批量判斷遠端埠是否開放的簡單粗糙辦法

思路: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