SwiftPing - Simple Network Host Scanner
SwiftPing is a lightweight yet powerful Bash script designed to quickly identify active hosts within a specified IP range using ICMP (ping) requests. It supports custom IP ranges and can optionally perform hostname resolution.
Features :
Fast Scanning: Utilizes parallel ping operations with a timeout for quick results.
Flexible IP Ranges: Scan an entire /24 subnet or define a custom start and end IP address.
Hostname Resolution: Attempts to resolve hostnames for active IPs using the dig command (if installed).
Robust Error Handling: Includes checks for dependencies, valid input, and handles unexpected issues.
Clean Output: Suppresses verbose ping output, showing only active hosts and their resolved hostnames.
Prerequisites :
Bash: The script is written in Bash and requires a Bash environment to run.
ping utility: Standard network utility, usually pre-installed on Linux/macOS.
dig utility (optional): For hostname resolution. If not installed, the script will still function but won't show hostnames. On Debian/Ubuntu, install with sudo apt-get install dnsutils. On Fedora/RHEL, install with sudo yum install bind-utils or sudo dnf install bind-utils.
Installation (Making it Executable) :
Save the script: Copy the content of the SwiftPing.sh script into a file named SwiftPing.sh.
#!/bin/bash
# Enable strict mode for robust error handling
set -euo pipefail
# Function to display usage instructions
display_usage() {
echo "Usage: ./SwiftPing.sh <IP_PREFIX> [START_OCTET] [END_OCTET]"
echo "Example 1 (full /24 subnet): ./SwiftPing.sh 192.168.1"
echo "Example 2 (custom range): ./SwiftPing.sh 192.168.1 10 50"
echo ""
echo "Note: Hostname resolution requires 'dig' command to be installed."
}
# Function to check for required commands
check_dependencies() {
local missing_commands=""
if ! command -v ping &>/dev/null; then
missing_commands+="ping "
fi
# Only check for dig if hostname resolution is part of the logic
# if command -v dig &>/dev/null; then # This would mean dig is optional
# Instead, we check if it's available when we use it.
if [ -n "$missing_commands" ]; then
echo "Error: The following required commands are not installed: $missing_commands"
echo "Please install them and try again."
exit 1
fi
}
# --- Main Script Logic ---
check_dependencies
# Check for correct number of arguments
if [ "$#" -lt 1 ] || [ "$#" -gt 3 ]; then
display_usage
exit 1
fi
IP_PREFIX="$1"
START_OCTET=1
END_OCTET=254
# If custom range is provided
if [ "$#" -eq 3 ]; then
START_OCTET="$2"
END_OCTET="$3"
# Input validation for start and end octets
# Regex to ensure only digits
if ! [[ "$START_OCTET" =~ ^[0-9]+$ && "$END_OCTET" =~ ^[0-9]+$ ]]; then
echo "Error: START_OCTET and END_OCTET must be numbers."
display_usage
exit 1
fi
# Range validation
if [ "$START_OCTET" -lt 1 ] || [ "$START_OCTET" -gt 254 ] || \
[ "$END_OCTET" -lt 1 ] || [ "$END_OCTET" -gt 254 ]; then
echo "Error: START_OCTET and END_OCTET must be between 1 and 254 (inclusive)."
display_usage
exit 1
fi
if [ "$START_OCTET" -gt "$END_OCTET" ]; then
echo "Error: START_OCTET cannot be greater than END_OCTET."
display_usage
exit 1
fi
fi
echo "Scanning $IP_PREFIX.$START_OCTET to $IP_PREFIX.$END_OCTET..."
echo "Active hosts found:"
echo "-------------------"
# Array to store PIDs of background processes
PIDS=()
for ip in $(seq "$START_OCTET" "$END_OCTET"); do
# Run ping in the background with a 1-second timeout (-W 1)
# The output is piped to a subshell to allow for hostname resolution if 'dig' is available.
(
TARGET_IP="$IP_PREFIX.$ip"
# Use ping -c 1 -W 1 with a timeout for faster checks
# Redirect stdout and stderr to /dev/null to keep main script output clean
if ping -c 1 -W 1 "$TARGET_IP" &>/dev/null; then
# If ping is successful, then process the output
echo -n "$TARGET_IP"
# Attempt hostname resolution if 'dig' is available
if command -v dig &>/dev/null; then
HOSTNAME=$(dig -x "$TARGET_IP" +short | head -n 1) # Get first hostname if multiple
if [ -n "$HOSTNAME" ]; then
echo " ($HOSTNAME)"
else
echo "" # Newline if no hostname
fi
else
echo "" # Newline if dig is not available
fi
fi
) & # Run this entire block in the background
PIDS+=($!) # Store the PID of the background subshell
done
# Wait for all background processes to complete
for pid in "${PIDS[@]}"; do
wait "$pid" || true # `|| true` prevents `set -e` from exiting if a background process fails
done
echo "-------------------"
echo "Scan complete."
echo "Note: Some hostnames might not resolve if 'dig' is not installed or available."
Make it executable: Navigate to the directory where you saved the file in your terminal and run:
chmod +x SwiftPing.sh
Usage :
Run the script from your terminal:
./SwiftPing.sh <IP_PREFIX> [START_OCTET] [END_OCTET]
Arguments :
<IP_PREFIX> (required): The first three octets of the IP address range you want to scan (e.g., 192.168.1).
[START_OCTET] (optional): The starting last octet for the scan. If omitted, defaults to 1.
[END_OCTET] (optional): The ending last octet for the scan. If omitted, defaults to 254.
Examples :
Scan an entire /24 subnet (e.g., 192.168.1.1 to 192.168.1.254):
./SwiftPing.sh 192.168.1
Scan a custom range within a subnet (e.g., 192.168.1.10 to 192.168.1.50):
./SwiftPing.sh 192.168.1 10 50
Save the output to a file:
./SwiftPing.sh 192.168.1 > active_hosts.txt
Important Notes :
-
Permissions: You do not need sudo privileges to run this script for basic ping scanning and dig lookups.
-
Ethical Use: Always ensure you have explicit permission to scan any network. Unauthorized scanning can be illegal and may violate network usage policies. This script is intended for legitimate network administration, troubleshooting, and educational purposes on networks you own or are authorized to assess.
-
Advanced Scanning: For more in-depth network analysis (e.g., open port detection, service versioning, OS fingerprinting), specialized tools like nmap are highly recommended. This script serves as a simple and fast host discovery utility.