2

I want to test in a script whether a hostname resolves to something other than 127.0.0.1 as a pre-flight test for an install script. The tools I know (dig, nslookup) specifically query DNS. In my situation a correct entry in /etc/hosts is a valid configuration. So the question is:

Is there a dig-like tool, ideally already on the system, that does the full name-resolution process via nsswitch.conf, like (for example) ping does?

dig my-test-server-that-only-exists-in-etc-hosts 

returns NXDOMAIN.

4

This already has an answer on SU:

Bash: lookup an IP for a host name, including /etc/hosts in search

Basically, you use

getent ahosts host_name

where host_name can either be an entry in your /etc/hosts, in which case it will resolve to that, or a host that your DNS can resolve.

Quoting the getent(1) man page:

ahosts
When no key is provided, use sethostent(3), gethostent(3), and endhostent(3) to enumerate the hosts database. This is identical to using hosts. When one or more key argu‐ ments are provided, pass each key in succession to getad‐ drinfo(3) with the address family AF_UNSPEC, enumerating each socket address structure returned

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.