#!/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 "-------------------------------------" TEMP_FILE=$(mktemp) for host in {1..254}; do ( IP="$SUBNET.$host" if ping -c 2 -W 1 "$IP" > /dev/null 2>&1; then # Try getent first, then fall back to host command for PTR lookup HOSTNAME=$(getent hosts "$IP" | awk '{print $2}') if [ -z "$HOSTNAME" ]; then HOSTNAME=$(host "$IP" 2>/dev/null | awk '/domain name pointer/ {print $NF}' | sed 's/\.$//') fi if [ -n "$HOSTNAME" ]; then echo "$IP (Hostname: $HOSTNAME)" >> "$TEMP_FILE" else echo "$IP (Hostname: Unknown)" >> "$TEMP_FILE" fi 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 line; do echo "[+] $line" done fi # Calculate total hosts found TOTAL_ONLINE=$(wc -l < "$TEMP_FILE" | tr -d ' ') # Clean up temp file rm -f "$TEMP_FILE" echo "-------------------------------------" echo "Scan complete. Total online hosts found: $TOTAL_ONLINE"