[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

[网络连接] 批处理怎样ping一组IP并根据返回结果执行不同操作?

新手求帮忙。  
过程是  输入IP地址192.168.1.XX 最后一组 数字XX, 然后ping 这个IP 。  ping 50次  (ping 192.168.1.1 -t 50 -l 65500) ,如果提示Request timed out.的次数超过5次就提示 “这个ip有问题”,  如果返回结果的 时间超过 20ms也提示“这个ip有问题”。没有触发这两个选项就提示 “成功了,并且按任意键 重新开始这个批处理 。”

  1. @echo off
  2. set /p a=输入IP 192.168.1.
  3. set n=0
  4. :loop
  5. ping 192.168.1.%a% -w 20 -n 1 >nul
  6. if errorlevel 1 (set /a n+=1) else (set n=0)
  7. if %n%==20 echo youwenti&pause
  8. goto loop
复制代码
大概写了下  没测试

TOP

本帖最后由 ivor 于 2016-2-15 18:30 编辑

回复 1# a394484
  1. @echo off&setlocal ENABLEDELAYEDEXPANSION
  2. rem IP格式192.168.1.XXX
  3. set ipLeft=192.168.1.
  4. rem 起始IP
  5. set startIp=1
  6. rem 截止IP
  7. set endIp=100
  8. for /l %%a in (%startIp%,1,%endIp%) do (
  9. call :scanIP !ipLeft!%%a
  10. echo ******************
  11. )
  12. pause
  13. goto :EOF
  14. :scanIP ipAdress
  15. set /a error=0
  16. for /l %%b in (1,1,50) do (
  17. ping %1 -n 1 -w 20>nul
  18. if !errorlevel! neq 0 (set /a error+=1)>nul
  19. if !error! equ 5 (echo 超时IP:%1 & goto :eof)
  20. )
  21. echo 正常IP:%1
复制代码
python 3.5 速度很快查找局域网存活主机
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3.   
  4. """
  5. 探测网络主机存活。
  6. """
  7.   
  8. import os
  9. import struct
  10. import array
  11. import time
  12. import socket
  13. import IPy
  14. import threading
  15.   
  16. class SendPingThr(threading.Thread):
  17.     '''
  18.     发送ICMP请求报文的线程。
  19.   
  20.     参数:
  21.         ipPool      -- 可迭代的IP地址池
  22.         icmpPacket  -- 构造的icmp报文
  23.         icmpSocket  -- icmp套字接
  24.         timeout     -- 设置发送超时
  25.     '''
  26.     def __init__(self, ipPool, icmpPacket, icmpSocket, timeout=3):
  27.         threading.Thread.__init__(self)
  28.         self.Sock = icmpSocket
  29.         self.ipPool = ipPool
  30.         self.packet = icmpPacket
  31.         self.timeout = timeout
  32.         self.Sock.settimeout( timeout + 3 )
  33.   
  34.     def run(self):
  35.         time.sleep(0.01)  #等待接收线程启动
  36.         for ip in self.ipPool:
  37.             try:
  38.                 self.Sock.sendto(self.packet, (ip, 0))
  39.             except socket.timeout:
  40.                 break
  41.         time.sleep(self.timeout)
  42.   
  43. class Nscan:
  44.     '''
  45.     参数:
  46.         timeout    -- Socket超时,默认3秒
  47.         IPv6       -- 是否是IPv6,默认为False
  48.     '''
  49.     def __init__(self, timeout=3, IPv6=False):
  50.         self.timeout = timeout
  51.         self.IPv6 = IPv6
  52.   
  53.         self.__data = struct.pack('d', time.time())   #用于ICMP报文的负荷字节(8bit)
  54.         self.__id = os.getpid()   #构造ICMP报文的ID字段,无实际意义
  55.   
  56.     @property   #属性装饰器
  57.     def __icmpSocket(self):
  58.         '''创建ICMP Socket'''
  59.         if not self.IPv6:
  60.             Sock = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.getprotobyname("icmp"))
  61.         else:
  62.             Sock = socket.socket(socket.AF_INET6, socket.SOCK_RAW, socket.getprotobyname("ipv6-icmp"))
  63.         return Sock
  64.   
  65.     def __inCksum(self, packet):
  66.         '''ICMP 报文效验和计算方法'''
  67.         if len(packet) & 1:
  68.             packet = packet + '\0'
  69.         words = array.array('h', packet)
  70.         sum = 0
  71.         for word in words:
  72.             sum += (word & 0xffff)
  73.         sum = (sum >> 16) + (sum & 0xffff)
  74.         sum = sum + (sum >> 16)
  75.   
  76.         return (~sum) & 0xffff
  77.   
  78.     @property
  79.     def __icmpPacket(self):
  80.         '''构造 ICMP 报文'''
  81.         if not self.IPv6:
  82.             header = struct.pack('bbHHh', 8, 0, 0, self.__id, 0) # TYPE、CODE、CHKSUM、ID、SEQ
  83.         else:
  84.             header = struct.pack('BbHHh', 128, 0, 0, self.__id, 0)
  85.   
  86.         packet = header + self.__data     # packet without checksum
  87.         chkSum = self.__inCksum(packet) # make checksum
  88.   
  89.         if not self.IPv6:
  90.             header = struct.pack('bbHHh', 8, 0, chkSum, self.__id, 0)
  91.         else:
  92.             header = struct.pack('BbHHh', 128, 0, chkSum, self.__id, 0)
  93.   
  94.         return header + self.__data   # packet *with* checksum
  95.   
  96.     def isUnIP(self, IP):
  97.         '''判断IP是否是一个合法的单播地址'''
  98.         IP = [int(x) for x in IP.split('.') if x.isdigit()]
  99.         if len(IP) == 4:
  100.             if (0 < IP[0] < 223 and IP[0] != 127 and IP[1] < 256 and IP[2] < 256 and 0 < IP[3] < 255):
  101.                 return True
  102.         return False
  103.   
  104.     def makeIpPool(self, startIP, lastIP):
  105.         '''生产 IP 地址池'''
  106.         IPver = 6 if self.IPv6 else 4
  107.         intIP = lambda ip: IPy.IP(ip).int()
  108.         ipPool = {IPy.intToIp(ip, IPver) for ip in range(intIP(startIP), intIP(lastIP)+1)}
  109.         return {ip for ip in ipPool if self.isUnIP(ip)}
  110.   
  111.     def mPing(self, ipPool):
  112.         '''利用ICMP报文探测网络主机存活
  113.   
  114.         参数:
  115.             ipPool  -- 可迭代的IP地址池
  116.         '''
  117.         Sock = self.__icmpSocket
  118.         Sock.settimeout(self.timeout)
  119.         packet = self.__icmpPacket
  120.         recvFroms = set()   #接收线程的来源IP地址容器
  121.   
  122.         sendThr = SendPingThr(ipPool, packet, Sock, self.timeout)
  123.         sendThr.start()
  124.   
  125.         while True:
  126.             try:
  127.                 recvFroms.add(Sock.recvfrom(1024)[1][0])
  128.             except Exception:
  129.                 pass
  130.             finally:
  131.                 if not sendThr.isAlive():
  132.                     break
  133.         return recvFroms & ipPool
  134.   
  135. if __name__ == '__main__':
  136.     s = Nscan()
  137. #更改下面的地址范围
  138.     ipPool = s.makeIpPool('192.168.1.1', '192.168.1.254')
  139.     print( s.mPing(ipPool) )
  140. input("press any key to continu......")
复制代码
#&cls&@powershell "Invoke-Expression ([Io.File]::ReadAllText('%~0',[Text.Encoding]::UTF8))" &pause&exit

TOP

返回列表