Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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 http://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.

share|improve this question

2 Answers 2

up vote 2 down vote accepted

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.

share|improve this answer
    
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
    
Oh! Thank you for enlightening me. –  clacke Nov 28 '13 at 6:26
    
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

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/).*$//'
  )
}
share|improve this answer
    
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

 
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.