Bash a complete list of IP addresses for a domain

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

First I create a list of domain options, for example:

webkinz.com www.webkinz.com 

Then I loop around this list and run dig for each change as follows:

  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 IP address is as follows:

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

Only 3 problems .: - (

  • Pass returned domains when I was expecting only ip addresses.
  • Some, like my script removed spaces between domains.
  • Some of the ip addresses from dig www.webkinz.com missing.

So how do I do this? How can I find out if dig returned a different domain instead of an IP address and ran it in this domain? Am I just ignoring the domain names returned from dig and is the number enough for the IP addresses? I want to catch every IP address that will be resolved in the domain, if possible. I did not think it should be so difficult. Any ideas?

+7
source share
4 answers

To get only 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 an echo with an argument without quotes reduces newline characters except at the end.

You do not need intermediate variables or temporary files.

+4
source

Use the following modification in the script to resolve DNS names if it is not an ip address

 while read domain; do IPs=`dig $domain | grep $domain | grep -v ';' | awk '{ print $5 }'`; # detect if '$IPs' is an ip address grep "\([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}" <(echo $IPs) >/dev/null 2>&1 if [ $? -eq 0 ]; then # if IPs is an ip address add it to the file echo " ${IPs}" >> /tmp/IPs; #array else # if not, resolve the domain name using the 'host' command (take just the first line using 'head -1') host $IPs | grep "has address" | head -1 | awk '{ print $4 }' >> /tmp/IPs fi done < mylist 
0
source

dig gives different types of responses, so it’s possible that the fifth column contains domain names. The fifth column will be an IP address only if the response row is answer A I would suggest:

 dig -t A $domain 

instead

 dig $domain 

to limit the type.

0
source

I know that this has already been answered; however, for a list of IPv4 and IPv6 addresses, try the following:

Script:

 info=$(host google.com); echo "$info" | grep "has address" | awk '{print $4}'; echo "$info" | grep "IPv6" | awk '{print $5}' host - get the IP addresses grep - filter the addresses awk - print the correct strings 

script (fewer lines):

 host google.com | awk '/address/ {print $NF}' 

Output:

 74.125.45.102 74.125.45.113 74.125.45.138 74.125.45.139 74.125.45.100 74.125.45.101 2607:f8b0:4002:c01::8a 
0
source

All Articles