Welcome to the most active Linux Forum on the web.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 08-23-2006, 12:44 PM   #1
Registered: Apr 2005
Distribution: Fedora,Trustix,FreeBSD
Posts: 62

Rep: Reputation: 15
A script to thoroughly resolve a hostname into IP addresses

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

 * paramater 1: a string
 * return: 0 hostname
 * return: 1 ip address
function is_ip_address()
        R=`echo $1 | egrep "[a-z]"`
        if [ -z "$R" ]
                return 1
        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 ]]
                echo "$1"
        for a in `dig $1 +short`
                resolve_hostname $a

# general way to call the function
resolve_hostname ""
# you can also assign the output to a variable
# and make the results unique
r=`resolve_hostname "" | sort |uniq`
echo $r
Old 08-23-2006, 05:40 PM   #2
LQ Guru
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

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


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
DNS bind cant resolve IP and hostname dwarf007 Linux - Software 1 01-14-2006 09:42 AM
Unable to resolve hostname adirock2 Linux - Networking 14 07-06-2005 03:08 AM
Unable to ping web addresses, but able to resolve them? boyd98 Linux - Networking 3 03-27-2005 03:37 PM
Trying to resolve IP Addresses? ajrosen Linux - Newbie 1 04-16-2004 12:20 PM
Internet Sharing can resolve external ip addresses but no access tragic_comic Linux - Newbie 2 10-13-2003 08:29 AM

All times are GMT -5. The time now is 01:19 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration