Parse output of Host command and place into a variable

Hi,

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 bbc.co.uk | 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



Thx
Nev
LVL 1
collinsnAsked:
Who is Participating?
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.

woolmilkporcCommented:
Why not just

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

or

VAR=$(host bbc.co.uk | 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.
0
collinsnAuthor Commented:
Hi,

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 google.co.uk
google.co.uk has address 216.58.208.35
google.co.uk has IPv6 address 2a00:1450:4009:80d::2003
google.co.uk mail is handled by 10 aspmx.l.google.com.
google.co.uk mail is handled by 50 alt4.aspmx.l.google.com.
google.co.uk mail is handled by 30 alt2.aspmx.l.google.com.
google.co.uk mail is handled by 20 alt1.aspmx.l.google.com.
google.co.uk mail is handled by 40 alt3.aspmx.l.google.com.

# host ps4updptl.uk.np.community.playstation.net
ps4updptl.uk.np.community.playstation.net is an alias for elb001-p4ci01.p4ci.usw2.np.cy.s0.playstation.net.
elb001-p4ci01.p4ci.usw2.np.cy.s0.playstation.net is an alias for p4ci-np-s-loadbala-sv3h6h0sxu8-369221137.us-west-2.elb.amazonaws.com.
p4ci-np-s-loadbala-sv3h6h0sxu8-369221137.us-west-2.elb.amazonaws.com has address 54.213.83.95
p4ci-np-s-loadbala-sv3h6h0sxu8-369221137.us-west-2.elb.amazonaws.com has address 52.11.91.131
p4ci-np-s-loadbala-sv3h6h0sxu8-369221137.us-west-2.elb.amazonaws.com has address 54.218.107.56
p4ci-np-s-loadbala-sv3h6h0sxu8-369221137.us-west-2.elb.amazonaws.com has address 52.11.92.13
p4ci-np-s-loadbala-sv3h6h0sxu8-369221137.us-west-2.elb.amazonaws.com has address 54.191.178.65
p4ci-np-s-loadbala-sv3h6h0sxu8-369221137.us-west-2.elb.amazonaws.com has address 54.201.171.253
p4ci-np-s-loadbala-sv3h6h0sxu8-369221137.us-west-2.elb.amazonaws.com has address 52.11.88.38
p4ci-np-s-loadbala-sv3h6h0sxu8-369221137.us-west-2.elb.amazonaws.com has address 52.11.90.17
0
woolmilkporcCommented:
Try this:

VAR=$(host bbc.co.uk | awk '/has address/ {print $NF; exit}')
echo $VAR
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

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

A bit shorter and in case you don't want to use awk :)
0
woolmilkporcCommented:
@Gerwin,

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 "ps4updptl.uk.np.community.playstation.net" posted by collinsn).

They're after the first IPv4 address instead (54.213.83.95 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.
0
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" :)
0
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.
0
woolmilkporcCommented:
Hi,

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 bbc.co.uk | 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
 fi
fi
[[ $VALID = "true" ]] && echo $VAR is valid || echo $VAR is invalid
0
woolmilkporcCommented:
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 bbc.co.uk 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 bbc.co.uk >/dev/null  2>&1
[[ $? -eq 0 ]] && echo "IPv4 networking active" || echo "No IPv4 networking or host not found"
0

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
Linux

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.