1

I am trying to print all the Live IPs that connected to my Network in the fastest way. I tried to ping in a for loop, but it is very slow:

def PingTry(host):
    ping = subprocess.Popen(["ping", host], stdout = subprocess.PIPE, stderr = subprocess.PIPE)
    out, error = ping.communicate()
    print out #This will show me the ping result, I can check the content and see if the host replyed or not

As I said, it is very slow (I need to do this 255 times).

I tried to connect to it using TCP connection with port 80:

import socket
IP = '192.168.1.100'
PORT = 80
tcpsoc = socket(AF_INET, SOCK_STREAM)
tcpsoc.listen(SOMAXCONN)
try:
    tcpsoc.bind(ADDR)
except Exception,ex:
    print "host is down!"

But still, it doesn't work for this IP, although it is working for the router IP

Is there a way to getting all the live IPs faster?

phihag
  • 278,196
  • 72
  • 453
  • 469
albeck
  • 510
  • 1
  • 7
  • 16
  • the reason ping is slow is because you are actually waiting for responses (pong) from the interface. you can instead just generate a list of connected interfaces with arp -a. – John Faulkner Jul 07 '13 at 16:19
  • I tried using arp -a, but I only get the router's IP address in the interface list, although my phone and my laptop are connected to the network too – albeck Jul 07 '13 at 17:04
  • What operating system are you on? Can you be certain that you have superuser (or at least `CAP_NET_ADMIN`) privileges? – phihag Jul 07 '13 at 18:08
  • @phihag I'm on Windows 7 64 Bit and yes, I have Admin privileges. – albeck Jul 07 '13 at 18:35

4 Answers4

0

Ping is the appropriate way to ask a machine if it has claimed an IP address. It is slow because (depending on your platform) the ping timeout is usually a second. You can speed it up by reducing the timeout or by using the threading module to send multiple pings at the same time.

You implement a ping in python directly: Pinging servers in Python

Alternatively, use a tool like nmap.

Community
  • 1
  • 1
Seth
  • 45,033
  • 10
  • 85
  • 120
  • I don't want to use a program to get all the connected IPs, I want to do so in Python. I already looked at this thread before I asked this question, although I setted the timeout to 1 sec, or 0.5 sec – albeck Jul 07 '13 at 17:06
0

You can call ping in parallel with a multiprocessing Pool:

from multiprocessing.pool import ThreadPool

def ping(host):
    ping = subprocess.Popen(['ping', '-w', '500', host],
                            stdout = subprocess.PIPE, stderr = subprocess.PIPE)
    out, error = ping.communicate()
    return (out, error)
addresses = ['192.168.0.1', '192.168.0.2',] # etc.
pool = Pool(10) # Increase number to increase speed and resource consumption
ping_results = pool.map(ping)
print(ping_results)

pool.close()
pool.join()

Alternatively, use ctypes to call ICMPSendEcho in the ping method.

phihag
  • 278,196
  • 72
  • 453
  • 469
0

I would use different approach, the Router usually is holding all the active IP addresses in its ARP table, assuming this is a professional network and any basic professional Router is answering SNMP requests, use Python and some SNMP package (like: PySNMP) and get the list from it.

Hint: ARP Table OID = 1.3.6.1.2.1.4.22 (ipNetToMediaTable)

After getting that list, I would double check it using ICMP(ping) or any other responsive protocol.

tal.tzf
  • 128
  • 7
0

scapy provides a function. I am not sure whether this is multithreaded. If not, just call the function for different ranges in a multithreaded way.

>>> arping("192.168.1.*")
mkind
  • 2,015
  • 2
  • 20
  • 25