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

标签 linux bash unix awk nslookup

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

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

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

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

awk '{print $1}' /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 $1}' /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

关于linux - 使用 Awk 将服务器名称从列表传递到 nslookup,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58226187/

相关文章:

javascript - 如何替换最后出现的逗号

bash - 什么时候以及为什么我需要一个 's' 标志来按列排序

linux - bash 后台进程不工作,或者我错过了什么?

Linux/Unix — 管道输出以加入

shell - 使用 shell 脚本将值存储在数组中

带有一些表的 mysqldump,不使用 --skip-table

linux - 允许传入流量到 CentOS 服务器

linux - shell中的-f和-e有什么区别?

linux - 在 shell 脚本上带有多个参数的命令

c -/dev/random 不阻塞