下面是最初的情况

#/usr/bin/env python
# -*- coding: utf-8 -*-

import os
import time
import subprocess
import threading
from threadpool import ThreadPool
import threadpool
import re
from Queue import Queue

def ping_call():
    while not IP_QUEUE.empty():
        ip=IP_QUEUE.get()
        command='ping -c 2 -W 1 %s'%ip
        print(command)
        result=subprocess.Popen(command,
                       shell=True,stdout=subprocess.PIPE) //A
        s=result.stdout.read()
        e = "ttl" in s
        if e:
            print('ip地址:{} ping ok'.format(ip))
        else:
            print('ip地址:{} ping fall'.format(ip))      //B


if __name__ == '__main__':
    network=input('请输入网段>>>').strip()
    host=input('请输入主机范围以空格隔开>>>').strip().split()
    a,b=host[0],host[1]
    print(network.split('.'))
    if len(network.split('.'))==3 and a.isdigit() and b.isdigit() and re.match('\d{1,3}\.\d{1,3}\.\d{1,3}',network):
        a=int(a)
        b=int(b)
        start_time = time.time()
        IP_QUEUE=Queue()
    threads=[]
    for i in range(a,b+1):
            IP_QUEUE.put(network+'.'+str(i))
    for i in range(50):
        thread=threading.Thread(target=ping_call)
        thread.start()
        threads.append(thread)
    for thread in threads:
        thread.join()
        print("程序耗时{:.2f}".format(time.time() - start_time))
View Code

相关文章:

  • 2021-08-05
  • 2022-12-23
  • 2021-09-07
  • 2021-08-24
  • 2021-05-23
  • 2022-12-23
  • 2022-12-23
  • 2022-02-07
猜你喜欢
  • 2021-07-23
  • 2021-08-01
  • 2022-12-23
  • 2021-11-18
  • 2022-12-23
  • 2021-05-19
相关资源
相似解决方案