首页 > 解决方案 > 在终端上运行批量 DNS + Whois 搜索并输出

问题描述

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

这很容易。

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

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

因此,当我将所有内容合并到一个 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 如下所示;

示例预期输出 Screengrab

谢谢你。

标签: bashshellcsvdnswhois

解决方案


您已经知道不同的域指向不同的 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,上面的脚本应该向您展示如何为列表中的每个域执行此操作。


推荐阅读