How do I determine what hostnames do not resolve to an IP address?
bash, hostname, shell
Solution
Read each hostname from the file, perform a DNS lookup of the hostname, and check the response:
#!/bin/bash
while read hstnm
do
if ! host ${hstnm} > /dev/null
then
echo "No ip for ${hstnm}"
fi
done < hostnames.txt
I used the `host` utility in this example, but you could also use `dig` (piped into `grep -q` for "NXDOMAIN", for example), or `nslookup`.
`dig` + `grep` example:
#!/bin/bash
while read hstnm
do
if dig ${hstnm} | grep -q 'NXDOMAIN'
then
echo "no ip for ${hstnm}"
fi
done < hostnames.txt
Problem
I'm using bash shell on Mac 10.9.1. Given a file of hostnames, which might ocntain entries like ``` dave.mydomain.com dave2.otherdomain.com somethingelse.whatever.com ``` How can I determine which of the host names in the file does not resolve to an IP address?