-1

First time poster so my apologies if this has been covered in a previous topic that I was unable to locate. Basically I'm tasked with creating a script to perform a NSLookup on 50 domain names, format the results and pass them to the hosts file. I'll worry about checking and overwriting duplicate entries later.

Example:

Input: nslookup www.cbc.ca

Result: Name: a1849.gc.akamai.net Addresses: 184.50.238.64, 184.50.238.89 Aliases: www.cbc.ca, www.cbc.ca.edgesuite.net

Eventual Output: #184.50.238.64 www.cbc.ca a1849.gc.akamai.net

I figured this was possible with grep, awk and sed but have been messing about with switches and haven't gotten the right combination (mostly cause I'm not the most learned when it comes to regular expressions.) I'm partial to vbs, batch, cmd suggestions as well.

Thanks in advance for the time and effort! :)

Zivs
  • 1
  • 2
  • Using `host` instead of `nslookup` might make the parsing a bit easier, if your system has it... It generally produces only a single line of output instead of the multi-line report that `nslookup` creates. – twalberg Jan 20 '15 at 22:07
  • Hey, I appreciate the feedback and have since switched to using host vs nslookup (I'd prefer dig +short but those aren't available to me) since as you mentioned it's considerably easier to parse. Thanks for the suggestion! – Zivs Jan 21 '15 at 17:44

1 Answers1

0
nslookup $NAME | awk -v name="$NAME" 'BEGIN{hit=0; addr=""; alias=""} /answer:/{hit=1} /^Address:/{if (hit == 1 && "" == addr) addr=$2} /^Name:/{alias=alias " " $2} END{print(addr, name, alias)}'

Only one address and would not solve multiple identical names like nslookup google.com...

lonewasp
  • 106
  • 3