码迷,mamicode.com
首页 > 编程语言 > 详细

linux,windows下检测指定的IP地址是否可用或者检测IP地址冲突的3种方式(批处理程序,python程序,linux shell 批量ping)

时间:2018-05-20 00:13:33      阅读:224      评论:0      收藏:0      [点我收藏+]

标签:文件   批量   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.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

 执行结果:

技术分享图片

 

linux,windows下检测指定的IP地址是否可用或者检测IP地址冲突的3种方式(批处理程序,python程序,linux shell 批量ping)

标签:文件   批量   check   find   时间   ip地址   bsp   style   报文   

原文地址:https://www.cnblogs.com/LiuBingBlogs/p/9054035.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!