#!/bin/bash # Usage: ./ping_sweep.sh # Example: ./ping_sweep.sh 192.168.1 if [ -z "$1" ]; then echo "Usage: $0 " echo "Example: $0 192.168.1" exit 1 fi SUBNET="$1" echo "Scanning $SUBNET.1 to $SUBNET.254..." echo "-------------------------------------" # Temporary file to store results safely across subshells TEMP_FILE=$(mktemp) for host in {1..254}; do ( IP="$SUBNET.$host" if ping -c 2 -W 1 "$IP" > /dev/null 2>&1; then echo "$IP" >> "$TEMP_FILE" fi ) & done # Wait for all background tasks to finish wait # Output online hosts sorted naturally by IP if [ -s "$TEMP_FILE" ]; then sort -V "$TEMP_FILE" | while read -r ip; do echo "[+] $ip is ONLINE" done fi # Calculate total hosts found TOTAL_ONLINE=$(wc -l < "$TEMP_FILE" | tr -d ' ') # Remove temporary file rm -f "$TEMP_FILE" echo "-------------------------------------" echo "Scan complete. Total online hosts found: $TOTAL_ONLINE"