0

Hi I am writing a script that check a network to see if certen people are connected by getting devices mac addresses from arp, I am roughly 0.5% through and I have a problem. I currently have to ping all ip addresses 192.168.1.2-254. Now this is incredibly slow is there a quicker way:

Code:

inport os
ipcount=2
ipup=[]

while ipcount<254:
 response=os.system("ping -c 1 192.168.1." + str(ipcount))
 if response == 0:
   ipup.append("192.168.1."+str(ipcount))
 ipcount=ipcount+1
Robert
  • 10,403
  • 14
  • 67
  • 117
Ioan Loosley
  • 119
  • 1
  • 1
  • 11
  • You might wanna take a look to this: http://stackoverflow.com/questions/12101239/multiple-ping-script-in-python (it uses `subprocess`, which spawns a separate process, therefore running your pings in parallel... I don't think `os.system` does that) – Savir Apr 14 '14 at 16:48
  • 1
    It is likely not your code that is slow (per se). It is the fact that you have to wait for a response, and you are only pinging one person at a time, so they all have to run in series. You should consider pinging multiple people in parallel, since the response time is what is slowing you down. – Cory Kramer Apr 14 '14 at 16:48
  • Yeah that what i ment – Ioan Loosley Apr 14 '14 at 16:49
  • [this answer](http://stackoverflow.com/a/12102040/4279) shows how to run multiple ping subprocesses in parallel – jfs Apr 14 '14 at 17:09

1 Answers1

1

Ping in parallel in many threads. Most time is spent waiting for response, so multiple threads can really speed up the process. Probably you'd like to create threads in advance and maybe reuse them because thread creation is pretty slow.

EDIT:

another way to do this is to implement ping using async network IO.

Andriy Tylychko
  • 15,967
  • 6
  • 64
  • 112