🎁 New User? Get 20% off your first purchase with code NEWUSER20 Register Now →
Menu

Categories

Linux Network Scanning with Python: Ping Sweep & Port Scan (2026 Guide)

Linux Network Scanning with Python: Ping Sweep & Port Scan (2026 Guide)

Network scanning is a fundamental skill for both sysadmins and security professionals. Whether you're inventorying devices on your network, checking which services are running on a server, or performing a security assessment, you need reliable scanning tools.

While nmap is the gold standard, it's not always available — many minimal server installations don't include it, and installing additional packages on production systems requires change management approval. dargslan-net-scanner provides essential network scanning capabilities using only Python's standard library.

Installing dargslan-net-scanner

pip install dargslan-net-scanner

# Or install the complete 15-tool toolkit
pip install dargslan-toolkit

CLI Usage

# Ping sweep — discover live hosts
dargslan-netscan ping 192.168.1.0/24

# Port scan a specific host
dargslan-netscan ports 192.168.1.1

# Scan specific ports only
dargslan-netscan ports 192.168.1.1 -p 22,80,443,3306,5432

# Scan a port range
dargslan-netscan ports 192.168.1.1 -p 1-1024

# Network sweep with port check
dargslan-netscan sweep 10.0.0.0/24

# Host information
dargslan-netscan info 192.168.1.1

# JSON output
dargslan-netscan json 192.168.1.0/24

Python API

from dargslan_net_scanner import NetScanner

ns = NetScanner(timeout=2)

# Discover live hosts on a subnet
alive = ns.ping_sweep("192.168.1.0/24")
print(f"Found {len(alive)} live hosts:")
for host in alive:
    print(f"  {host['ip']:16s} RTT: {host['rtt_ms']:.1f}ms")

# Port scan a specific host
ports = ns.port_scan("192.168.1.1")
for p in ports:
    if p['open']:
        service = p.get('service', 'unknown')
        print(f"  Port {p['port']:>5}: OPEN ({service})")

# Scan specific ports
ports = ns.port_scan("192.168.1.1", ports=[22, 80, 443, 3306, 5432, 6379, 8080])
open_ports = [p for p in ports if p['open']]
print(f"Open ports: {', '.join(str(p['port']) for p in open_ports)}")

# Host information
info = ns.host_info("192.168.1.1")
print(f"Hostname: {info.get('hostname', 'N/A')}")
print(f"Reachable: {info['reachable']}")

# Full network report
report = ns.network_report("192.168.1.0/24", common_ports=True)
for host in report:
    ports_str = ", ".join(str(p) for p in host['open_ports'])
    print(f"  {host['ip']:16s} Ports: {ports_str or 'none'}")

Understanding Network Scanning

Ping Sweep (Host Discovery)

A ping sweep sends ICMP echo requests to every IP in a subnet to find which hosts are alive. It's the fastest way to inventory a network.

# Manual ping sweep
for ip in $(seq 1 254); do
    ping -c 1 -W 1 192.168.1.$ip &>/dev/null && echo "192.168.1.$ip is alive" &
done
wait

# Using nmap
nmap -sn 192.168.1.0/24

# Using fping (faster)
fping -a -g 192.168.1.0/24 2>/dev/null

Port Scanning

Port scanning connects to TCP ports to determine which services are listening. Each open port represents a running service.

# Using netcat
nc -zv 192.168.1.1 22
nc -zv 192.168.1.1 1-1024

# Using nmap
nmap -p 22,80,443 192.168.1.1
nmap -p- 192.168.1.1           # All 65535 ports
nmap -sV 192.168.1.1           # Service version detection

# Using bash
(echo >/dev/tcp/192.168.1.1/22) 2>/dev/null && echo "Port 22 open"

Common Service Ports

PortServiceNotes
22SSHRemote access — restrict with keys
25SMTPEmail sending — often targeted by spam
53DNSDomain resolution — UDP and TCP
80/443HTTP/HTTPSWeb traffic
3306MySQLShould NOT be externally exposed
5432PostgreSQLShould NOT be externally exposed
6379RedisShould NOT be externally exposed
8080HTTP AltDevelopment servers, proxies
27017MongoDBShould NOT be externally exposed

Network Troubleshooting Commands

# IP address and interface info
ip addr show
ip link show
ip route show

# DNS resolution
dig example.com
nslookup example.com
host example.com

# Traceroute
traceroute example.com
mtr example.com          # Interactive traceroute + ping

# ARP table (local network)
ip neigh show
arp -a

# Connection tracking
ss -tnp                  # TCP connections
ss -tlnp                 # Listening ports
conntrack -L             # Connection tracking table

# Bandwidth testing
iperf3 -c server-ip      # Client mode
iperf3 -s                # Server mode

Automated Network Monitoring

#!/usr/bin/env python3
# /opt/scripts/network-scan.py
from dargslan_net_scanner import NetScanner

EXPECTED_HOSTS = ["192.168.1.1", "192.168.1.10", "192.168.1.20"]
CRITICAL_PORTS = {
    "192.168.1.10": [22, 80, 443],
    "192.168.1.20": [22, 5432],
}

ns = NetScanner(timeout=3)

# Check expected hosts are alive
alive_ips = [h['ip'] for h in ns.ping_sweep("192.168.1.0/24")]
for host in EXPECTED_HOSTS:
    if host not in alive_ips:
        print(f"ALERT: {host} is DOWN!")

# Check critical ports
for host, ports in CRITICAL_PORTS.items():
    results = ns.port_scan(host, ports=ports)
    for p in results:
        if not p['open']:
            print(f"ALERT: {host}:{p['port']} is CLOSED!")

🌐 Master Network Security

Our cybersecurity eBooks cover network scanning, penetration testing, firewall configuration, IDS/IPS, and enterprise network security architecture.

Browse Security Books →

Network scanning is your eyes on the network. dargslan-net-scanner provides essential ping sweep and port scanning capabilities without requiring nmap or any external tools — just Python.

Install now: pip install dargslan-net-scanner — or get all 15 tools: pip install dargslan-toolkit

Download our free Network Scanner Cheat Sheet for quick reference.

Share this article:
Dargslan Editorial Team (Dargslan)
About the Author

Dargslan Editorial Team (Dargslan)

Collective of Software Developers, System Administrators, DevOps Engineers, and IT Authors

Dargslan is an independent technology publishing collective formed by experienced software developers, system administrators, and IT specialists.

The Dargslan editorial team works collaboratively to create practical, hands-on technology books focused on real-world use cases. Each publication is developed, reviewed, and...

Programming Languages Linux Administration Web Development Cybersecurity Networking

Stay Updated

Subscribe to our newsletter for the latest tutorials, tips, and exclusive offers.