2

我用 Python 编写了一个小脚本,它 ping 我学校无线网络的所有子网,并打印出连接到网络每个子网的计算机的 IP 地址和主机名。我目前的设置是我依靠创建线程来处理每个 ping 请求。

from threading import Thread
import subprocess
from Queue import Queue
import time
import socket

#wraps system ping command
def ping(i, q):
    """Pings address"""
    while True:
        ip = q.get()
        #print "Thread %s: Pinging %s" % (i, ip)
        result = subprocess.call("ping -n 1 %s" % ip, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
        #Avoid flooding the network with ping requests
        time.sleep(3)
        if result == 0:

            try:
                hostname=socket.gethostbyaddr(ip)
                print "%s (%s): alive" % (ip,hostname[0])
            except:
                print "%s: alive"%ip
        q.task_done()

num_threads = 100
queue = Queue()
addresses=[]
#Append all possible IP addresses from all subnets on wireless network
for i in range(1,255):
    for j in range(1,254):
        addresses.append('128.119.%s.%s'%(str(i),str(j)))
#Spawn thread pool
for i in range(num_threads):
    worker = Thread(target=ping, args=(i, queue))
    worker.setDaemon(True)
    worker.start()
#Place work in queue
for ip in addresses:
    queue.put(ip)
#Wait until worker threads are done to exit    
queue.join()

但是,我想修改我的脚本,以便它只查找子网中的第一个可用主机。这意味着假设我有以下子网 (128.119.177.0/24) 并且第一个可用主机是 128.119.177.20。我希望我的脚本在我成功联系 128.119.177.20 后停止 ping 128.119.177.0/24 中的剩余主机。我想对我网络上的每个子网(128.119.0.1 - 128.119.255.254)重复一遍。鉴于我目前的设置,进行此更改的最佳行动方案是什么?我正在考虑做类似队列列表(其中每个队列为其中一个子网保存 255 个 IP 地址)并让一个线程处理每个队列(除非我可以在 Windows 上的 Python 中生成的线程数有限制) .

编辑:我玩过 nmap(和 Angry IP 扫描仪)来完成这项任务,但我对编写自己的脚本很感兴趣。

4

2 回答 2

1

最简单的事情是让一个线程在整个子网中工作并在找到主机时退出。

未经测试

from Queue import Queue
import time
import socket

#wraps system ping command
def ping(i, q):
    """Pings address"""
    while True:
        subnet = q.get()
        # each IP addresse in subnet 
        for ip in (subnet=str(x) for x in range(1,254)):
            #print "Thread %s: Pinging %s" % (i, ip)
            result = subprocess.call("ping -n 1 %s" % ip, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
            #Avoid flooding the network with ping requests
            time.sleep(3)
            if result == 0:

                try:
                    hostname=socket.gethostbyaddr(ip)
                    print "%s (%s): alive" % (ip,hostname[0]  
                except:
                    print "%s: alive"%ip
                break
        q.task_done()

num_threads = 100
queue = Queue()

#Put all possible subnets on wireless network into a queue
for i in range(1,255):
    queue.put('128.119.%s.'%i)

#Spawn thread pool
for i in range(num_threads):
    worker = Thread(target=ping, args=(i, queue))
    worker.setDaemon(True)
    worker.start()

#Wait until worker threads are done to exit    
queue.join()
于 2009-12-11T00:08:49.963 回答
0

由于您知道在运行开始时获得了多少线程,因此您可以定期检查当前运行的线程数是否 nowThreadCount < startThreadCount。如果为真,则终止当前线程。

PS:最简单的方法也是清除队列对象,但我在文档中找不到。

于 2009-12-11T00:27:52.547 回答