22

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'
Roman Cheplyaka
  • 1,184
  • 3
  • 11
  • 25
  • 5
    What exactly is wrong with `getent`? – phemmer Apr 05 '13 at 20:16
  • 2
    Yeah, I don't thing it could get more convenient than getent. – Bratchley Apr 05 '13 at 20:19
  • 1
    @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 05 '13 at 20:48
  • @RomanCheplyaka - See eppesuig's answer that shows how to use getent to only look for IPv4 addresses: http://unix.stackexchange.com/a/71392/7453 – slm Aug 29 '13 at 18:51

6 Answers6

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
eppesuig
  • 3,208
  • 1
  • 14
  • 13
5

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
Stéphane Chazelas
  • 522,931
  • 91
  • 1,010
  • 1,501
4

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.

Georgyo
  • 341
  • 2
  • 5
3

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

dan3
  • 660
  • 6
  • 16
1

(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.

Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
0

I used my pure perl knowledge and made a little script with error handling:

/usr/local/bin/gethostip:

#!/usr/bin/perl

# inspired by: https://unix.stackexchange.com/questions/71379/host-lookup-that-respects-etc-hosts#71393

use strict;
use Socket;

my $name = $ARGV[0];
if ($name eq '') {
  print STDERR "Usage: gethostip <hostname>\n";
  exit 1;
}
my $ip = inet_aton($name);
die("Unable to resolve host name $name") if ($ip eq '');
my $ipstr = inet_ntoa($ip);
print "$ipstr\n";

Thx to Stéphane Chazelas for the initial idea

Daniel Alder
  • 838
  • 11
  • 26