Extract IPs from multiples websites using bash and awk


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)


"domain.com:","1.1.1.1"


nslookup



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.


"domain.com",



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


"domain.com",


Name:



Thanks.




1 Answer
1


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



Output:





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 at 19:02






Okay, I've updated my answer.
– Cyrus
Jun 30 at 19:08





Cool, Thanks. I like the way you use awk.
– pancho
Jun 30 at 19:21





@EdMorton: I've updated my answer to fix the bug and tested it with Ubuntu 16.04.
– Cyrus
Jul 1 at 18:07






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

List of Kim Possible characters

Audio Livestreaming with Python & Flask

NSwag: Generate C# Client from multiple Versions of an API