0

I am in the shell and I want to know the corresponding IP address(es) for a hostname.

I know I could get it through Perl, PHP, Python or a number of other scripting languages (probably even awk!), and this will be my workaround, but I am surprised that there seems to be no command-line tool to do this, no simple wrapper around getaddrinfo().

Am I wrong? Is there one? host, dig and getent do not count, I want something that uses the libc and acts according to /etc/nsswitch.conf, and something that is probably installed on any (linux) system by default.

Moved here from https://stackoverflow.com/questions/20211326/is-there-a-standard-command-line-tool-to-do-a-hostname-lookup

Answer: I was wrong, getent does not exclusively look in /etc/hosts, it actually does a proper lookup.

3

the traditional tool for doing host name lookups is nslookup from bind, but it is being replaced with dig. both tools are dns speciffic and don't use nsswitch. getent hosts uses nsswitch, but much less used.

10
  • getent does not use nsswitch, it exclusively looks in the hosts file, host and dig ignore the hosts file. – clacke Nov 28 '13 at 5:52
  • from the man page "getent - get entries from Name Service Switch libraries" and " hosts When no key is provided, use sethostent(3), gethostent(3), and endhostent(3) to enumerate the hosts database. When one or more key arguments are provided, pass each key to gethostbyaddr(3) or gethostby‐name2(3), depending on whether a call to inet_pton(3) indicates that the key is an IPv6 or IPv4 address or not, and display the result." so if it locks the hosts files, that is how your nsswitch.conf is setup. If you remove files it won't. – hildred Nov 28 '13 at 5:54
  • Btw, where is that man page from? My man page in Ubuntu is not that clear, it simply says gathers entries from the specified administrative database using the specified search keys. Where database is one of passwd, group, hosts, services, protocols, or networks. (Ubuntu 12.04, manpage inherited from Debian Lenny) – clacke Nov 28 '13 at 8:47
  • And RHEL has no manpage at all. Found yours at linux.die.net/man/1/getent , but I wonder what their source is. – clacke Nov 28 '13 at 8:51
  • I run debian stable and that man page is from version 3.44-1 of the manpages package. – hildred Nov 28 '13 at 8:54
0

getent is certainly the right tool for the job but it is unfortunately not available everywhere, one of the reason being it is not mandatory being undefined by POSIX.

On the other hand, while it isn't either specified by the standard, arp is very unlikely not to be available on any server that does name resolution. As arp follows nsswitch.conf settings, only uses libc, is available on any Linux/Unix system, and provides an output that seems to be consistent between implementations, parsing its output would do the job:

$ arp stackoverflow.com | sed -e 's/.*(//' -e 's/).*$//'
198.252.206.16

Here is a shell function that uses that method, avoiding an issue with Linux arp which switch to a different output format in some corner cases:

resolve()
{
  : ${1:?Usage: resolve name}
  (
    PATH=$PATH:/usr/sbin
    ao=$(arp $1)
    [ "$(echo "$ao"|grep HWaddress)" ] && ao=$(arp -a $1)
    echo "$ao" | sed -e 's/.*(//' -e 's/).*$//'
  )
}
2
  • I have yet to see a glibc-based distribution that does not come with the glibc command-line tools installed. Such a distribution may exist, but getent definitely fits my requirements. – clacke Nov 29 '13 at 9:12
  • Your question ruled out getent and I assumed that was because of its unavailability on some Linux platform you were working on. getent seems to be mainstream with current Linux distributions but that wasn't the case many years ago. It first appeared on Solaris and I believe many Unix outside those Solaris & Linux based are still missing getent for the reasons I posted. – jlliagre Nov 29 '13 at 11:35

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.