0

I'm looking to print the name and IP that results from an nslookup so I can output them to a file.

When I run an nslookup on a host, I get a result like this:

Server: 10.10.10.10
Address: 10.10.10.10#53

Name: EXAMPLE.example.com
Address: 10.10.10.100

I want to output the name and IP into separate columns of a file, so my command right now, is this:

nslookup EXAMPLE.example.com | awk 'FNR ==4 {print$2}' | awk 'FNR ==5 {print$2}'

I'm getting nothing even though the separate awk's return the right value on their own. I realize why this won't work, but I'm wondering the right syntax for printing out both of these results into their respective columns.

4
  • 1
    can you please post an example of desired output – vfbsilva May 31 '18 at 18:59
  • Will there always be exactly one A record returned? – Jeff Schaller May 31 '18 at 18:59
  • The problem seem to be the second AWK is getting as input the sole word (field) parsed by the first awk, hence the output is empty as you don't have a second field on it – vfbsilva May 31 '18 at 19:07
  • 1
    ... the second awk is only getting a single record (FNR==4) - so FNR==5 will never be true – steeldriver May 31 '18 at 19:11
1
nslookup EXAMPLE.example.com \
| awk '/^Name:/ {N=$2}; /^Address:/ {A=$2}; END {print N,A}'

Will print only the last name and address, but if you want them all:

nslookup EXAMPLE.example.com \
| awk '/^Name:/ {N=$2}; /^Address:/ {print N,$2}'
4
  • Doesn't seem to mach properly: nslookup www.americanas.com.br Server: 201.21.192.119 Address: 201.21.192.119#53 Non-authoritative answer: www.americanas.com.br . Name: e6654.dscg.akamaiedge.net Address: 2.23.99.232 Name: e6654.dscg.akamaiedge.net Address: 2600:1419:a000:182::19fe Name: e6654.dscg.akamaiedge.net Address: 2600:1419:a000:18e::19fe nslookup www.americanas.com.br | awk '/^Name:/ {N=$1}; /^Address:/ {A=$2}; END {print N,A}' Name: 2600:1419:a000:182::19fe – vfbsilva May 31 '18 at 19:23
  • 1
    Perhaps you should execute the command I gave you, iso. some variation. – Gerard H. Pille May 31 '18 at 19:32
  • @gerard-h-pille what do you mean? – vfbsilva May 31 '18 at 19:38
  • 1
    You executed another command than the one in my answer. – Gerard H. Pille May 31 '18 at 19:39
1

Using dig instead:

$ dig +noall +answer example.com | awk '{ print $1, $NF }'
example.com. 93.184.216.34

$ dig +noall +answer www.google.com | awk '{ print $1, $NF }'
www.google.com. 172.217.20.36

The awk script prints the first and last whitespace-delimited fields from the output of the dig command (the raw output also contains a few other columns in-between these).


Your command:

nslookup EXAMPLE.example.com | awk 'FNR ==4 {print$2}' | awk 'FNR ==5 {print$2}'

This does not work since the second awk program is trying to get the fifth line from the output of the first awk program, which only outputs one single line.

Something like this might have worked,

$ nslookup example.com | awk 'FNR == 4 || FNR == 5 { print $2 }'
answer:
example.com

but on my system, as you can see, the wanted data is on other lines (not line 4 and 5). This may work better for you.

0
nslookup -type=A cdnjs.cloudflare.com | awk '/^Name:/ {c=2;N=$2} !--c {print N,$2}'

which will print all records:

cdnjs.cloudflare.com 104.16.18.94
cdnjs.cloudflare.com 104.16.19.94

Unlike other solutions, this script will only match the actual answer and not the DNS server address. It won't output anything in the case of failure.

This was inspired by an answer from this question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.