51

There are several command line utilities to resolve host names (host, dig, nslookup), however they all use nameservers exclusively, while applications in general look in /etc/hosts first (using gethostbyname I believe).

Is there a command line utility to resolve host names that behaves like a usual application, thus looking in /etc/hosts first and only then asking a nameserver?

(I am aware that it would probably be like 3 lines of c, but I need it inside of a somewhat portable shell script.)

Mark Henderson
  • 68,316
  • 31
  • 175
  • 255
Zulan
  • 595
  • 1
  • 5
  • 8
  • Could you please explain your situation a little more? Does `awk '/hostname/ { print $1 }' /etc/hosts` help? – quanta Aug 22 '11 at 11:08
  • @quanta Actually the current solution is grep/sed magic on /etc/hosts. I wanted to make that more general with a fallback. – Zulan Aug 22 '11 at 11:20

11 Answers11

73

This is easily achieved with getent:

getent hosts 127.0.0.1

getent will do lookups for any type of data configured in nsswitch.conf.

womble
  • 95,029
  • 29
  • 173
  • 228
29

One tool that would work is getent. So you could use getent hosts www.google.com, or getent hosts localhost. It will retrieve entries from the databases as specified in your Name Service Switch configuration /etc/nsswitch.conf.

For more modern implementations use getent ahosts www.google.com which will get multiple results.

Zoredache
  • 128,755
  • 40
  • 271
  • 413
  • 1
    Yes, but that would not fall back on DNS. – slowpoison Mar 19 '12 at 19:55
  • No, it resolves it in nsswitch.conf order. – cjc Mar 19 '12 at 19:58
  • @slowpoison, Take a look at your nsswitch config. My system has `files dns` for hosts, which means /etc/hosts is consulted and then the DNS resolver. Your config may be different. – Zoredache Mar 19 '12 at 20:07
  • @cjc, it does. I don't think I tried it correctly. – slowpoison Mar 19 '12 at 20:07
  • @Zoredache, I'm quite impressed with `getent`. Thanks for the intro to this command. – slowpoison Mar 19 '12 at 20:11
  • It's a helpful tool, but why use it to do a lookup in the /etc/hosts file which has been deprecated since about 2 decades, give or take? Thank goodness tools such as ping ignore the hosts file. There are other ways to accomplish these things, such as adding your own custom zone files to your nameserver. – aseq Mar 19 '12 at 21:28
11

Use getent ahosts, for instance:

$ getent ahosts www.google.com | sed -n 's/ *STREAM.*//p'
216.58.210.196
2a00:1450:4006:803::2004

You'll get all IPv4 and IPv6 addresses, via the glibc resolver (thus using /etc/hosts first, as usually configured in /etc/nsswitch.conf).

Do not use getent hosts, as it will give you either IPv6 or IPv4 addresses (not both), and the chosen protocol may not be one that does not work. Indeed, IPv6 addresses are generally preferred, but at some places, IPv6 data are filtered (not supported) by the routers.

vinc17
  • 213
  • 2
  • 6
10

You can use a gethostbyname() (deprecated) wrapper like:

python -c 'import socket;print socket.gethostbyname("www.google.com")'

Or a getaddrinfo() wrapper like:

python -c 'import socket;print socket.getaddrinfo("www.google.com","http")[0][4][0]'

For python3:

python -c 'import socket;print(socket.getaddrinfo("www.google.com","http")[0][4][0])'

Note that getaddrinfo will return all instances as a list. The last part of the command selects only the first tuple. This can also return IPv6 addresses.

Mircea Vutcovici
  • 16,706
  • 4
  • 52
  • 80
7

resolveip will do this.

Oddly, it's part of the mysql-server packages on RHEL and Ubuntu.

cjc
  • 24,533
  • 2
  • 49
  • 69
7

You could use [your favorite language here] to write a script that calls getnameinfo. That is how binaries (like ping) should be doing it, so you're ensured you get the same treatment.

Kyle Smith
  • 9,563
  • 1
  • 30
  • 32
5

You could be really hacky and use arp:

arp -n somehostname | tr -d '()' | awk '{print $2}'

but that would be really ugly so you shouldn't do that.

dawud
  • 14,918
  • 3
  • 41
  • 61
Paul M
  • 553
  • 5
  • 10
4

"gethostbyname" command line version:

#!/usr/bin/perl
use Socket;

$host = shift @ARGV;
die("usage: gethostbyname hostname\n") unless(defined($host));

$packed_ip = gethostbyname($host);

if (defined $packed_ip) {
    $ip_address = inet_ntoa($packed_ip);
    print "$ip_address\n";
    exit 0
} else {
    warn "$host not found\n";
    exit 1
}
Erik Aronesty
  • 284
  • 2
  • 7
2

Try this:

if [ `grep -c "hostname" /etc/hosts` -ge 1 ]; then
    ip=`awk '/hostname/ { print $1 }' /etc/hosts`
else
    ip=`host hostname | awk '/hostname has address/ { print $4 }'`
fi
quanta
  • 50,327
  • 19
  • 152
  • 213
2

getent hosts is broken. It prefers IPv6 addresses, gai.conf should be configured to prefer ipv4 but....

The Perl gethostbyname uses the precedence in /etc/nsswitch.conf

hosts: files dns

So this works like getent hosts should work for me.

Also:

perl -e 'use Socket; print inet_ntoa(inet_aton("www.google.com")) . "\n";'

should work.

slm
  • 7,355
  • 16
  • 54
  • 72
  • 7
    It's not broken, it just doesn't use getaddrinfo (which reads /etc/gai.conf). To use getaddrinfo, run `getent ahosts`. – craig65535 Oct 12 '15 at 18:35
-1

The following command will perform lookup via DNS /etc/hosts over direct DNS server lookup.

ping -W2 -c1 google.com | grep PING | sed -r "s/^[^\(]*\(([\.0-9]*)\).*/\1/"

This command will attempt to ping a domain (in this case, google.com) once with a wait timeout of 2 seconds, get the first line of the PING command which will state "PING google.com (216.58.199.36) 56(84) bytes of data", then use Stream Editor (sed) to detect the first set of brackets and extract the data within it which is the IP address we are looking for.

NOTE: the regex expression will not work if there are parenthesis (aka circular brackets) in the URL, but this would be a rare case.

John
  • 101
  • 2