How to Scan Devices in Your Local Network Using Python and ARP

Light - June 9, 2025, 10:13 a.m.

In local networks, especially for system administrators, ethical hackers, or home network enthusiasts, it’s often important to know what devices are currently connected. Fortunately, Python combined with Scapy makes this process incredibly easy and powerful.

In this tutorial, we’ll show you how to build a simple ARP scanner in Python that can scan a subnet (like 172.16.20.0/24) and return the IP and hostname of each connected device.

⚙️ Requirements

You’ll need to install Scapy if you haven't already:

pip install scapy

Also, run the script with sudo/root permission since ARP scanning requires raw socket access.

💻 Python Code: ARP-based Network Scanner

import ipaddress
import scapy.all as scapy
import socket

def get_device_name(ip):
    try:
        return socket.gethostbyaddr(ip)[0]
    except socket.herror:
        return "Unknown"

def scan(ip_range):
    arp_request = scapy.ARP(pdst=ip_range)
    broadcast = scapy.Ether(dst="ff:ff:ff:ff:ff:ff")
    packet = broadcast / arp_request
    answered = scapy.srp(packet, timeout=2, verbose=0)[0]

    devices = []
    for sent, received in answered:
        ip = received.psrc
        hostname = get_device_name(ip)
        devices.append({"ip": ip, "hostname": hostname})
    
    return devices

if __name__ == "__main__":
    # subnet = "192.168.1.0/24"
    subnet = "172.16.20.0/24"
    print(f"Scanning subnet: {subnet}...\n")

    results = scan(subnet)
    print("Found devices:")
    for device in results:
        print(f"- IP: {device['ip']} | Hostname: {device['hostname']}")

📌 What This Script Does

Example Output:

🔒 Security Note

This script is safe and uses standard ARP discovery, similar to what tools like arp-scan or nmap -sn do. Make sure you only use it on networks you own or have permission to test.


🧩 Next Steps

You can extend this scanner to:

⚠️ Disclaimer: For Educational Use Only

📌 Legal Notice:
The script and techniques shared in this article are intended solely for educational purposes, ethical hacking training, and internal network administration.
Do not use this code to scan, probe, or interfere with any network or device that you do not own or explicitly have permission to test.

❌ Unauthorized scanning or probing of networks may violate cybersecurity laws and result in serious legal consequences.

✅ Always use these tools and techniques responsibly, ethically, and within the bounds of the law.

Read Similars

Automated LAN Scanning with Nmap and Python – Exporting Results to JSON

Learn how to automatically scan your local network using Nmap with Python, detect active devices and operating systems, and export scan results to JSON format.

5 min read

How to Fingerprint a Device in Your Local Network Using Python

Learn how to fingerprint LAN devices using Python, Scapy, and Impacket. Collect OS info, open ports, HTTP server data, and SMB banners with a single script.

5 min read

How to Scan Local Network for Active Devices Using Python and TCP SYN

Learn how to write a Python script to scan local networks using TCP SYN packets, detect live hosts, and discover connected devices with IPs and hostnames.

5 min read

Testing Common Django Security Vulnerabilities with Python Scripts

Learn how to test common Django security vulnerabilities like XSS, CSRF, SQL Injection, and more using Python scripts.

5 min read

Automate Website Security Scans: Headers, SSL, Ports & Vulns

Scan your website for security headers, SSL issues, open ports, and vulnerabilities like SQL Injection or XSS using a Python script and generate a HTML.

5 min read