1

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.

Timothy Clotworthy
  • 1,960
  • 2
  • 19
  • 42

2 Answers2

2

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
Marcus Müller
  • 34,677
  • 4
  • 53
  • 94
1

Using dig as @Marcus suggest is the most clean way.


To answer your question, we can use 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
0stone0
  • 34,288
  • 4
  • 39
  • 64