Search code examples
bashawkipnslookup

Extract IPs from multiples websites using bash and awk


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.


Solution

  • 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"