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.

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.

share|improve this question

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

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

    
I still wonder a bit why Unix shells are not considered as programming languages... –  Albert Aug 16 '10 at 3:11
    
Because shell traditionally is seen as scripting, not programming ;) –  Tino Mar 28 '13 at 17:10

7 Answers 7

up vote 4 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.

share|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

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
share|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

Well, my current solution:

ping -c1 -n www.google.com | head -n1 | sed "s/.*(\([0-9]*\.[0-9]*\.[0-9]*\.[0-9]*\)).*/\1/g"
share|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

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
share|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

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

share|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

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

share|improve this answer

Why not dig +short hostname ?

(query DNS)

share|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

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.