I'd like to be able to return only the IP from a nslookup command. Currently if I execute:
>nslookup foo21.bar.local
it will return something like:
Server: 11.13.5.134
Address: 11.13.5.134#53
Name: foo21.bar.local
Address: 11.13.35.312
I would like a command that returns just the:
11.13.35.312
Thanks for any answers.
CodePudding user response:
dig
has options to make that easy:
dig short foo21.bar.local
will just give you the A records, one per line (a domain can have more than one A record). To only get the first one:
dig short foo21.bar.local | head -n1
CodePudding user response:
Using dig
as @Marcus suggest is the most clean way.
To answer your question, we can use awk to get only the line with Address
and then get the second col:
nslookup google.com | awk '/^Address: / { print $2 }'
Will output:
216.58.208.110