-1

¿Which is the fastest way to check if a host is up or down using python?.

I need to perform ~ 120K pings so it doesn't matter if there is a few of false negatives.

I have 2 different approaches:

  • ICMP requests with scapy
  • Execute os.system("ping -c 1 " . ip)
jfleach
  • 501
  • 1
  • 8
  • 21
A.Vadillo
  • 43
  • 7
  • run both version and check its time - you will see which one is faster. – furas Jun 28 '19 at 11:45
  • why not use something specifically designed for this? I've used [`nmap`](https://nmap.org/) for years but there might be better tools now. it has many non-obvious ways of "pinging" --- i.e. not just ICMP pings – Sam Mason Jun 29 '19 at 15:49

3 Answers3

1

Following does a ping and assigns state to host_state Boolean.

import os

host_ip = "127.0.0.1" # replace with IP in question
host_state  = True if os.system("ping -c 1 " + host_ip) is 0 else False
print(host_state)

With "-c 1" it'll ping only once and return

hsen
  • 11
  • 3
1

My guess would be Scapy, because you can send all packets to all hosts at once.

from scapy.all import *
packets = IP(dst=["www.google.com", "www.google.fr"])/ICMP()
results = sr(packets)
Cukic0d
  • 5,111
  • 2
  • 19
  • 48
0

According to your question, I'd imagine the best choice would be to go for multiprocessing instead of trying to shorten the ping's time. Here's a nice place to get you started.

rmssoares
  • 175
  • 1
  • 11