【问题标题】:Pinging first available host in network subnetsping 网络子网中的第一个可用主机
【发布时间】:2010-12-25 09:13:21
【问题描述】:

我用 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 扫描仪)来完成这项任务,但我对编写自己的脚本很感兴趣。

【问题讨论】:

  • 你考虑过使用nmap吗?另外,请小心,因为虽然您所做的事情可能是无辜的,但它可能看起来像是在工作。
  • +1 嘎呼。 @rohanbk,还要检查您学校的网络使用政策。我在一所大学工作了几年,这会违反使用政策并让你被禁止(甚至可能被开除)。

标签: python networking subnet


【解决方案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()

【讨论】:

    【解决方案2】:

    由于您知道在开始运行时获得了多少线程,因此您可以定期检查当前运行的线程数以查看 nowThreadCount

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

    【讨论】:

      猜你喜欢
      • 2011-04-11
      • 2015-03-17
      • 1970-01-01
      • 2021-05-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多