17

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.

Peter Mortensen
  • 2,318
  • 5
  • 23
  • 24
Albert
  • 332
  • 1
  • 2
  • 12

10 Answers10

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
Amber
  • 307
  • 3
  • 6
11

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

Janne Pikkarainen
  • 31,852
  • 4
  • 58
  • 81
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

Tino
  • 1,123
  • 1
  • 12
  • 16
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.

Ether
  • 322
  • 3
  • 12
  • 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 03: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
  • @Ether `sed -e '/some people consider Perl a better form/s/better/worse/'` SCNR – Tino Mar 08 '15 at 03:32
6

Why not dig +short hostname ?

(query DNS)

gWaldo
  • 11,957
  • 8
  • 42
  • 69
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"
Albert
  • 332
  • 1
  • 2
  • 12
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'))"
}
erny
  • 351
  • 1
  • 7
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
Peter Mortensen
  • 2,318
  • 5
  • 23
  • 24
ennuikiller
  • 838
  • 8
  • 8
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 '()' ; }
Bruno BEAUFILS
  • 523
  • 1
  • 4
  • 5
-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
Philippe Gachoud
  • 1,687
  • 16
  • 21
  • 2
    A bit more info on the various switches and why used would add to your answer – Dave M Aug 14 '15 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 '15 at 16:38