LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   A script to thoroughly resolve a hostname into IP addresses (https://www.linuxquestions.org/questions/programming-9/a-script-to-thoroughly-resolve-a-hostname-into-ip-addresses-476625/)

ahz10 08-23-2006 11:44 AM

A script to thoroughly resolve a hostname into IP addresses
 
Since sometimes `dig example.com +short` returns more hostnames, here's a script to thoroughly resolve all the results into only IP addresses.

Code:

#!/bin/bash
/**
 * paramater 1: a string
 * return: 0 hostname
 * return: 1 ip address
 */
function is_ip_address()
{
        R=`echo $1 | egrep "[a-z]"`
        r=$?
        if [ -z "$R" ]
        then
                return 1
        fi
        return 0

}

#
# parameter 1: a hostname to resolve
# side effects: echos IP addresses (which may not be unique)
function resolve_hostname()
{
        is_ip_address $1
        if [[ $? -eq 1 ]]
        then
                echo "$1"
                return
        fi
        for a in `dig $1 +short`
        do
                resolve_hostname $a
        done
}


# general way to call the function
resolve_hostname "www.concentric.net"
# you can also assign the output to a variable
# and make the results unique
r=`resolve_hostname "real.npr.org" | sort |uniq`
echo $r


Matir 08-23-2006 04:40 PM

Moved: This thread is more suitable in Programming and has been moved accordingly to help your thread/question get the exposure it deserves.


All times are GMT -5. The time now is 12:15 PM.