【问题标题】:Run Batch DNS + Whois Search on Terminal with Output在终端上运行批量 DNS + Whois 搜索并输出
【发布时间】:2019-01-15 11:32:17
【问题描述】:

我正在尝试在我的终端上将一些 DNS 记录搜索的批量搜索与 Whois 搜索结合起来。我有一个包含域列表的 CSV 文件,我想运行以下批量搜索器:

  • MX搜索:host -t mx $domain
  • NS 搜索:host -t ns $domain

这很容易。

将其与 Whois 搜索结合使用;它只返回一些 Whois 数据的摘要;我需要在 whois 服务器上查询域,这很好: 谁是

我可以使用-h,只记录域名注册人的详细信息,例如电话、国家代码等。我试过这个:

  • Whois:whois -h 'Registrar WHOIS Server:' "domain" 这也为我提供了仅注册人详细信息的输出。

因此,当我将所有内容合并到一个 bash 文件中时,我得到:

#!/usr/bin/env bash

file="${1:-input_test1.csv}"

if [[ ! -f "$file" ]]; then
    printf 'No file: %s\n' "$file" >&2
    exit 1
fi

(
  read -r header; printf '%s\n' "$header"
  while IFS=, read -r domain; do
    mx="$(host -t mx "$domain" | sort | head -1)"
    ns="$(host -t ns "$domain" | sort| head -1)"
    whois="$(whois -h "$(whois" "$domain" | grep 'Registrar WHOIS Server:') "$domain")
    printf '%s,"%s"\n' "$domain" "$mx" "$ns" "$whois"
  done
) < "$file"

我很想得到一个包含域的 CSV 输出,mx(只有 1 个),NS(只有 1 个),whois whois is registrant data 如下所示;

Sample Expected Output Screengrab

谢谢。

【问题讨论】:

    标签: bash shell csv dns whois


    【解决方案1】:

    您已经知道不同的域指向不同的 whois 服务器。我想您会发现每个注册商都有自己喜欢的通过 whois 呈现信息的方式,而且它们并不一致。 ICANN 要求通过 whois 提供最少的数据集,但您要查找的某些数据可能不属于该集。

    以下内容仅从 whois.internic.net 中删除基本数据,您可以将其用于收集 DNS 服务器、whois 服务器和 MX:

    #!/usr/bin/env bash
    
    mapfile -t domains < domains.lst
    
    declare -i i
    for this in "${domains[@]}"; do
      unset a; declare -A a=()
      unset ns; declare -a ns=()
      whois=""
      i=0
      while IFS=: read -r key value; do
        #printf "key=%s / value=%s\n" "$key" "$value"
        case "$key" in
          *"Registrar WHOIS Server") whois="${value## }" ;;
          *"Name Server") ns+=("${value## }") ;;
        esac
      done < <(whois -h whois.internic.net "$this")
      read mx < <(host -t mx "$this" | sort | awk 'NR==1{print $NF}')
    
      printf '%s,%s,%s,%s\n' \
        "$this" \
        "$mx" \
        "$whois" \
        "$(printf '%s ' "${ns[@]}")"
    done
    

    如果您真的想尝试从 $whois 处的 whois 数据中抓取,上面的脚本应该会向您展示如何为列表中的每个域执行此操作。

    【讨论】:

    • 您好 Ghoti,非常感谢您提供的优雅解决方案。我一直在尝试使用名为 test1.lst 的测试文件运行它,但我一直收到错误消息。 hunt2.sh: line 2: mapfile: command not found hunt2.sh: line 16: syntax error near unexpected token
    • 啊,您可能使用的是旧版本的 bash。 mapfile 命令是在大约十年前的 bash 4 中引入的。据我所知,唯一仍在提供 bash 3 的现代操作系统是 macOS。如果您使用的是 macOS,则可以使用 brewMacPorts 安装 bash 4。为了将来参考,最好将您正在使用的版本放在您的问题中,以避免被假设所困扰。
    • 谢谢ghoti,你太客气了。一旦我通过 bash 更新到 5,我就可以启动并运行脚本。
    • 脚本正在运行并在终端上打印输出,但未写入文件。我需要做些什么来将输出传输到文件或新文件吗?设法通过运行单个班轮并使用whois -h $(whois example.com | grep 'WHOIS Server:' | cut -f2- -d:) example.com' and i was hoping to modify the section here with that at: done rfc1036/whois
    • 我很高兴 - 我希望每个人都与 StackOverflow 建立长期而有用的关系,高质量的长期问题是其中的重要组成部分。 :) 也就是说,解析 cmets 中包含的代码非常困难。如果您认为此代码作为此问题的一部分有意义,请编辑问题并添加说明。如果您认为这是一个不同的问题,请执行ask a new question。一个包含太多部分的问题不太可能得到完整的答案。
    猜你喜欢
    • 1970-01-01
    • 2023-03-03
    • 1970-01-01
    • 1970-01-01
    • 2015-12-26
    • 1970-01-01
    • 1970-01-01
    • 2020-09-25
    • 2020-02-24
    相关资源
    最近更新 更多