简体   繁体   English

通过python最快的ping方法?

[英]Fastest pinging method via python?

I'm looking for the fastest pinging method via python.我正在寻找通过 python 最快的 ping 方法。 I need to ping over 100,000 servers and my current procedure below takes approximately 85 minutes to complete.我需要 ping 100,000 多台服务器,而我当前的以下过程大约需要 85 分钟才能完成。 I've read small snippets about scapy, along with general ICMP and python ping.我已经阅读了关于 scapy 的小片段,以及一般的 ICMP 和 python ping。 I need to know a definitive method, or at least a solid way to test, which is the fastest.我需要知道一种确定的方法,或者至少是一种可靠的测试方法,这是最快的。 I cannot test python - ping from work as it is not an approved package.我无法测试 python - 从工作中 ping,因为它不是经过批准的包。 I also tried a code snippet for scapy , but got an error:我还尝试了scapy的代码片段,但出现错误:

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

So I'm admittedly looking for code snippets I can test at home or ways around that error from more experienced persons所以我承认我正在寻找我可以在家里测试的代码片段或从更有经验的人那里解决这个错误的方法

To prove I've tried, here are some related posts, as well as my current code为了证明我已经尝试过,这里有一些相关的帖子,以及我当前的代码

Current code:当前代码:

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"')

This does exactly what I need, however, it does not do it quickly enough.这正是我所需要的,但是,它做得不够快。

I've tried the very small snippet:我试过非常小的片段:

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

resulting in gaierror: [Errno 11001] getaddrinfo failed导致gaierror: [Errno 11001] getaddrinfo failed

I've also tried:我也试过:

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

resulting in:导致:

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

A few links I looked at but could not garner a solid answer from:我查看了一些链接,但无法从以下链接中获得可靠的答案:

Ping a site in Python? 在 Python 中 Ping 站点?

Fastest way to ping a host in python? 在python中ping主机的最快方法?

This only solves the Python part.这只解决了Python部分。 The comments are very right.评论说得很对。

OSError: Windows native L3 Raw sockets are only usable as administrator ! OSError:Windows 原生 L3 原始套接字只能以管理员身份使用! Install Winpcap/Npcap to workaround !安装 Winpcap/Npcap 来解决!

I find this pretty damn explicit.我觉得这非常明显。 If you follow's Scapy documentation for windows it says you need to install Npcap .如果您遵循 Windows 的 Scapy 文档,它会说您需要安装Npcap https://nmap.org/npcap/ https://nmap.org/npcap/

Other than that,除此之外,

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

Is likely the cleanest way to go.可能是最干净的方式。 Works on my machine.. make sure you're using the latest development version from GitHub (unzip it and install it via python setup.py install ).在我的机器上工作.. 确保您使用的是来自 GitHub最新开发版本(解压缩并通过python setup.py install )。

If you are using the latest version , you might even want to turn on threaded=True in sr() to send and receive packets on two threads, as pointed out by the comments.如果您使用的是最新版本,您甚至可能希望在sr()打开threaded=True以在两个线程上发送和接收数据包,如评论所指出的。 You might also want to use prn and store=False to not store the answers (100k is a lot)您可能还想使用prnstore=False来不存储答案(100k 很多)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM