We all know we can use ping command to get the ip address of a hostname , but sometimes we need to do it for multiple hosts.

This post shows you how to resolve hostname to ip address in several ways , like use command ping or getent or host ,etc, so that we can do it via bash script.

Method 1: use ping command

ping command is always our friend , it’s very reliable and intuitive .

ping -q -c1 -t1 yourhostname | tr -d '()' | awk '/^PING/{print $3}'

Let me explain above command a little bit:

  • -q Quiet output. Nothing is displayed except the summary lines at startup time and when finished
  • -c1 only send one ping packet
  • -t1 set TTL to 1 , if not pingable return immediately
  • tr -d '()' removed Parentheses around ip address
  • awk ... print ip address

Method 2: use getent command

The getent command displays entries from databases supported by the Name Service Switch libraries, which are configured in /etc/nsswitch.conf.

getent ahostsv4 www.example.com

For IP v6 , you can use

getent ahostsv6 www.yahoo.com

For both IP v4 and v6:

getent ahosts www.yahoo.com

Let see one example:

j@ubuntu:~/tmp/1030$ getent ahostsv4 www.yahoo.com
180.222.102.202 STREAM new-fp-shed.wg1.b.yahoo.com
180.222.102.202 DGRAM  
180.222.102.202 RAW    
180.222.102.201 STREAM 
180.222.102.201 DGRAM  
180.222.102.201 RAW    

Here the domain has 2 IP addresses and a CNAME(kind of alias) new-fp-shed.wg1.b.yahoo.com points to one of these 2 IP addresses randomly.

So to get one of the IP address directly, below command can be used.

getent ahostsv4 www.bing.com | grep STREAM | head -n 1 | awk '{print $1}'

Method 3: use host command

host is a simple utility for performing DNS lookups. It is normally used to convert names to IP addresses and vice versa. When no arguments or options are given, host prints a short summary of its command line arguments and options.

j@ubuntu:~/tmp/1030$ host www.yahoo.com
www.yahoo.com is an alias for new-fp-shed.wg1.b.yahoo.com.
new-fp-shed.wg1.b.yahoo.com has address 180.222.102.202
new-fp-shed.wg1.b.yahoo.com has address 180.222.102.201
new-fp-shed.wg1.b.yahoo.com has IPv6 address 2406:2000:ec:c58::3001
new-fp-shed.wg1.b.yahoo.com has IPv6 address 2406:2000:ec:c58::3000

Similarly we can use below command to get one of the IP v4 address

j@ubuntu:~/tmp/1030$ host www.yahoo.com | grep "has address" | head -1 |awk '{print $NF}'
180.222.102.202

Method 4: use dig command

Dig is a flexible tool for interrogating DNS name servers. It performs DNS lookups and displays the answers that are returned from the name server(s) that were queried. Most DNS administrators use dig to troubleshoot DNS problems because of its flexibility, ease of use and clarity of output. Other lookup tools tend to have less functionality than dig.

For example:

j@ubuntu:~/tmp/1030$ dig +short www.yahoo.com
new-fp-shed.wg1.b.yahoo.com.
180.222.102.201
180.222.102.202

Method 5: use resolveip command

The resolveip utility resolves host names to IP addresses and vice versa. This command is easy to use but seems not available by default.

j@ubuntu:~/tmp/1030$ resolveip www.yahoo.com
IP address of www.yahoo.com is 180.222.102.201
IP address of www.yahoo.com is 180.222.102.202

Method 6 : use command nslookup

Nslookup is a program to query Internet domain name servers.

j@ubuntu:~/tmp/1030$ nslookup www.yahoo.com
Server:		127.0.0.53
Address:	127.0.0.53#53

Non-authoritative answer:
www.yahoo.com	canonical name = new-fp-shed.wg1.b.yahoo.com.
Name:	new-fp-shed.wg1.b.yahoo.com
Address: 180.222.102.201
Name:	new-fp-shed.wg1.b.yahoo.com
Address: 180.222.102.202
Name:	new-fp-shed.wg1.b.yahoo.com
Address: 2406:2000:ec:c58::3000
Name:	new-fp-shed.wg1.b.yahoo.com
Address: 2406:2000:ec:c58::3001

j@ubuntu:~/tmp/1030$

To get one of the IP v4 addresses only , you can try below command:

j@ubuntu:~/tmp/1030$ nslookup www.yahoo.com | awk '/^Address: / { print $2 ; exit }'
180.222.102.202

Bash script example

#!/bin/bash
hostname=www.yahoo.com

ip=$(ping -q -c1 -t1 $hostname 2>/dev/null | tr -d '()' | awk '/^PING/{print $3}')

if [ -n "$ip" ]; then
    echo IP: $ip
else
    echo "Could not resolve hostname $hostname."
fi

Where 2>/dev/null suppressed errors.