linux,windows下檢測指定的IP地址是否可用或者檢測IP地址衝突的3種方式(批次程式,python程式,linux shell 批量ping)

來源:互聯網
上載者:User

標籤:檔案   批量   check   find   時間   ip地址   bsp   style   報文   

本文中的指令碼適用範圍:

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.txt11 echo %date% %time%  >>IpCheckerResLog.txt12 echo IpCheckerResult >>IpCheckerResLog.txt13 14 ::for迴圈執行ping命令,每次迴圈都會從制定的檔案中讀取ip地址進行ping操作,ping count=1,timeout=1ms,每次Ping結果都會重新導向到詳細記錄檔15 set /a avaNum=016 set /a unReaccNum=017 for /f "delims=" %%i in (ip.txt) do (18 Ping.exe -n 1 -w 1 %%i >> IpCheckerDetailLog.txt19 if not errorlevel 1 ( echo %%i            is avaliable20 echo %%i            is avaliable >> IpCheckerResLog.txt21 set /a avaNum+=1 ) else ( echo %%i            is unreachable[Warning] 22 set /a unReaccNum+=123 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.txt10 echo DetailResult>>IpCheckerResLog.txt11 CurT=$(date "+%Y:%m:%D %H:%M:%S")12 IpAvaCount=0;13 IpUnReachCount=0;14 15 #Init Log File16 echo $CurT>>IpCheckerDetailLog.txt17 echo $CurT>>pCheckerResLog.txt18 19 echo Starting ping...20 #Read Ip 21 while read ip22 do23     if ping -c 1 -w 1 $ip >/dev/null ;then24         echo $ip     is avaliable25         echo $ip     is avaliable >>IpCheckerResLog.txt26         IpAvaCount=$(($IpAvaCount+1))27     else echo $ip     is unreachable28          echo $ip     is unreachable >>IpCheckerResLog.txt    29         IpUnReachCount=$((1+$IpUnReachCount))30     fi31 done <ip.txt32 33 #statistics34 echo Statis:35 echo Total Count : $(($IpUnReachCount+$IpAvaCount))36 echo Avaliable :$IpAvaCount,Unreachable :$IpUnReachCount37 }38 PingFun

 執行結果:

 

linux,windows下檢測指定的IP地址是否可用或者檢測IP地址衝突的3種方式(批次程式,python程式,linux shell 批量ping)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.