Answer the question
In order to leave comments, you need to log in
How to scan a local network?
Hello.
There is a program:
Advanced ip scanner
Task:
Similar to this program, scan the local network and get devices connected to it using Python. All the solutions that I found, for some reason, do not find the devices that this program finds. The local network is organized using a router.
Current copy-paste code (python3):
import os
import time
from subprocess import Popen
import sys
devnull = open(os.devnull, 'wb')
p = []
active = 0
unknown = 0
passive = 0
for f_part in range(0,2):
for s_part in range(0,256):
ip = "192.168." + str(f_part) + "." + str(s_part)
if os.name == 'nt':
CREATE_NO_WINDOW = 0x08000000
p.append((ip, Popen(['ping', '-c', '3', ip], stdout=devnull, creationflags=CREATE_NO_WINDOW)))
else:
p.append((ip, Popen(['ping', '-c', '3', ip], stdout=devnull)))
while p:
for i, (ip, proc) in enumerate(p[:]):
if proc.poll() is not None:
p.remove((ip, proc))
if proc.returncode == 0:
print('%s Active' % ip)
active = active + 1
elif proc.returncode == 2:
print('%s Unknown' % ip)
active = unknown + 1
else:
print('%s Passive' % ip)
passive = passive + 1
time.sleep(.04)
devnull.close()
print("Your system: ",os.name)
print("Active [ ",active," ]")
print("Passive [ ",passive," ]")
print("Unknown [ ",unknown," ]")
input("")
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question