发布时间:2019-09-17 07:48:15编辑:auto阅读(1564)
1.shell脚本ping局域网的脚本
脚本代码
#!/bin/bash for i in {1..50} do ping -c 1 192.168.0.$i >/dev/null if [ $? = 0 ] then echo "192.168.0.$i used" else echo "192.168.0.$i unused" fi done
执行结果及执行时间
2.python版本ping局域网的脚本(windows和linux通用)
#!/usr/bin/python #_*_coding:utf-8_*_ import os import time from threading import Thread class PING(Thread): def __init__(self,ip): Thread.__init__(self) self.ip=ip def run(self): if os.name=="nt": output=os.popen("ping -n 1 %s" % (self.ip)).read().split("\r\n") if "Packets: Sent = 1, Received = 1, Lost = 0 (0% loss)," in output: print "%s 使用" % self.ip else: print "\033[32m%s 未使用\033[0m" % self.ip elif os.uname()[0] == "Linux": output=os.popen("ping -c 1 -w 2 %s" % (self.ip)).read().split("\n") if "1 packets transmitted, 1 received, 0% packet loss, time 0ms" in output: print "%s 使用\r" % self.ip else: print "\033[32m%s 未使用\r\033[0m" % self.ip else: print ("%s is ERROR" %(self.ip)) #多线程同时执行 T_thread=[] Ip_addr=[] aa=raw_input('\033[31m input ping range,exp:192.168.0.0,192.168.0.10 \033[0m\n>>>') aa1=aa.split('.') ip1="%s.%s.%s." %(aa1[0],aa1[1],aa1[2]) ip2=int(aa1[6]) ip2+=1 aa2=aa1[3].split(',') for i in range(int(aa2[0]),ip2): Ip_addr.append(ip1+str(i)) for i in Ip_addr: t=PING(i) T_thread.append(t) for i in range(len(T_thread)): T_thread[i].start()
执行结果和时间截图
总结:
有可能小批量任务看不出明显差别,但是大批量执行,python多线程脚本是shell脚本使用的一半时间
上一篇: 关于ModuleNotFoundErro
下一篇: 关于grequests的session/
47840
46389
37280
34731
29312
25971
24909
19950
19541
18030
5791°
6411°
5926°
5961°
7063°
5911°
5942°
6437°
6404°
7777°