#!/bin/bash
# alive2.sh
# Checks to see if a range of IP addresses are alive
# Input should be in the format alive2.sh 192.168.1.7-192.168.1.123

# Validate the input format
if [[ $1 =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+-[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
    start=$(echo $1 | cut -d'-' -f1 | cut -d'.' -f4)
    end=$(echo $1 | cut -d'-' -f2 | cut -d'.' -f4)
    
    # Iterate through IP addresses
    for ((n=start; n<=end; n++)); do
        host=$(echo $1 | cut -d'-' -f1 | cut -d'.' -f1-3).$n
        ping -c2 $host &> /dev/null
        if [ $? = 0 ]; then
            echo "$host is UP"
        else
            echo "$host is DOWN"
        fi
    done
else
    echo "Invalid input format. Please use the format alive2.sh 192.168.1.7-192.168.1.123"
fi
