Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash comprehensive list of IP addresses for a domain

Tags:

grep

bash

sed

awk

I'm trying to produce a list of all the possible ip addresses for a given domain name. I think I'm close but don't know what I'm missing (or if there is a better way).

First I create a list of variations of the domain like so:

 webkinz.com
 www.webkinz.com

I then loop over this list and run dig on each variation like so:

 while read domain; do
    IPs=`dig $domain | grep $domain | grep -v ';' | awk '{ print $5 }'`;
    echo " ${IPs}" >> /tmp/IPs; #array
 done < /tmp/mylist

 sort -u /tmp/IPs > /tmp/TheIPs; #remove duplicates
 cat /tmp/TheIPs| tr -d "\n" > /tmp/IPs  #remove new lines (making it 1 long line)

My IPs file looks like this:

  66.48.69.100 www.webkinz.com.edgesuite.net.a1339.g.akamai.net.

Only 3 problems. :-(

  1. Dig returned domains when I was only expecting ip addresses.
  2. Some how my script deleted the spaces between the domains.
  3. Some of the ip addresses from dig www.webkinz.com are missing.

So, how should I do this? Do I somehow figure out if dig returned another domain instead of an ip address and run dig on that domain? Do I just ignore domain names returned from dig and figure the ip addresses is sufficient? I want to catch every ip address that will resolve to the domain if possible. I didn't think it should be this hard. Any ideas?

like image 646
exvance Avatar asked Jun 20 '12 21:06

exvance


1 Answers

In order to get just the IP addresses, use dig +short:

#!/bin/bash
while read -r domain
do
    dig +short "$domain"
done < /tmp/mylist | sort -u | awk '{printf "%s ", $0} END {printf "\n"}' > outputfile

or

#!/bin/bash
echo $(xargs -a /tmp/mylist dig +short | sort -u) > outputfile

Using echo with an unquoted argument drops the newlines except at the end.

You don't need any intermediate variables or temporary files.

like image 102
Dennis Williamson Avatar answered Sep 25 '22 23:09

Dennis Williamson