2

I want to print all IP addresses of local network on the Python console. Is there a way?

I've written a code for this problem but it's too slow, I need faster code.

import subprocess
ip="192.168.1."
list=[]
for i in range(1,255,1):
      ipn=ip+str(i)
      s = subprocess.check_output(["ping",ipn])

      if ("TTL" in str(s)):
      list.append(ipn)

print("ip list")
for j in list:
     print(j)
  • 1
    Does this answer your question? [List of IP addresses/hostnames from local network in Python](https://stackoverflow.com/questions/207234/list-of-ip-addresses-hostnames-from-local-network-in-python) – Adam.Er8 Mar 12 '20 at 08:41

1 Answers1

0

You could use subprocess and arp -a :


import subprocess
cmd = subprocess.run(["arp", "-a"], capture_output=True)
a = str(cmd).split(",")
b = a[3].replace("stdout=b'", "").replace("\\n", "\n")
#print(b)
c=b.split("on wlan0")
for x in c:
    print("Device: ",x)

You may have to adapt the split statement, htey depend on the language your console is using (language means native language e.g. English, French)

MoPaMo
  • 517
  • 6
  • 24