Link to home
Start Free TrialLog in
Avatar of collinsn
collinsnFlag for United Kingdom of Great Britain and Northern Ireland

asked on

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
Avatar of woolmilkporc
woolmilkporc
Flag of Germany image

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.
Avatar of collinsn

ASKER

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
Try this:

VAR=$(host bbc.co.uk | awk '/has address/ {print $NF; exit}')
echo $VAR
VAR=$(host bbc.co.uk | head -1 | cut -d" " -f4)

A bit shorter and in case you don't want to use awk :)
@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.
You're right, adding grep -v alias would fix that but that's almost the same as grepping for "has address" :)
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.
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
ASKER CERTIFIED SOLUTION
Avatar of woolmilkporc
woolmilkporc
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial