Skip to content
Advertisement

Linux script to check if DNS have the same IP like the one next to it in the same list

I’ve got a list of DNS and IP adresses in this format:

DNS IP

DNS IP

DNS IP

And I need to do a script which do dig +short DNS and check if it has the same IP as the one next to it, and that with all the document. The idea is that if it’s the same, it returns a message like $DNS and $IP match if are the same, or an error message if they doesn’t.

I’ve tried with awk '{ DDD=$(dig +short $1); if ( $DDD == $2 ); then; print "'$DDD' and '$2' match."; fi }' all.log

But the result is:

(space) and (space) match.

(space) and (space) match.

(space) and (space) match.

Advertisement

Answer

Here is a simple answer using only bash:

while read DNS IP; do
  DDD=$(dig +short "$DNS")
  if [ "$DDD" = "$IP" ]; then
    echo "'$DDD' and '$IP' match."
  else
    echo "'$DDD' and '$IP' do not match."
  fi
done < all.log

Be warned that dig +short NAME command can return more than one line as a result, and in this case the script will not work…

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement