LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices

Reply
 
Search this Thread
Old 08-23-2006, 12:44 PM   #1
ahz10
Member
 
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 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
 
Old 08-23-2006, 05:40 PM   #2
Matir
Moderator
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

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


Reply


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 07:47 AM.

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