15

What is the easiest way to get the IP address 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.

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

10 Answers 10

6

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.

4
  • 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, 2010 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, 2010 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, 2010 at 19:03
  • @Ether sed -e '/some people consider Perl a better form/s/better/worse/' SCNR
    – Tino
    Mar 8, 2015 at 3:32
14

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
10
  • Also does not work. (For hosts in /etc/hosts.) Try host localhost.
    – Albert
    Aug 14, 2010 at 17:01
  • serv ~ $ host localhost \n localhost has address 127.0.0.1
    – Amber
    Aug 14, 2010 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, 2010 at 17:09
  • 2
    See my edit - you might try resolveip.
    – Amber
    Aug 14, 2010 at 17:13
  • Hm, resolveip is not installed by default on my Debian.
    – Albert
    Aug 14, 2010 at 17:16
10

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

3
  • Nice, and works for IPv6, too.
    – Tino
    Mar 8, 2015 at 3:35
  • And doesn't need installing syslinux!
    – Claudiu
    Mar 21, 2016 at 19:18
  • Interesting that for some IPs it returns nothing while ahosts does return multi-line info. Nov 3, 2017 at 23:59
7

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

1
  • Came here to give this answer, up-voted you instead.
    – Not Now
    Oct 31, 2011 at 23:17
5

Why not dig +short hostname ?

(query DNS)

4
  • It does not take /etc/hosts into account. Nov 1, 2011 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, 2011 at 15:06
  • I know, but the original question wanted /etc/hosts :) Nov 1, 2011 at 15:14
  • sure enough; my reading comprehension fail...
    – gWaldo
    Nov 1, 2011 at 16:49
3

Well, my current solution:

ping -c1 -n www.google.com | head -n1 | sed "s/.*(\([0-9]*\.[0-9]*\.[0-9]*\.[0-9]*\)).*/\1/g"
2
  • this doesn't give only the IP: PING www.l.google.com (72.14.234.104): 56 data bytes Aug 14, 2010 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, 2010 at 17:19
0

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
3
  • Well, it finds the entry but it only lists its MAC, not its IP. :)
    – Albert
    Aug 14, 2010 at 17:14
  • Ah, arp -n hostname shows the IP.
    – Albert
    Aug 14, 2010 at 17:14
  • Only, this way doesn't work for servers outside the network. :P
    – Albert
    Aug 14, 2010 at 17:15
0

Using ping is not that bad since you generally do not have any strong dependencies.

Here is the function I used on Linux systems :

getip () { ping -c 1 -t 1 $1 | head -1 | cut -d ' ' -f 3 | tr -d '()' ; }
0

Here goes a Python one-liner. Should work on all OS:

python -c "import sys, socket; print (socket.gethostbyname('<hostname>'))"

This can be embedded easily in a shell script:

function gethostip() {
    python -c "import sys, socket; print (socket.gethostbyname('$1'))"
}
-1
nmap -sP 192.168.1.0/24|grep SEARCHED_HOSTNAME|sed -n 's/.*[(]\([0-9\.]*\)[)].*/\1/p'
  • Nmap gets from your subnet (192.168.1.0 or whatever) the adresses
  • with grep get only the line of the hostname you are looking for
  • With sed get only the ip address inside the parentheses
2
  • 2
    A bit more info on the various switches and why used would add to your answer
    – Dave M
    Aug 14, 2015 at 15:33
  • There are at least four different reasons why this answer is not going to work. You make assumptions about the range of IP addresses within which the answer will be found. It is inefficient due to producing a lot more network traffic than needed. It only works for IP addresses which responds to probes. It assumes reverse DNS contains the exact same mappings as forward DNS.
    – kasperd
    Aug 14, 2015 at 16:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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