Have you ever used the command line tool “nslookup” and were not to sure what was going on? Sometimes you just need that simple name associated with an IP address or an IP address associated with a name, so you really have no need for the other information that it returns, such as what nameserver it used to do the lookup on.
I set out to create a simple BASH script to be able to just return back what IP Address I looked up and what the hostname was that was associated with it, as well as what hostname I searched for and the IP address associated with that.
The following script is lengthy but it is very helpful in making things look nicer then the usual nslookup output. You can save the script to your home directory or put it in a directory included in your PATH, such as /usr/bin, this way you can call the script no matter what directory you are currently working in.
A sample output from nslookup when looking up “google.com” would return back:
Server: 192.75.233.35
Address: 192.75.233.35#53Non-authoritative answer:Name: google.comAddress: 74.125.226.16Name: google.comAddress: 74.125.226.17Name: google.comAddress: 74.125.226.18Name: google.comAddress: 74.125.226.19Name: google.comAddress: 74.125.226.20Server: 192.75.233.35Address: 192.75.233.35#53
Non-authoritative answer:
Name: google.com
Address: 74.125.226.16
Name: google.com
Address: 74.125.226.17
Name: google.com
Address: 74.125.226.18
Name: google.com
Address: 74.125.226.19
Name: google.com
Address: 74.125.226.20
With this script, the returned output would be:
Determining the IP address from hostname…
Hostname……: GOOGLE.COM
IP Address….: 74.125.226.17
74.125.226.18
74.125.226.19
74.125.226.20
74.125.226.16
Nice and simple, literally all it tells you is you looked up google.com and the IP addresses associated with it. To utilize the script open up a command line and create a new file inside “/usr/bin” for example, type “sudo nano -w /usr/bin/rdns” and enter your password. This will open up nano and you are editing the file “rdns” inside “/usr/bin”. Paste the following code inside this document and hit “CTRL+X” to save it. Once that is done, type now “sudo chmod +x /usr/bin/rdns” this will make the file executable. Now from anywhere, you can either just type “rdns” to be prompted what you want to look up or you can type “rdns <ipaddress>” or “rdns <hostname>” replacing <ipaddress> with an ipaddress, or <hostname> with a hostname.
This is currently configured to use the google dns servers. If you don’t want this and want to use your own, change the “8.8.4.4″ on the line stating “nameserver = 8.8.4.4″ to whatever your dns server is.
#!/bin/bash
#
# Specify the nameserver to use below
nameserver=8.8.4.4
#
#
NO_ARGS=0
ipaddr=”"
if [ $# -eq "$NO_ARGS" ] # should check for no arguments
then
clear
echo “”
echo -n “Please specify either IP or Hostname…: ”
read ipaddr
else
ipaddr=”$1″
fi
regex=”\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b”
CHECK=$(echo $ipaddr | egrep $regex)
if [[ "$?" -eq 0 ]]
then
clear
echo “”
echo “Determining the hostname from IP address…”
echo “”
LOOKUP_RES=`nslookup $ipaddr $nameserver`
FAIL_COUNT=`echo $LOOKUP_RES | grep “** server can’t find ” | wc -l`;
if [ $FAIL_COUNT -eq 1 ]
then
NAME=’IP Address Could Not Be Resolved’;
else
NAME=`echo $LOOKUP_RES | awk -F”= ” ‘/name/{print $2}’`
fi
echo -e “IP Address….: \t$ipaddr”
echo -ne “Hostname……:\t\t”
echo $NAME | tr ‘[:lower:]‘ ‘[:upper:]‘
echo “”
echo “”
else
clear
echo “”
echo “Determining the IP address from hostname…”
echo “”
LOOKUP_RES=`nslookup $ipaddr $nameserver`
FAIL_COUNT=`echo $LOOKUP_RES | grep “** server can’t find ” | wc -l`;
if [ $FAIL_COUNT -eq 1 ]
then
NAME=’Hostname Could Not Be Resolved’;
else
NAME=`nslookup $ipaddr $nameserver | grep Add | grep -v ‘#’ | cut -f 2 -d ‘ ‘`
fi
echo -ne “Hostname……:\t\t”
echo $ipaddr | tr ‘[:lower:]‘ ‘[:upper:]‘
echo -ne “IP Address….:\t\t$NAME”
echo “”
echo “”
fi
Popularity: 2%









