Bash: lookup an IP for a host name, including /etc/hosts in search

17

5

Ubuntu 10.10+

In my script I need to lookup an IP for a given host name.

If that name is listed in /etc/hosts, then command should print IP from /etc/hosts, not from DNS server.

What commands I tried (nslookup, dig, host), completely ignore /etc/hosts — at least for names that are not known to the DNS server.

Note: I would prefer solution that would not require me to grep /etc/hosts by hand.

Alexander Gladysh

Posted 2011-07-04T04:16:10.507

Reputation: 792

Answers

23

getent uses the low-level glibc information functions to query all configured sources.

$ getent ahosts amd.com
163.181.249.32  STREAM amd.com
163.181.249.32  DGRAM  
163.181.249.32  RAW    
$ getent ahosts ipv6.google.com
2001:4860:b009::69 STREAM ipv6.l.google.com
2001:4860:b009::69 DGRAM  
2001:4860:b009::69 RAW    

Ignacio Vazquez-Abrams

Posted 2011-07-04T04:16:10.507

Reputation: 100 516

3getent hosts amd.com is probably a little simpler – higuita – 2015-10-22T13:35:46.643

6

$ gethostip localhost
localhost 127.0.0.1 7F000001
$ gethostip -d example.org
192.0.43.10

From the syslinux package, at least in Ubuntu 12.04.

l0b0

Posted 2011-07-04T04:16:10.507

Reputation: 6 306

3

This is super-hacky, but I've been using it for ages, and it works (for ipv4):

function ipfor() {
  ping -c 1 $1 | grep -Eo -m 1 '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}';
}

Use like: ipfor google.com

Mark Jaquith

Posted 2011-07-04T04:16:10.507

Reputation: 131

Hacky but portable. Me like. – luis.espinal – 2014-07-22T14:11:45.547

0

I simply use the following as replacement for inapt 'host' cmd. This automatically will do the right thing with some restrictions (IPv4 only).

myhost.c:

#include <stdio.h>
#include <stdlib.h>
#include <netdb.h>
#include <errno.h>
#include <string.h>

#define TOIN(a) ((struct sockaddr_in *)&(a))

main(argc, argv)
    char **argv;
{
    int err;
    struct sockaddr sa;
    char hbuf[NI_MAXHOST];

    if (argc <= 1) {
        printf("more args\n");
        exit(-1);
    }
    TOIN(sa)->sin_family = AF_INET;
    if (inet_pton(AF_INET, *(argv + 1), &TOIN(sa)->sin_addr) != 1) {
        printf("can't inet_pton: %s\n", errno ? strerror(errno) : "format err");
        exit(-1);
    }
    if (err = getnameinfo(&sa, sizeof(struct sockaddr_in), hbuf, sizeof hbuf, 0, 0, NI_NAMEREQD)) {
//        printf("%s\n", gai_strerror(err));
        printf("Host %s not found: 3(NXDOMAIN)\n", *(argv + 1));
        exit(-1);
    } else {
        printf("%s\n", hbuf);
        exit(0);
    }
}

sparkie

Posted 2011-07-04T04:16:10.507

Reputation: 2 110

0

nmap -sP 192.168.1.0/24|grep SEARCHED_HOSTNAME|sed -n 's/.*[(]\([0-9\.]*\)[)].*/\1/p'

No DNS Query

Philippe Gachoud

Posted 2011-07-04T04:16:10.507

Reputation: 545

While this may indeed answer the question it would be good to explain how and why it does so. A command line with little or no explanation as to what it is doing may not help future visitors who might need to solve a similar problem. – Mokubai – 2015-08-15T07:02:50.957