#!/bin/sh ################################################# ## rdns v2.22 ## ## this script takes an IP address (or the ## ## first three octets of an IP address) and ## ## looks up all of names associated with the ## ## IP addresses within that class C network. ## ## ## ## more info: ## ## http://Business-PHP.com/opensource/rdns/ ## ## ## ## Copyright (C) 16 Feb 2003 Atom Emet ## ## Atom*Business-PHP.com ## ## ## ## This program is free software; you can ## ## redistribute it and/or modify it under the ## ## terms of the GNU General Public License as ## ## published by the Free Software Foundation; ## ## either version 2 of the License, or ## ## (at your option) any later version. ## ## ## ## This program is distributed in the hope ## ## that it will be useful, but WITHOUT ANY ## ## WARRANTY; without even the implied warranty ## ## of MERCHANTABILITY or FITNESS FOR A ## ## PARTICULAR PURPOSE. See the GNU General ## ## Public License for more details. ## ## ## ## You should have received a copy of the ## ## GNU General Public License along with this ## ## program; if not, write to ## ## the Free Software Foundation, Inc. ## ## 59 Temple Place - Suite 330 ## ## Boston, MA 02111-1307, USA ## ################################################# ################################################# ## check if the input appears to be an IP address ## or the first 3 octets of an IP address ## ip_test1=`echo "${1}" | tr '.' ' '` ip_test2=`echo "${1}" | egrep '^[0-9.][0-9.]*$'` ip_test3=`echo "${1}" | tr '.' ' ' | wc -w` ######################################################### ## the "1" in the following line will cause the following ## tests to be made even if there is no input ## ## although there is some redundancy in the following ## loop, it should not add any significant run time ## for x in ${ip_test1} 1 do if [ ! "${ip_test2}" ] || [ "${x}" -gt 255 ] || [ "${ip_test3}" -gt 4 ] || [ "${ip_test3}" -lt 3 ] then #################################################### ## if the argument doesn't seem to be an IP address, ## or is missing - issue an error and bail out ## echo "Usage: rdns [IP-Address]" >&2 exit 1 fi done ################################################################# ## we're only concerned with the first 3 octets of the IP address ## first_three_octets=`echo "${1}" | cut -d. -f1-3` ################################################################################## ## some systems have `jot`, other systems have `seq` ## few systems have both, so this function does their job and counts from 0 to 255 ## count () { n=0 while [ "${n}" -lt '256' ] do echo "${n}" n=`expr "${n}" + 1` done } ###################################################################### ## this is the fun part ## append a sequential number from the `count` function to ## the first three octets of the IP address and lookup each IP address ## for fourth_octet in `count` do ######################## ## lookup the IP address ## name=`host -t a "${first_three_octets}.${fourth_octet}" 2> /dev/null` ############################################### ## we'll check the exit status a few lines down ## name_err="$?" ################################################## ## get the name(s) associated with that IP address ## name=`echo "${name}" | cut -d' ' -f5-` ###################################################### ## did we successfully find a name for the IP address? ## if [ "${name_err}" = '0' ] && [ "${name}" ] && [ "${name}" != 'for' ] then if [ "${newline}" ] then ################################################################################## ## generate a line-feed *IF* this successful lookup follows an unsuccessful lookup ## echo '' fi ######################################## ## echo the IP address we just looked up ## followed by a literal tab ## and gobble up the newline ## echo "${first_three_octets}.${fourth_octet} " | tr -d '[\n\r]' ########################################################### ## echo the name(s) associated with the IP ## this is echoed on the same line as the IP, after the tab ## echo ${name} | sed 's!^for !!' ############################### ## unset the "newline" variable ## newline= else ########################################################### ## if we didn't successfully find a name for the IP address ## echo a '*' and gobble up the newline ## echo '*' | tr -d '[\n\r]' ############################# ## set the "newline" variable ## newline=1 fi done ################################################## ## at this point, we've looked up 256 IP addresses ## it's almost quittin' time ## if [ "${newline}" ] then ################################################################################### ## if the last IP wasn't successfully looked up, enter a line-return before exiting ## echo '' fi