#2楼@brucelandor
```
#!/usr/bin/env python
#coding:utf-8
import os
import time
import threading
numlock =threading.RLock()
prlock=threading.RLock()
zxs=0
class pings(threading.Thread):
def __init__(self,num,interval):
threading.Thread.__init__(self)
self.nums=num
self.inter=interval
self.thread_stop=False
self.ns=0
def run(self):
global zxs
start=self.nums
while start<254 and not self.thread_stop:
ret=os.system('ping -c 1 -W 2 172.16.100.%d >/dev/null' % start)
if not ret:
prlock.acquire()
print 'ping 172.16.100.%d ok' % start
prlock.release()
self.ns +=1
start+=self.inter
#print '线程%d结束, 此线程共获得 %d 个在线数据' %(self.nums,self.ns)
numlock.acquire()
zxs+=self.ns
numlock.release()
def stop(self):
self.thread_stop=True
def pingt():
s=254
r=s-1
threads=[]
for i in range(1,s):
t=pings(i,r)
threads.append(t)
for i in threads:
i.start()
for i in threads:
i.join()
global zxs
print zxs,'个ip在线'
if __name__=='__main__':
pingt()
```
$ cat /etc/rc.local
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
exit 0
最近的评论