写了个检查一个C段的ip是否可以ping通的脚本

使用多线程,在linux服务器上秒出结果,主要是快,只支持一个c段之内的地址,例如python ipcheck.py 192.168.0.1 192.168.0.254

#-*-coding:utf-8-*-
#****************************************************************#
# ScriptName: ipcheck.py
# Author: kf
# Create Date: 2019-05-26 18:38
# Modify Author: kf
# Modify Date: 2019-05-26 18:38
# Function: 
#***************************************************************#
import threading,sys,os,re,time
inused = []
notused = []
inusedcount = 0
notusedcount = 0
lock = threading.Lock()
class ipcheck(threading.Thread):
    def __init__(self,ip,cmd):
        threading.Thread.__init__(self)
        self.ip = ip
        self.cmd = cmd
    def run(self):
        code = os.system(self.cmd)
        global inusedcount
        global notusedcount
        global inused
        global notused
        if code == 0:
            lock.acquire()
            inused.append(self.ip)
            inusedcount += 1
            lock.release()
        else:
            lock.acquire()
            notused.append(self.ip)
            notusedcount += 1
            lock.release()
            



if len(sys.argv) != 3:
    print(len(sys.argv))
    print "Please input the range of ip to check !!!!"
    print "e.g : ipcheck.py 192.168.1.1 192.168.1.99" 
    exit(1)
#else:
#    if sys.argv 
start = sys.argv[1].split('.')
end = sys.argv[2].split('.')
if start[0] == end[0] and start[1] == end[1] and start[2] == end[2]:
    netbit = "%s.%s.%s"%(start[0],start[1],start[2])
    x = int(start[3])
    y = int(end[3]) + 1
    for hostbit in range(x,y):
        ip = netbit + "." + str(hostbit)
        cmd = "ping -c 1 -W 1 " + ip + " > /dev/null" 
        ipcheck(ip,cmd).start()
time.sleep(1)
while True:
    if threading.active_count() <= 1:
        print "in using :"
        print inused
        print inusedcount
        print "-------------------------------------------------------"
        print "not used :"
        print notused
        print notusedcount
        break



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值