3 ways to detect whether a specified IP address is available or to detect IP address collisions under Linux,windows (batch program, python program, Linux shell batch ping)

Source: Internet
Author: User
Tags python script



The scope of the script in this article applies:



1) Detect whether some IP address is occupied;



2) detect the existence of some devices in the network;



3) batch detect if there are conflicting machines in the environment before assigning a new IP address



The above detection is based on the ICMP ping message, all devices are required to allow Ping, the device is disabled ICMP policy, firewall prohibit ICMP packets and so on is not within the scope of this article.



Not much to say, on the code:



(i) Windows batch script


 
1 :: autor lb
 2 :: date 2018 05
 3 @echo off
 4 :: display the current test time and output to the test results (detailed log and result log)
 5 echo% date%% time%
 6
 7 :: Delete the log first every time the script is executed
 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 loop executes the ping command. Each loop will read the ip address from the specified file for ping operation. Ping count = 1, timeout = 1ms, each Ping result will be redirected to the detailed log file.
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)
twenty four )
25 set / a Total =% avaNum% +% unReaccNum%
26 echo Total Count:% Total%
27 echo% avaNum% avaliable;% unReaccNum% unreachable!
28 pause





The test is as follows:



Create Ip.txt,









Execute script






(ii) Windows python script


 
1 #! Windows 64 python3.6.5
 2 # coding = utf-8
 3
 4 # Ip detection script
 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 # Create verbose log files
19 LogFp = open (LogFileName, ‘w +‘);
20 LogFp.write ("PingTestDetailLog \ n")
21 LogFp.write (CurTInLog);
twenty two 
23 # Create a result log file
24 ResFp = open (ResFileName, ‘w +‘);
25 ResFp.write ("PingTestResult \ n");
26 ResFp.write (CurTInLog);
27
28 # open ip file
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 #The TTL keyword was found to prove ping
38 if strRet.find ("TTL")! = -1 or strRet.find ("missing = 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 ();


Operation Result:






(c) 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


Execution Result:









3 ways to detect whether a specified IP address is available or to detect IP address collisions under Linux,windows (batch program, python program, Linux shell batch ping)


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.