Skip to content

Instantly share code, notes, and snippets.

@kacchan822
Last active October 28, 2016 12:24
Show Gist options
  • Save kacchan822/f5a4bdd75563e8ea53cf to your computer and use it in GitHub Desktop.
Save kacchan822/f5a4bdd75563e8ea53cf to your computer and use it in GitHub Desktop.
rDNSlookup
#!/bin/bash
#
# Copyright (c) 2016 Katsuya SAITO
# This software is released under the MIT License.
# http://opensource.org/licenses/mit-license.php
#
# @(#) rDNSlookup.sh ver.1.0.2 2016.01.29
#
# Usage:
# rDNSlookup.sh file
# file: List of IP address, each line has an IP address.
#
# Description:
# Looking for a host name from a given IP addresses.
# Output is like a CSV file style... "IP_Address","FQDN"
#
#
###############################################################################
# CONFIGER SECTION #----------------------------------------------------------#
readonly DNSV="8.8.8.8" # DNS Server. Default is Google Public DNS
#-----------------------------------------------------------------------------#
# FUNCTION SECTION #-----------------------------------------------------------
function reverse_dns_lookup() {
ans=`dig @$DNSV -x $ipaddr PTR +short | sed -e 's/\.$//' 2>/dev/null`
if [ -z "$ans" ]; then
fqdn="No_HOST"
else
fqdn="$ans"
fi
return 0
}
function temp_check() {
count=`grep -c $ipaddr $TEMPFILE 2>/dev/null`
if [ $count = 0 ]; then
echo -n "$ipaddr," >> $TEMPFILE
return 0
else
return 1
fi
}
function cleanup() {
if [ -e $TEMPFILE ]; then
rm -f "$TEMPFILE"
fi
}
function cleanup_error() {
if [ -e $TEMPFILE ]; then
rm -f "$TEMPFILE"
fi
echo "Any error happend!!"
exit 1
}
#-----------------------------------------------------------------------------#
# SCRIPTS SECTION ------------------------------------------------------------#
if [ $# != 1 ]; then
echo "Useage: $0 ipaddresses_file"
echo " This scripts need an input file"
exit 1
fi
TEMPFILE=`mktemp`
trap cleanup EXIT
trap cleanup_error SIGHUP SIGINT SIGTERM
echo "IP_Address,FQDN"
for ipaddr in `cat $1`
do
temp_check
if [ $? = 0 ]; then
reverse_dns_lookup
echo "$fqdn" >> $TEMPFILE
echo "$ipaddr,$fqdn"
else
echo `grep $ipaddr $TEMPFILE`
fi
done
exit 0
#-----------------------------------------------------------------------------#
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment