0

I have a script that looks up specific records for a domain.

Ie. domain Cpanel.domain; ftp.domain; mail.domain; mx record and txt records. When a specific record is not found the script stops but does not fail or end until it is manually cancelled.

echo "==[ DNS Check ]=="
echo "Google 8.8.8.8 : $(dig @8.8.8.8 $dom +short)"
echo "Cloudflare 1.1.1.1 : $(dig @1.1.1.1 $dom +short)"
echo "Quad 9 9.9.9.9 : $(dig @9.9.9.9 $dom +short)"
echo "Afrihost ns.dns1.co.za : $(dig @ns.dns1.co.za $dom +short)"
#echo "Afrihost 169.1.1.1 : $(dig @169.1.1.1 $dom +short)"
echo "-- -- -- -- -- -- -- -- -- -- -- -- "
echo
echo "==[ PUBLIC ]=="
getIP=$(dig @8.8.8.8 +short $dom)
if [[ "$getIP" == ""  ]];then
    echo "Domain NSLookup failed!! - Public DNS Resolution failed."
else
    echo "IP : $getIP :: $(nslookup $getIP |grep "name =" |awk '{print $4}')"
    echo "cPanel : $(dig @8.8.8.8 +short cpanel.$dom) -- $(nslookup $(dig @8.8.8.8 +short cpanel.$dom) |grep "name =" |awk '{print $4}')"
    echo "FTP : $(dig @8.8.8.8 +short ftp.$dom) -- $(nslookup $(dig @8.8.8.8 +short ftp.$dom) |grep "name =" |awk '{print $4}')"
    echo "mail.$dom : $(dig @8.8.8.8 +short mail.$dom) -- $(nslookup $(dig @8.8.8.8 +short mail.$dom) |grep "name =" |awk '{print $4}')"
fi

Output:

./resolve somedomain

Date : 17/10/2019 Time : 07.21.11

==[ DNS Check ]==
Google 8.8.8.8 : 103.9.170.0
Cloudflare 1.1.1.1 : 103.9.170.0
Quad 9 9.9.9.9 : 103.9.170.0
Afrihost ns.dns1.co.za : 
-- -- -- -- -- -- -- -- -- -- -- -- 

==[ PUBLIC ]==
IP : 103.9.170.0 :: hostingservername.
> 

I would like it to not stop but move on or just indicate record not found and then go to the next command.

At the ">" would be the cpanel.domain A record, which does not exist for the domain in question.

What I would like it to do is print "not found" and then continue to the next command.

Ruslan Osmanov
  • 20,486
  • 7
  • 46
  • 60
  • Is the `$dom` variable initialized? If it isn't, then the command `dig @8.8.8.8 +short $dom` outputs non-empty list of root servers, and the test `[[ "$getIP" == "" ]]` is not passed. To avoid issues related to uninitialized variables, `set -o nounset` at the top of your script – Ruslan Osmanov Oct 17 '19 at 08:37
  • So handle errors of each command. – KamilCuk Oct 17 '19 at 08:45
  • Sorry I am new to this. I added ```set -o nounset``` above the variable: ```#!/bin/bash set -o nounset dom=$1 echo echo date +"Date : %d/%m/%Y Time : %H.%M.%S" echo echo "==[ DNS Check ]==" ``` It did not have effect – AfriGhost Oct 17 '19 at 08:48

1 Answers1

0

So the problem is that 'dig' is returning an empty string, so when you pass that to nslookup, it sees that there are no command line parameters and runs in interactive mode, i.e. it prints the > prompt and waits for user input.

To avoid this, I would use some simple functions to encapsulate the dig and nslookup calls, and include error checking in those functions:

declare    dns_server=8.8.8.8

function my_dig() {
  [ ${#1} -eq 0 ] && return 1

  local    ip=$(dig @${dns_server} +short ${1})
  [ ${#ip} -eq 0 ] && return 2

  echo ${ip}
}

function my_nslookup() {
  [ ${#1} -eq 0 ] && return 1

  local    hostname=$(nslookup ${1} | sed -n 's/.* name = \(.*\)/\1/p')
  [ ${#hostname} -eq 0 ] && echo "Not Found." && return 2

  echo ${hostname}
}

function print_line() {
  echo "${1} : ${2:-Not Found} -- $(my_nslookup ${2})"
}

...
# The rest of your code.
...

else
  print_line IP ${getIP}
  print_line cPanel $(my_dig cpanel.$dom)
  print_line FTP $(my_dig ftp.$dom)
  print_line mail.${dom} $(my_dig mail.$dom)
fi
Andrew Vickers
  • 2,504
  • 2
  • 10
  • 16