Files
scripts/scan_hosts.sh
2026-03-24 00:03:59 +01:00

53 lines
1.3 KiB
Bash
Executable File

#!/bin/bash
# Check if the subnet argument is provided
if [ "$#" -ne 1 ]; then
echo "Usage: $0 <subnet/CIDR>"
exit 1
fi
# Extract the base IP and CIDR from the argument
subnet="$1"
IFS='.' read -r -a octets <<< "$(echo "$subnet" | cut -d'/' -f1)"
cidr=$(echo "$subnet" | cut -d'/' -f2)
# Function to ping a single IP
ping_host() {
ip="$1"
if ping -c 1 -W 1 "$ip" &> /dev/null; then
echo "$ip is up"
fi
}
# Export the function so it can be used by GNU Parallel
export -f ping_host
# Generate IPs based on CIDR
if [ "$cidr" -eq 24 ]; then
# For /24, only the last octet changes
for i in {1..254}; do
ip="${octets[0]}.${octets[1]}.${octets[2]}.$i"
echo "$ip"
done
elif [ "$cidr" -eq 16 ]; then
# For /16, the last two octets change
for i in {0..255}; do
for j in {0..255}; do
if [ "$i" -eq 0 ] && [ "$j" -eq 0 ]; then
continue # Skip network address
fi
if [ "$i" -eq 255 ] && [ "$j" -eq 255 ]; then
continue # Skip broadcast address
fi
ip="${octets[0]}.${octets[1]}.$i.$j"
echo "$ip"
done
done
else
echo "Unsupported CIDR notation. Only /24 and /16 are supported in this script."
exit 1
fi | parallel -j 512 --progress ping_host
echo "Scan completed."