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

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

If you're managing a local network or exploring ethical network testing, it's helpful to know which devices are currently active. While traditional ARP-based scans work well, they can miss certain devices or fail in segmented networks. Instead, you can perform a TCP SYN scan on commonly used ports to identify active systems.

In this tutorial, we’ll create a Python script that sends TCP SYN packets using Scapy and identifies which devices respond — revealing their IP address, open port, and if possible, hostname.

โš™๏ธ Prerequisites

Install the required libraries first:

pip install scapy netaddr

๐Ÿ’ป Python Code: TCP SYN Scanner for LAN

from scapy.all import *
from concurrent.futures import ThreadPoolExecutor
import socket
from netaddr import IPNetwork

COMMON_PORTS = [22, 80, 135, 139, 443, 445, 3389, 554]

def syn_scan(ip, port):
    pkt = IP(dst=ip)/TCP(dport=port, flags="S")
    resp = sr1(pkt, timeout=1, verbose=0)
    if resp and resp.haslayer(TCP) and resp.getlayer(TCP).flags == 0x12:
        send(IP(dst=ip)/TCP(dport=port, flags="R"), verbose=0)  # RST to close
        return True
    return False

def scan_ip(ip):
    for port in COMMON_PORTS:
        if syn_scan(ip, port):
            try:
                hostname = socket.gethostbyaddr(ip)[0]
            except:
                hostname = "Unknown"
            return {"ip": ip, "port": port, "hostname": hostname}
    return None

def scan_subnet(subnet):
    ip_list = [str(ip) for ip in IPNetwork(subnet).iter_hosts()]
    results = []
    with ThreadPoolExecutor(max_workers=100) as executor:
        for res in executor.map(scan_ip, ip_list):
            if res:
                results.append(res)
    return results

if __name__ == "__main__":
    subnet = "172.16.20.0/24"
    print(f"Scanning subnet {subnet} with common ports...\n")
    devices = scan_subnet(subnet)
    print("Detected devices (accepted TCP SYN):\n")
    for d in devices:
        print(f"- IP: {d['ip']} | Port: {d['port']} | Hostname: {d['hostname']}")

๐Ÿ“Œ What This Script Does

๐Ÿ” Why Use TCP SYN Instead of Ping?

๐Ÿงช Sample Output

๐Ÿš€ Potential Improvements

โš ๏ธ Disclaimer: For Educational Use Only

๐Ÿ“Œ Legal Notice:
The script and techniques demonstrated in this article are intended solely for educational purposes, ethical network testing, and system administration.
Do not use this script to scan any network or device without explicit authorization. Unauthorized scanning may violate cybersecurity laws and result in legal consequences.

โœ… Always use such tools ethically and within the boundaries 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 Devices in Your Local Network Using Python and ARP

Learn how to use Python and Scapy to scan your local network, detect connected devices by IP and hostname, and quickly discover active hosts in your LAN.

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