通过python最快的ping方法?

问题描述 投票:0回答:1

我正在寻找通过python最快的ping方法。我需要对100,000台以上的服务器执行ping操作,下面的当前过程大约需要85分钟才能完成。我已经阅读了有关船尾的小片段,以及常规的ICMP和python ping。我需要知道一种确定的方法,或者至少是一种可靠的测试方法,这是最快的。我无法测试python-无法正常工作,因为它不是经过批准的软件包。我也尝试了scapy的代码段,但出现了错误:

OSError: Windows native L3 Raw sockets are only usable as administrator !
Install 'Winpcap/Npcap to workaround !

因此,我想找的是我可以在家测试的代码段,或者是经验丰富的人解决该错误的方法

为了证明我已经尝试过,这里有一些相关的文章以及我当前的代码

当前代码:

import pandas as pd
import subprocess
import threading
raw_list = []
raw_list2 = []
def ping(host):
    raw_list.append(host+ ' '+ str((subprocess.run('ping -n 3 -w 800 '+host).returncode)))
with open(r"FILEPATH", "r") as server_list_file:
    hosts = server_list_file.read()
    hosts_list = hosts.split('\n')
num_threads = 100
num_threads2 = 10
num_threads3 = 1
number = 0
while number<len(hosts_list):
    print(number)
    if len(hosts_list)>number+num_threads:
        for i in range(num_threads):
            t = threading.Thread(target=ping, args=(hosts_list[number+i],))
            t.start()
        t.join()
        number = number + num_threads
    elif len(hosts_list)>(number+num_threads2):
        for i in range(num_threads2):
            t = threading.Thread(target=ping, args=(hosts_list[number+i],))
            t.start()
        t.join()
        number = number + num_threads2
    elif len(hosts_list)>(number+num_threads3-1):
        for i in range(num_threads3):
            t = threading.Thread(target=ping, args=(hosts_list[number+i],))
            t.start()
        t.join()
        number = number + num_threads3
    else:
        number = number+1
for x in range(len(raw_list)):
    if(raw_list[x][-1] == '0'):
        raw_list2.append(raw_list[x][0:-2])
to_csv_list = pd.DataFrame(raw_list2)
to_csv_list.to_csv('ServersCsv.csv', index = False, header = False)
to_csv_list.to_csv(r'ANOTHERFILEPATH', index = False, header = False) 
subprocess.call(r'C:\ProgramData\Anaconda3\python.exe "A_PROGRAM_THAT_INSERTS_INTO_SQL"')

这恰好满足了我的需要,但是做起来还不够快。

我尝试了非常小的代码段:

from scapy.all import *
packets = IP(dst=["www.google.com", "www.google.fr"])/ICMP()
results = sr(packets)

导致gaierror: [Errno 11001] getaddrinfo failed

我也尝试过:

TIMEOUT = 2
conf.verb = 0
packet = IP("ASERVERNAME", ttl=20)/ICMP()
reply = sr1(packet, timeout=TIMEOUT)
if not (reply is None):
     print(reply.dst + "is online")
else:
     print("Timeout waiting for %s") % packet[IP].dst

导致:

OSError: Windows native L3 Raw sockets are only usable as administrator !
Install Winpcap/Npcap to workaround !

我查看了一些链接,但无法从以下位置获得确切的答案:

Ping a site in Python?

Fastest way to ping a host in python?

python python-3.x multithreading performance ping
1个回答
0
投票

OSError:Windows本机L3 Raw套接字只能以管理员身份使用!安装Winpcap / Npcap即可解决!

我发现这很明确。如果您是follow's Scapy documentation for windows,则说您需要安装Npcaphttps://nmap.org/npcap/

除此之外,

packets = IP(dst=["www.google.com", "www.google.fr"])/ICMP()
results = sr(packets)

可能是最干净的方法。在我的机器上工作..确保您使用的是latest development version from GitHub(将其解压缩并通过python setup.py install安装):发行速度通常很慢。

© www.soinside.com 2019 - 2024. All rights reserved.