Python security #1 : Network scanner : Discover all ip address on your network

Code network scanner to get all the ip address connected on your network with python.

Code network scanner to get all the ip address connected on your network with python.


 

Introduction :

 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

Start coding :

Setup the script file :

Create file :

First you have to create a file with python extension (ends with .py)

i will name it scan.py

Import required modules:

import sh    
from subprocess import Popen, PIPE
import re

Function to get the Mac address of the ip :

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 :

  1. Uses the command arp -n to get the information of the ip.
  2. Then it uses the regex to get the mac .
  3. 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.

Generate some ip 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:

  1. Generates an ip
  2. Then it runs the command ping to check if this ip is alive
  3. Then it gets the mac address
  4. 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 :

wlo1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.1.6  netmask 255.255.255.0  broadcast 192.168.1.255
        inet6 fd7c:11cb:b823:5200:d4dc:1fd4:23e2:b95a  prefixlen 64  scopeid 0x0<global>
        inet6 fe80::fb2d:ea55:3964:dd3b  prefixlen 64  scopeid 0x20<link>
        inet6 fd7c:11cb:b823:5200:9d6:45ba:f7dc:4bf0  prefixlen 64  scopeid 0x0<

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.

Full code :

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 

Run the script :

open the folder contains the script on a terminal and run:

python3 scan.py

OUTPUT :

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 ...


You may like these posts