1

I'm looking for the best way to get multiples websites IPs and output them as "domain.com:","1.1.1.1" in order to achieve this I was thinking on using nslookup (not sure if is the best option but I don't want to use ping)

Well I was trying something like:

for domain in $(cat domains.txt)
do
   nslookup $domain 8.8.8.8 | awk '/Address: ([[:digit:]]+\.){3}/{gsub(/\.$/,"",$1); printf "\"%s\",\"%s\"\n",$1,$NF}'; done

With this, I'm get this output:

"Address:","64.233.190.101"
"Address:","64.233.190.138"
"Address:","64.233.190.100"
"Address:","64.233.190.139"
"Address:","64.233.190.113"
"Address:","64.233.190.102"
"Address:","98.137.246.8"
"Address:","98.138.219.231"
"Address:","72.30.35.9"
"Address:","72.30.35.10"
"Address:","98.138.219.232"
"Address:","98.137.246.7"
"Address:","93.184.216.34"

Expected Output

"google.com","64.233.190.101"
"google.com","64.233.190.138"
"google.com","64.233.190.100"
"google.com","64.233.190.139"
"google.com","64.233.190.113"
"google.com","64.233.190.102"
"yahoo.com","98.137.246.8"
"yahoo.com","98.138.219.231"
"yahoo.com","72.30.35.9"
"yahoo.com","72.30.35.10"
"yahoo.com","98.138.219.232"
"yahoo.com","98.137.246.7"
"example.com","93.184.216.34"

domains.txt content:

google.com
yahoo.com
example.com

I was trying to do this but I can’t get the correct domain under "domain.com", on the output.

I'm not sure if using $domain or awk, Can any one help me to get the correct syntax. Please note that I need requested domain inside "domain.com", not the Name: in nslookup

Thanks.

pancho
  • 176
  • 1
  • 2
  • 13

1 Answers1

2
domain="mail.yahoo.com"
nslookup "$domain" 8.8.8.8 | awk -v n="$domain" -F ' +' '$1=="Address:"{print "\""n"\",\""$2"\""}'

Output:

"mail.yahoo.com","87.248.116.12"
"mail.yahoo.com","87.248.116.11"
Cyrus
  • 84,225
  • 14
  • 89
  • 153
  • Thanks @Cyrus it does work but I need to get the domain requested. For example if I look for mail.yahoo.com I get "fd-geoycpi-uno.gycpi.b.yahoodns.net","200.152.162.137" and I need as output "mail.yahoo.com","200.152.162.137" – pancho Jun 30 '18 at 19:02
  • Cool, Thanks. I like the way you use awk. – pancho Jun 30 '18 at 19:21
  • 1
    @EdMorton: I've updated my answer to fix the bug and tested it with Ubuntu 16.04. – Cyrus Jul 01 '18 at 18:07