linux,windows下檢測指定的IP地址是否可用或者檢測IP地址衝突的3種方式(批處理程式,python程式,linux shell 批量ping)
阿新 • • 發佈:2019-02-05
本文中的指令碼適用範圍:
1)檢測某些IP地址是否被佔用;
2)檢測網路中某些裝置是否存活;
3)在分配新的ip地址之前,批量檢測環境中是否存在衝突的機器
以上檢測基於ICMP Ping報文,要求所有的裝置允許Ping,裝置開通禁止ICMP策略,防火牆禁止ICMP報文等情況不在本文的考慮範圍之內。
不多說,上程式碼:
(一)windows 批處理指令碼
1 ::autor lb 2 ::date 2018 05 3 @echo off 4 ::屏顯當前的測試時間並輸出到測試結果(詳細日誌和結果日誌) 5 echo %date% %time% 6 7 ::每次執行指令碼時都要先刪除日誌 8 del%cd%\IpCheckerDetailLog.txt 9 del %cd%\IpCheckerResLog.txt 10 echo %date% %time% >>IpCheckerDetailLog.txt 11 echo %date% %time% >>IpCheckerResLog.txt 12 echo IpCheckerResult >>IpCheckerResLog.txt 13 14 ::for迴圈執行ping命令,每次迴圈都會從制定的檔案中讀取ip地址進行ping操作,ping count=1,timeout=1ms,每次Ping結果都會重定向到詳細日誌檔案15 set /a avaNum=0 16 set /a unReaccNum=0 17 for /f "delims=" %%i in (ip.txt) do ( 18 Ping.exe -n 1 -w 1 %%i >> IpCheckerDetailLog.txt 19 if not errorlevel 1 ( echo %%i is avaliable 20 echo %%i is avaliable >> IpCheckerResLog.txt 21 set /a avaNum+=1 ) else ( echo %%i is unreachable[Warning]22 set /a unReaccNum+=1 23 echo %%i is unreachable[Warning] >> IpCheckerResLog.txt ) 24 ) 25 set /a Total=%avaNum% + %unReaccNum% 26 echo Total Count:%Total% 27 echo %avaNum% avaliable; %unReaccNum% unreachable! 28 pause
測試如下:
建立ip.txt,
執行指令碼
(二) Windows Python指令碼
1 #!windows 64 python3.6.5 2 # coding=utf-8 3 4 # Ip檢測指令碼 5 # author lb 6 # time 2018 05 7 import os 8 import datetime 9 10 SrcFileName = "ip.txt"; 11 CurT = datetime.datetime.now().strftime("%m%d_%H%M%S"); 12 CurTInLog = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S\n"); 13 LogFileName = "%s_LogDetail.txt" % CurT; 14 ResFileName = "%s_Res.txt" % CurT; 15 IpOkNum=0; 16 IpFailNum=0; 17 18 # 建立詳細日誌檔案 19 LogFp = open(LogFileName, 'w+'); 20 LogFp.write("PingTestDetailLog\n") 21 LogFp.write(CurTInLog); 22 23 # 建立結果日誌檔案 24 ResFp = open(ResFileName, 'w+'); 25 ResFp.write("PingTestResult\n"); 26 ResFp.write(CurTInLog); 27 28 # 開啟ip檔案 29 SrcFp = open(SrcFileName, "r"); 30 for ip in SrcFp: 31 #print(ip) 32 cmd = "ping -n 1 -w 1 %s" % ip; 33 ret = os.popen(cmd); 34 strRet="%s\n" % ret.read(); 35 LogFp.write( strRet); 36 print(strRet) 37 #找到了TTL關鍵字,證明ping通 38 if strRet.find("TTL") != -1 or strRet.find("丟失 = 0")!=-1: 39 strok="%s Is Reachable \n"% ip; 40 print(strok); 41 ResFp.write(strok); 42 IpOkNum+=1; 43 else: 44 strfail="%s Is Uneachable[Warning]\n"% ip; 45 print(strfail); 46 ResFp.write(strfail); 47 IpFailNum+=1; 48 Res="Total Ip Num i:%d\nReachable Count:%d,Unreachable Count:%d " % ((IpFailNum+IpOkNum),IpOkNum,IpFailNum); 49 print(Res) 50 51 LogFp.close(); 52 ResFp.close(); 53 SrcFp.close();
執行結果:
(三)linux shell
1 #!bin/sh 2 #author lb 3 #date 2018 05 4 5 6 PingFun() 7 { 8 #Creat Log File 9 echo DetailLog>>IpCheckerDetailLog.txt 10 echo DetailResult>>IpCheckerResLog.txt 11 CurT=$(date "+%Y:%m:%D %H:%M:%S") 12 IpAvaCount=0; 13 IpUnReachCount=0; 14 15 #Init Log File 16 echo $CurT>>IpCheckerDetailLog.txt 17 echo $CurT>>pCheckerResLog.txt 18 19 echo Starting ping... 20 #Read Ip 21 while read ip 22 do 23 if ping -c 1 -w 1 $ip >/dev/null ;then 24 echo $ip is avaliable 25 echo $ip is avaliable >>IpCheckerResLog.txt 26 IpAvaCount=$(($IpAvaCount+1)) 27 else echo $ip is unreachable 28 echo $ip is unreachable >>IpCheckerResLog.txt 29 IpUnReachCount=$((1+$IpUnReachCount)) 30 fi 31 done <ip.txt 32 33 #statistics 34 echo Statis: 35 echo Total Count : $(($IpUnReachCount+$IpAvaCount)) 36 echo Avaliable :$IpAvaCount,Unreachable :$IpUnReachCount 37 } 38 PingFun
執行結果:
http://www.q-wei.cn/http://news.q-wei.cn/
http://www.yichaonet.cn/
http://news.yichaonet.cn/
http://www.vxjezfv.cn/
http://news.vxjezfv.cn/
http://www.xibiyo.com.cn/
http://news.xibiyo.com.cn/
http://www.9208361.org.cn/
http://news.9208361.org.cn/
http://www.9111316.cn/
http://news.9111316.cn/
http://www.bluelf.com.cn/
http://news.bluelf.com.cn/
http://www.qqq136com.cn/
http://news.qqq136com.cn/
http://www.2819w.cn/
http://news.2819w.cn/
http://www.9019758.org.cn/
http://news.9019758.org.cn/
http://www.wydaogou.cn/
http://news.wydaogou.cn/
http://www.ralhys.cn/
http://news.ralhys.cn/