What is the easiest way to get the IP from a hostname?

I was thinking about trying a ping and parse it from the output. However, that doesn't seem very nice and will probably not work the same way on all systems.

I searched a bit around and found solutions with nslookup but that doesn't work for hostnames in /etc/hosts.

link|improve this question
Why the close vote? Why without comment? – Albert Aug 14 '10 at 17:02
The close vote is for "move to serverfault", since this isn't really a programming question. – Amber Aug 14 '10 at 17:03
Why? Such a thing would only be needed for programming, nowhere else. – Albert Aug 14 '10 at 17:04
4  
I see it as a programming question for shellscript - he is not trying to get the IP, he is trying to write a program which needs to get it. – Amadan Aug 14 '10 at 17:05
There's a reason why they're close votes, and why 5 of them are required. If general consensus is that it should stay, then it will stay. Don't get too worked up about it. Note, however, that you might actually get a better response on ServerFault, if nothing else due to the greater likelihood that sysops will know more about shell scripting w.r.t. servers than the average programmer. – Amber Aug 14 '10 at 17:09
show 8 more comments
feedback

migrated from stackoverflow.com Aug 15 '10 at 15:29

This question came from our site for professional and enthusiast programmers.

7 Answers

up vote 3 down vote accepted

You can do this with standard system calls. Here's an example in Perl:

use strict; use warnings;
use Socket;
use Data::Dumper;

my @addresses = gethostbyname('google.com');
my @ips = map { inet_ntoa($_) } @addresses[4 .. $#addresses];
print Dumper(\@ips);

produces the output:

$VAR1 = [
          '74.125.127.104',
          '74.125.127.103',
          '74.125.127.105',
          '74.125.127.106',
          '74.125.127.147',
          '74.125.127.99'
        ];

(On the command-line, the same script can be written as: perl -MSocket -MData::Dumper -wle'my @addresses = gethostbyname("google.com"); my @ips = map { inet_ntoa($_) } @addresses[4 .. $#addresses]; print Dumper(\@ips)')

You can do this similarly in other languages -- see the man page for the system calls at man -s3 gethostbyname etc.

link|improve this answer
Cool, that works. Esp., perl -MSocket -MData::Dumper -wle'my @addresses = gethostbyname("www.google.com"); my @ips = map { inet_ntoa($_) } @addresses[4 .. $#addresses]; print $ips[0]'. – Albert Aug 14 '10 at 17:44
Very strange that this answer is some program code... :) Looks almost like a Stackoverflow answer. Doesn't really belong on Serverfault. But I'll accept the answer anyway. – Albert Aug 16 '10 at 3:13
@Albert: well to be fair: 1. the question was posted on SO originally and migrated to SF, and 2. the type of data you're looking for needs to be parsed with something; some people consider Perl a better form of shell script :D – Ether Aug 28 '10 at 19:03
feedback

host <hostname>

Ex:

serv ~ $ host stackoverflow.com
stackoverflow.com has address 69.59.196.211

Edit

On Linux, (and some OS X variants, at least), you might be able to use resolveip, which is part of the MySQL server package:

/etc/hosts:
 ...
 127.0.0.1     localhost localhost.localdomain foo
 ...

serv ~ $ resolveip foo
IP address of foo is 127.0.0.1
link|improve this answer
Also does not work. (For hosts in /etc/hosts.) Try host localhost. – Albert Aug 14 '10 at 17:01
serv ~ $ host localhost \n localhost has address 127.0.0.1 – Amber Aug 14 '10 at 17:07
Hm, well, not here. I'm curious why that works for you. Or why it does not for me. – Albert Aug 14 '10 at 17:09
See my edit - you might try resolveip. – Amber Aug 14 '10 at 17:13
Hm, resolveip is not installed by default on my Debian. – Albert Aug 14 '10 at 17:16
show 5 more comments
feedback

For IPv4 there is a standard program which works out of the box using the resolver including /etc/hosts:

host="localhost"
ip="`gethostip -d "$host"`"

It is part of Debian, install it with:

apt-get install syslinux

For other protocols than IPv4 (like IPv6) I currently don't know a similar tool. Update: Because of this I just wrote a small tool which is capable to resolve IPv6, too:

https://github.com/hilbix/misc/blob/master/src/ipof.c

It is thought for a quick and dirty shell use like gethostip but allows IPv6, too:

ip="`ipof -6 -- heise.de`"

It also can be used interactively, for example:

ipof -a -d -x -v -h -

HTH

link|improve this answer
Came here to give this answer, up-voted you instead. – Not Now Oct 31 '11 at 23:17
Thanks. And updated for IPv6. – Tino Nov 1 '11 at 11:28
feedback

Well, my current solution:

ping -c1 -n www.google.com | head -n1 | sed "s/.*(\([0-9]*\.[0-9]*\.[0-9]*\.[0-9]*\)).*/\1/g"
link|improve this answer
this doesn't give only the IP: PING www.l.google.com (72.14.234.104): 56 data bytes – klez Aug 14 '10 at 17:18
Yea, seems that sed behaves slightly different on each system. :) How annoying. I changed it a bit, I think it should work everywhere now. – Albert Aug 14 '10 at 17:19
feedback

This ancient post seem to have many creative solutions.

If I need to make sure also /etc/hosts gets accessed, I tend to use

getent hosts somehost.com

This works, at least if `/etc/nsswitch.conf' has been configured to use files (as it usually is).

link|improve this answer
feedback

on some unices the following will work:

arp <hostname>

For example on MAC OS X, I get this:

arp My-iMac.local
My-iMac.local (192.168.1.2) -- no entry
link|improve this answer
Well, it finds the entry but it only lists its MAC, not its IP. :) – Albert Aug 14 '10 at 17:14
Ah, arp -n hostname shows the IP. – Albert Aug 14 '10 at 17:14
Only, this way doesn't work for servers outside the network. :P – Albert Aug 14 '10 at 17:15
feedback

Why not dig +short hostname ?

(query DNS)

link|improve this answer
It does not take /etc/hosts into account. – Janne Pikkarainen Nov 1 '11 at 13:38
DNS is supposed to be a definitive source for Name Resolution, and if you use the host file to override, that's fine. But that's an override, not definitive... – gWaldo Nov 1 '11 at 15:06
I know, but the original question wanted /etc/hosts :) – Janne Pikkarainen Nov 1 '11 at 15:14
sure enough; my reading comprehension fail... – gWaldo Nov 1 '11 at 16:49
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.