Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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

share|improve this question
    
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
add comment

8 Answers

up vote 21 down vote accepted

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.

share|improve this answer
add comment

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.

share|improve this answer
    
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
show 1 more comment

resolveip will do this.

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

share|improve this answer
add comment

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.

share|improve this answer
add comment

You can use a gethostbyname() wraper like:

python -c 'import socket;print socket.gethostbyname("www.google.com")'
share|improve this answer
    
This will work, but it's been obsolete for a while. See linux.die.net/man/3/gethostbyname. –  Kyle Smith Mar 19 '12 at 21:01
    
Thank you, I did not know about it. ;) –  Mircea Vutcovici Mar 20 '12 at 0:53
add comment

"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
}
share|improve this answer
add comment

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
share|improve this answer
add comment

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.

share|improve this answer
add comment

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.