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

Extract IPs from multiples websites using bash and awk

我正在寻找获取多个网站 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(/\.$/,"",); printf "\"%s\",\"%s\"\n",,$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:

谢谢。

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

输出:

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