使用 Awk 将服务器名称从列表传递到 nslookup

Passing name of server from a list to nslookup with Awk

背景故事:我正在编写一个脚本来检查域黑名单并查看哪些域仍然有效(解析为 IP),因此我可以从列表中删除旧的非解析域。该列表有数百万行,所以我使用 awk(而不是 "do while read")来提高速度。

我正在尝试编写一个 awk 语句,该语句将 nslookup 域列表并仅将可解析域列表打印到另一个列表。

我几乎完成了,只是卡在了一部分上 - 如何指定 nslookup 使用的服务器?

我有 -port=54 工作,但我也在尝试配置 nslookup 使用的 DNS 服务器。

awk '{print }' /etc/pihole/gravity.list | nslookup -port=54| awk '/[Nn]ame/ {print $NF}'  >> /etc/pihole/gravityProcessed.list

如果我尝试指定 -server= 这不是有效参数 如果我尝试将 nslookup 更改为使用 1.1.1.1,而不是使用 1.1.1.1 作为服务器,它会尝试使用 nslookup 1.1.1.1。

awk '{print }' /etc/pihole/gravity.list | nslookup 1.1.1.1 | awk '/[Nn]ame/ {print $NF}'  >> /etc/pihole/gravityProcessed.list

问题是 nslookup 没有 -server 参数 afaik(是的,它有一个 -port 参数) 所以我需要 awk 来做:

nslookup [INSERT HOST] server -port=

这里是 /etc/pihole/gravity.list

的示例
google.com
yahoo.com
skype.com
microsoft.com

我想尝试合并的另一个选项是正确域语法的正则表达式,因为如果脚本遇到格式不正确的域,它当前就会死掉。就像把这个通过 grep (?=^.{4,253}$)(^(?:[a-zA-Z0-9](?:(?:[a-zA-Z0-9\-]){0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,}$)

如果您想使用备用名称服务器,请查看以下针对 nslookup 的说明:

ARGUMENTS
Interactive mode is entered in the following cases:
1. when no arguments are given (the default name server will be used)
2. when the first argument is a hyphen (-) and the second argument is the host name or Internet address of a name server.

所以试试

nslookup - 1.1.1.1 < /etc/pihole/gravity.list 2>/dev/null | 
   awk '/[Nn]ame/ {print $NF}' >> /etc/pihole/gravityProcessed.list