How can i find all IP addresses in local network using Python codes?

Viewed 196

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 Answers

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)

Related