In this article, we will explain how to resolve a hostname/domain name to an IPv4 and IPv6 address in a Bash script. However, before proceeding towards creating the script, let us review some of the commands that can be used to resolve the hostname/domain name to an IP address.
Ping
Ping is the most simple and built-in tool that is available on almost all operating systems. It is used to verify the reachability of a host in a network. However, we can also used it to find the IP address against any hostname/domain name. Use the following syntax to find the IP address of a targeted hostname/domain name:
Nslookup
Nslookup is widely used to resolve the hostname to an IP address. In order to use this command for an IP lookup, use the following syntax:
Host
Another command-line utility “host” can be used to find IP address against any hostname/domain name. In order to use this command, use the following syntax:
Dig
Dig is another useful command line tool that is used to query various DNS related records. It can be used to find IP address against any hostname/domain name. Use Dig command in the following way to find an IP address against a specific hostname/domain name.
Bash script to resolve a hostname to an IP address
In order to use the bash script for an IP lookup, follow the below steps:
- Create a bash file using any text editor. Here I will be using the Nano editor to create a script named “iplookup.sh”.
$ sudo nano script.sh
- Copy-paste the following lines in your script file. Note that, here in this script, I am specifying Google’s public DNS server for IP lookup. You can specify any other DNS server as per your environment.
# Specify DNS server
dnsserver="8.8.8.8"
# function to get IP address
function get_ipaddr {
ip_address=""
# A and AAA record for IPv4 and IPv6, respectively
# $1 stands for first argument
if [ -n "$1" ]; then
hostname="${1}"
if [ -z "query_type" ]; then
query_type="A"
fi
# use host command for DNS lookup operations
host -t ${query_type} ${hostname} &>/dev/null ${dnsserver}
if [ "$?" -eq "0" ]; then
# get ip address
ip_address="$(host -t ${query_type} ${hostname} ${dnsserver}| awk '/has.*address/{print $NF; exit}')"
else
exit 1
fi
else
exit 2
fi
# display ip
echo $ip_address
}
hostname="${1}"
for query in "A-IPv4" "AAAA-IPv6"; do
query_type="$(printf $query | cut -d- -f 1)"
ipversion="$(printf $query | cut -d- -f 2)"
address="$(get_ipaddr ${hostname})"
if [ "$?" -eq "0" ]; then
if [ -n "${address}" ]; then
echo "The ${ipversion} adress of the Hostname ${hostname} is: $address"
fi
else
echo "An error occurred"
fi
done - Once done, use Ctrl+O and Ctrl+X to save and exit the file respectively.
- Now to find an IP address against a targeted hostname/domain name, run the script using the following syntax:
$ ./script.sh target-host
For instance, to resolve the IP address of “google.com”, the command would be:
$ ./iplookup.sh google.comThe output would be similar to this:
Similarly, to resolve the IP address of “yahoo.com”, the command would be:
$ ./iplookup.sh yahoo.comThe output would be similar to this:
That is all there is to it! In this article, we have learned to resolve the hostname to an IPv4 and IPv6 address using a bash script. We also learned some other command-line tools such as Ping, Nslookup, Host, and Dig that can be used to perform an IP lookup.
from Linux Hint https://ift.tt/3dIbHX6
0 Comments