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.

Utilities like host and dig let you see the IP address corresponding to the host name.

There is also the getent utility that can be used to query /etc/hosts or other NSS databases.

I am looking for a convenient standard utility (which is available in Debian, say) which resolves a host name regardless of where it is defined.

It should be more or less equivalent to

ping "$HOST" | head -1 | perl -lne '/\((.*?)\)/ && print $1'
share|improve this question
3  
What exactly is wrong with getent? –  Patrick Apr 5 '13 at 20:16
2  
Yeah, I don't thing it could get more convenient than getent. –  Bratchley Apr 5 '13 at 20:19
    
@Patrick: hmm, I didn't realise getent also looks in DNS (I thought it only looks in /etc/hosts). Now that I've tried it, here's what's wrong with getent: for google.com, it returns a single address, and that address is IPv6. Which is not helpful, since I'm on an IPv4 network, and my command would actually print an IPv4 address. –  Roman Cheplyaka Apr 5 '13 at 20:48
    
@RomanCheplyaka - See eppesuig's answer that shows how to use getent to only look for IPv4 addresses: unix.stackexchange.com/a/71392/7453 –  slm Aug 29 '13 at 18:51

5 Answers 5

up vote 2 down vote accepted

The only command that I am aware that does what you want is resolveip:

http://linux.die.net/man/1/resolveip

However it only comes with mysql-server, which may not be ideal to install everywhere.

share|improve this answer
    
Nice one, thanks! –  Roman Cheplyaka Apr 5 '13 at 21:16

If the problem is that you do not want to resolve these names using ipv6, then just ask getent to use ipv4 only. This will enumerate all ipv4 addresses:

giuseppe@blatta:~$ getent ahostsv4 www.google.com | cut -d' ' -f1 | sort -u
173.194.40.80
173.194.40.81
173.194.40.82
173.194.40.83
173.194.40.84
share|improve this answer

The thing is there are several APIs to resolve host names like gethostbyname, getaddrinfo and inet_pton and some of those can return more than one address and/or you can query the type of address you want.

If you want a portable way to get one IPv4 address, then maybe:

perl -MSocket -le 'print inet_ntoa inet_aton shift' www.google.com
share|improve this answer

(This answer only applies if you're root on the machine.)

I used to be annoyed by this too, and then I standardized on running dnsmasq on all my machines. Dnsmasq is a lightweight DNS cache. As a side benefit, it serves the content of /etc/hosts over DNS.

share|improve this answer

gethostip -d name.domain from the syslinux package on Ubuntu (and probably Debian). -d outputs decimal format.

share|improve this answer

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.