Here is my script and I'm good up to bold section. I'm trying to finish my script by seeing if my hostnames are an A record or an alias. I'm having no luck and really need someones advice. I've tried using grep and awk and seems to not work.

#Query DB for cnames
#Pulling cnames
#Lets use sed to clean up and remove "" that regex miss.
#Added dos2unix to get rid of the hidden M from /tmp/final.csv

mssql -f csv -c ~/applications/mssql/mssql.json -q "SELECT * FROM Cname" | cut -f 3 -d "," | sed 's/["]//g' | sort | uniq | dos2unix >/tmp/final.csv  

#Validating cnames from file /tmp/final.csv

while read -r host  
    echo $host | egrep "^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)+([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$" >/dev/null 2>&1  
if [ $? -eq 0 ]  
        echo "host $host"  
        echo "unable to resolve '$host'" >&2  
done < /tmp/final.csv

# removed weird stars from some of these lines -- thrig
#Verify whether it's an A record or an alias  
LOOKUP=`host $host`  
if [ $? -eq 0 ]  
        echo $LOOKUP | grep -f "alias" | awk '{print $2}'| grep -v '#' | awk '{print $2}'  
        echo "$LOOKUP is null"  
# what is this in relation to?? -- thrig  
  • That is where I am having issues. I am trying to see which hostnames from /tmp/final.csv are either an A record or alias. Not sure how to get that to work and I'm sure the code is wrong. – jphil1971 Mar 26 '17 at 15:52

An alias in DNS is a CNAME, so if you're checking for that, it would be best to query for that record type, use the option '-t CNAME'. Also you might want to consider using dig instead of host (I think host is deprecated, but not sure about that), because it doesn't return anything if it's an A record, and might be easier to test for than how host returns. For example:

$ dig -t CNAME +short ftp.med.cornell.edu

$ dig -t CNAME +short web-prd01.med.cornell.edu

That last command has no output. If you really want to use host:

$ host -t CNAME ftp.med.cornell.edu
ftp.med.cornell.edu is an alias for web-prd01.med.cornell.edu.

$ host -t CNAME  web-prd01.med.cornell.edu
web-prd01.med.cornell.edu has no CNAME record

I think you should be able to finish up the script with that.

  • host and nslookup are indeed deprecrated/not recommended tools for DNS operations. dig is the recommended one. – Patrick Mevzek Mar 27 '17 at 1:15
  • From where did you get that host is deprecated? It comes in the same package from ISC as dig. It's just a simpler version of dig. – Johan Myréen Mar 27 '17 at 6:36
  • 1
    deprecated is maybe too strong, it is just that once you learn to use dig there is no case where you need to go back to host. So why learn 2 tools where one is clearly all you need? As the documentation says: "The domain information groper (dig) is the most versatile and complete of these lookup tools." (discussion being between dig, host and nslookup) – Patrick Mevzek Mar 27 '17 at 23:07
  • I thought I read it in a man page that host was going to be deprecated but I can't find that anymore. There was a plan to deprecate nslookup and host back in 2004, so maybe that is what I was thinking of: en.wikipedia.org/wiki/Nslookup – Dale Mar 28 '17 at 0:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.