Parse output of Host command and place into a variable


Is there a more efficient way to capture the 1st IP address from the output of the HOST command.

Currently I have the following:

host | awk '{match($0,/[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/); ip = substr($0,RSTART,RLENGTH); print ip}'|awk '/./' |awk 'NR==1{print $1}'

Open in new window

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Why not just

VAR=$(host | awk 'NR==1 {print $4}')
echo $VAR


VAR=$(host | awk 'NR==1 {print $NF}')
echo $VAR


What is the unfiltered output of your host command?
On many UNIXes you must use $3 instead of $4.
$NF should work anyway, however.
collinsnAuthor Commented:

Here is a couple of example output of HOST. I'm just interested in capturing the first IPV4 IP address.  It can be any HOST.

# host has address has IPv6 address 2a00:1450:4009:80d::2003 mail is handled by 10 mail is handled by 50 mail is handled by 30 mail is handled by 20 mail is handled by 40

# host is an alias for is an alias for has address has address has address has address has address has address has address has address
Try this:

VAR=$(host | awk '/has address/ {print $NF; exit}')
echo $VAR
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

Gerwin Jansen, EE MVETopic Advisor Commented:
VAR=$(host | head -1 | cut -d" " -f4)

A bit shorter and in case you don't want to use awk :)

your command will sometimes return "alias" or "address" or so, depending on the (varying!) output sequence of the "host" command (see the example for host "" posted by collinsn).

They're after the first IPv4 address instead ( in the example) - if I understood everything right, that is.

The fact that the output can vary in sequence makes the attribute "first" a bit questionable, however.
Gerwin Jansen, EE MVETopic Advisor Commented:
You're right, adding grep -v alias would fix that but that's almost the same as grepping for "has address" :)
collinsnAuthor Commented:
Sorry, I've been off sick for a while a completely forgot about this post. Both suggestion were accepted, but what I really needed was for it to validate the address as a valid ip4 address.

I hope you're well again!

I'm rather sure that "host" will never display an invalid IP, but you can check it anyway like this:

VAR=$(host | awk '/has address/ {print $NF; exit}')
if egrep -q  '^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$' <<<$VAR; then
 myIFS=$IFS; IFS='.'
 A=($VAR); IFS=$myIFS
 if [[ ${A[0]} -lt 256 && ${A[1]} -lt 256 && ${A[2]} -lt 256 && ${A[3]} -lt 256 ]]; then
[[ $VALID = "true" ]] && echo $VAR is valid || echo $VAR is invalid
On the other hand, if your goal is just to make sure that the returned address is an IPv4 address just use the "-4" option of "host"

VAR=$(host -4 2>/dev/null | awk '/has address/ {print $NF; exit}')
echo $VAR  ## if empty the host does not exist or uses only IPv6 networking.

Finally, if the address itself is of no interest and the only goal is to verify that the host acually has an IPv4 address this will suffice:

host -4 >/dev/null  2>&1
[[ $? -eq 0 ]] && echo "IPv4 networking active" || echo "No IPv4 networking or host not found"

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.