使用bash和awk从多个网站提取IP

时间:2018-06-30 18:43:20

标签: bash awk ip nslookup

我正在寻找获取多个网站IP并将其输出为"domain.com:","1.1.1.1"的最佳方法,以实现这一目标,我正在考虑使用nslookup(不确定是否是最佳选择,但我不想使用ping)

我正在尝试类似的事情:

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

有了这个,我得到以下输出:

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

预期产量

"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内容:

google.com
yahoo.com
example.com

我试图这样做,但是在输出的"domain.com",下找不到正确的域。

我不确定是否使用$ domain或awk,谁能帮助我获得正确的语法。请注意,我需要"domain.com",内的请求域,而不是nslookup

中的Name:

谢谢。

1 个答案:

答案 0 :(得分:2)

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

输出:

"mail.yahoo.com","87.248.116.12"
"mail.yahoo.com","87.248.116.11"