Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

share|improve this question
add comment

1 Answer 1

up vote 1 down vote accepted

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

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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