title: Fastest pinging method via python?
body: <p>I'm looking for the fastest pinging method via python.  I need to ping over 100,000 servers and my current procedure below takes approximately 85 minutes to complete.  I've read small snippets about scapy, along with general ICMP and 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.  I also tried a code snippet for <code>scapy</code>, but got an error:</p>
  
  <pre class="lang-none prettyprint-override"><code>OSError: Windows native L3 Raw sockets are only usable as administrator !
  Install 'Winpcap/Npcap to workaround !
  </code></pre>
  
  <p>So I'm admittedly looking for code snippets I can test at home or ways around that error from more experienced persons</p>
  
  <p>To prove I've tried, here are some related posts, as well as my current code</p>
  
  <p>Current code:</p>
  
  <pre><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&lt;len(hosts_list):
      print(number)
      if len(hosts_list)&gt;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)&gt;(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)&gt;(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"')
  </code></pre>
  
  <p>This does exactly what I need, however, it does not do it quickly enough.</p>
  
  <p>I've tried the very small snippet:</p>
  
  <pre><code>from scapy.all import *
  packets = IP(dst=["www.google.com", "www.google.fr"])/ICMP()
  results = sr(packets)
  </code></pre>
  
  <p>resulting in <code>gaierror: [Errno 11001] getaddrinfo failed</code></p>
  
  <p>I've also tried:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>resulting in:</p>
  
  <pre><code>OSError: Windows native L3 Raw sockets are only usable as administrator !
  Install Winpcap/Npcap to workaround !
  </code></pre>
  
  <p>A few links I looked at but could not garner a solid answer from:</p>
  
  <p><a href="https://stackoverflow.com/questions/316866/ping-a-site-in-python">Ping a site in Python?</a></p>
  
  <p><a href="https://stackoverflow.com/questions/56806072/fastest-way-to-ping-a-host-in-python">Fastest way to ping a host in python?</a></p>