Check DNS resolver from subnet / Prefix

 #!/bin/bash


check_resolver() {

    local ip=$1

    response=$(dig @${ip} google.com +short +time=2 +tries=1)

    if [[ -n "$response" ]]; then

        if [[ $response =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]]; then

            echo "IP $ip dapat digunakan sebagai DNS resolver. Respon: $response"

        else

            echo "IP $ip merespon, tetapi tidak memberikan jawaban valid. Respon: $response"

        fi

    else

        echo "IP $ip tidak merespon."

    fi

}


read -p "Masukkan subnet (contoh: 103.103.103.0/24): " subnet


if ! command -v ipcalc &>/dev/null; then

    echo "ipcalc tidak ditemukan. Menginstalnya..."

    sudo apt-get install -y ipcalc

fi


range=$(ipcalc -n $subnet -b | grep -E "^Network:" | awk '{print $2}')

prefix=$(echo $subnet | cut -d'/' -f2)


total_ips=$((2**(32-prefix)))

IFS=. read -r i1 i2 i3 i4 <<<$(echo $range | cut -d'/' -f1)


echo "Mengecek semua IP dalam $subnet..."

for ((i=1; i<=$((total_ips-2)); i++)); do

    ip="${i1}.${i2}.${i3}.$((i4+i))"

    check_resolver "$ip"

done