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


    本文中的脚本适用范围:

    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
    ");
    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
    ")
    21 LogFp.write(CurTInLog);
    22 
    23 # 创建结果日志文件
    24 ResFp = open(ResFileName, 'w+');
    25 ResFp.write("PingTestResult
    ");
    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
    " % 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 
    "% ip;
    40         print(strok);
    41         ResFp.write(strok);
    42         IpOkNum+=1;
    43     else:
    44         strfail="%s Is Uneachable[Warning]
    "% ip;
    45         print(strfail);
    46         ResFp.write(strfail);
    47         IpFailNum+=1;
    48 Res="Total Ip Num i:%d
    Reachable  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

     执行结果:

    除特殊说明外,其余所有文章均属原创。未经允许,请勿进行转载或者其他操作 有问题欢迎留言交流
  • 相关阅读:
    Google Chrome开发者工具
    自动跳转页面
    Java读取properties配置文件
    Linux(CentOS):安装JDK + MySQL + FTP
    MySQL数据库完全卸载
    Java冒泡排序
    Java选择排序
    Java多线程总结
    数据库索引(Oracle和MySql)
    Oracle数据库
  • 原文地址:https://www.cnblogs.com/LiuBingBlogs/p/9054035.html
Copyright © 2020-2023  润新知