Bash 域的 IP 地址综合列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11128451/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 02:32:34  来源:igfitidea点击:

Bash comprehensive list of IP addresses for a domain

bashsedawkgrep

提问by exvance

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).

我正在尝试为给定的域名生成所有可能的 IP 地址的列表。我想我很接近但不知道我错过了什么(或者是否有更好的方法)。

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:

然后我遍历这个列表并对每个变体运行 dig ,如下所示:

 while read domain; do
    IPs=`dig $domain | grep $domain | grep -v ';' | awk '{ print  }'`;
    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:

我的 IPs 文件如下所示:

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

Only 3 problems. :-(

只有3个问题。:-(

  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.comare missing.
  1. 当我只期待 IP 地址时挖掘返回的域。
  2. 我的脚本如何删除域之间的空格。
  3. 部分 ip 地址dig www.webkinz.com丢失。

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?

那么,我该怎么做呢?我是否以某种方式弄清楚 dig 是否返回另一个域而不是 IP 地址并在该域上运行 dig ?我是否只是忽略从 dig 返回的域名并认为 IP 地址就足够了?如果可能,我想捕获将解析为域的每个 IP 地址。我不认为它应该这么难。有任何想法吗?

采纳答案by Paused until further notice.

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

为了仅获取 IP 地址,请使用dig +short

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

or

或者

info=$(host google.com); echo "$info" | grep "has address" | awk '{print }'; echo "$info" | grep "IPv6" | awk '{print }'

host - get the IP addresses
grep - filter the addresses
awk - print the correct strings

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

使用带有不带引号的参数的 echo 会删除除末尾之外的换行符。

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

您不需要任何中间变量或临时文件。

回答by Jared Burrows

I know this already answered; however, for a list of IPv4 and IPv6 addresses, try this:

我知道这已经回答了;但是,对于 IPv4 和 IPv6 地址列表,请尝试以下操作:

Script:

脚本:

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

script (less lines):

脚本(更少的行):

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

Output:

输出:

while read domain; do
    IPs=`dig $domain | grep $domain | grep -v ';' | awk '{ print  }'`;

    # 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  }' >> /tmp/IPs
    fi

done < mylist

回答by higuaro

Use the following modification in your script to resolve the dns names when is not an ip address

当不是 ip 地址时,在脚本中使用以下修改来解析 dns 名称

dig -t A $domain

回答by musiphil

diggives different types of responses, so it's possible that the fifth column contains domain names. The fifth column will be IP addresses only when the response line is an Aresponse. I would suggest:

dig给出了不同类型的响应,因此第五列可能包含域名。仅当响应行是A响应时,第五列才是 IP 地址。我会建议:

dig $domain

instead of

代替

##代码##

to restrict the type.

来限制类型。