python类脚本

一在windows主机上探测主机是否存活

下面以多线程的方式:

import os
import time
import subprocess
from concurrent.futures import ThreadPoolExecutor
import re


def ping_call(num):
        ipaddr='ping 192.168.0.'+str(num)
        command=ipaddr+' -n 2 -w 1'
        # print(command)
        result=subprocess.Popen(command,
                               shell=True,stdout=subprocess.PIPE)
        s=result.stdout.read().decode('gbk')
        e = "TTL" in s
        if e:
            print('ip地址:{} ping ok'.format(ipaddr))
        else:
            print('ip地址:{} ping fall'.format(ipaddr))


# ping_call() #单执行程序耗时96.08
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()
        res_l=[]
        pool=ThreadPoolExecutor(50)
        for line in range(a,b):
            ret=pool.submit(ping_call,line)
            res_l.append(ret)
        pool.shutdown()
        print("程序耗时{:.2f}".format(time.time() - start_time))

猜你喜欢

转载自www.cnblogs.com/mmyy-blog/p/9565425.html