0

How could make bash script to filter list of DNS resolver because it out with false positive results

suggested code

!/bin/sh
for IP in `cat ./resolvers.txt`; do
    printf "%-4s", $IP
    dig @$IP test.com
done

test.com is my domain which I already know what the output would be, then any different result would be from bad resolver.

Emad
  • 143
  • 8

1 Answers1

1
#!/usr/bin/env bash
case $BASH_VERSION in '') echo "ERROR: Use bash, not sh" >&2; exit 1;; esac

test_domain="test.com"
correct_ip=1.2.3.4

while IFS= read -r ip; do
  if [[ $(dig +short "@$ip" "$test_domain" 2>/dev/null) = "$correct_ip" ]]; then
    printf '%s\n' "$ip"
  fi
done <all-resolvers.txt >good-resolvers.txt

...will read all-resolvers.txt, and write a list of only the DNS servers that correctly function (resolve test_domain to correct_ip) to good-resolvers.txt.


See:

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441