27
Discover all ip address connected to your network (Network scanner)
Hello, today we are going to code a network scanner to scan our network and find the ip address connected we are going to use the ping command ... follow
In my case i am using linux ..
This script is going to show all connected devices on our network
First you have to create a file with python extension (ends with .py)
i will name it scan.py
import sh
from subprocess import Popen, PIPE
import re
First, we are going to create a function to get the Mac address of the ip we have:
def getMac(ip):
pid = Popen(["arp", "-n", ip], stdout=PIPE)
s = pid.communicate()[0]
a=re.search(r"(([a-f\d]{1,2}\:){5}[a-f\d]{1,2})", str(s))
if a ==None:
b=('this')
return b
else:
mac = a.groups()[0]
return mac
This function :
- Uses the command arp -n to get the information of the ip.
- Then it uses the regex to get the mac .
- The if statement is for checking if the ip is our ip
On Linux operating systems, the arp command manipulates or shows the kernel's IPv4 network neighbour cache. It can add entries to the table, delete one, or display the current content. ARP stands for Address Resolution Protocol, which is used to find the address of a network neighbor for a given IPv4 address.
Now we are going to do a for loop to generate some ip address
for num in range(1,256):
ip = "192.168.1."+str(num)
try:
sh.ping(ip, "-c 1",_out="/dev/null")
mac=getMac(ip)
print ("PING ",ip , "OK ",mac)
except sh.ErrorReturnCode_1:
#print ("PING ", ip, "FAILED")
pass
This loop:
- Generates an ip
- Then it runs the command ping to check if this ip is alive
- Then it gets the mac address
- After that it print the ip if everything is okay
Ping is a computer network administration software utility used to test if an host is reachable on an Internet Protocol network.you can do it on all operating systems that have network access, including most embedded network administration software.
NOTE : the ip variable i used can be different of yours first you have to check what types of ip address your router uses.
To check what ip address your router uses you can simply run the command :
ifconfig
and you will see an output like that :
inet 192.168.1.6 netmask 255.255.255.0 broadcast 192.168.1.255
in my case you can see that my ip is 192.168.1.6 so all others ip will be start with 192.168.1. that's why i used 192.168.1. to generate ip address.
import sh
from subprocess import Popen, PIPE
import re
def getMac(ip):
pid = Popen(["arp", "-n", ip], stdout=PIPE)
s = pid.communicate()[0]
a=re.search(r"(([a-f\d]{1,2}:){5}[a-f\d]{1,2})", str(s))
if a ==None:
b=('this')
return b
else:
mac = a.groups()[0]
return mac
for num in range(1,256):
ip = "192.168.1."+str(num)
try:
sh.ping(ip, "-c 1",_out="/dev/null")
mac=getMac(ip)
print ("PING ",ip , "OK ",mac)
except sh.ErrorReturnCode_1:
#print ("PING ", ip, "FAILED")
pass
Enter fullscreen mode
Exit fullscreen mode
open the folder contains the script on a terminal and run:
python3 scan.py
PING 192.168.1.1 OK 00:00:00:00:00
PING 192.168.1.2 OK 00:00:00:00:00
PING 192.168.1.5 OK 00:00:00:00:00
PING 192.168.1.10 OK 00:00:00:00:00
PING 192.168.1.14 OK 00:00:00:00:00
You will see mac address instead of 00:00:00:00:00
Don't forget to share this post..
Happy coding ...
27