【问题标题】:Pass stdin into Unix host or dig command将标准输入传递给 Unix 主机或 dig 命令
【发布时间】:2010-11-17 19:45:59
【问题描述】:

假设我有一个 IP 列表进入我正在跟踪的日志:

1.1.1.1
1.1.1.2
1.1.1.3 

我想轻松地将它们解析为主机名。我希望能够

tail -f access.log | host - 

由于主机不以这种方式理解来自标准输入的输入而失败。无需编写静态文件或回退到 perl/python/etc.,最简单的方法是什么?

【问题讨论】:

    标签: host dig


    【解决方案1】:

    使用xargs -l:

    tail -f access.log | xargs -l host
    

    【讨论】:

    • 这实际上会打嗝,因为主机实际上将与主机 1.1.1.1 1.1.1.2 一起运行,从而导致在无效 DNS 服务器上进行 dns 查找。设置 "-d '\n'" 似乎没有任何帮助。
    • 使用“xargs -l”(或“xargs -L 1”)确保对每一行都运行该命令。
    【解决方案2】:

    您也可以使用 read 内置函数:

    tail -f access.log | while read line; do host $line; done
    

    【讨论】:

      【解决方案3】:

      在下面的命令中,如果需要,将cat 替换为tail -f 等。

      使用host

      $ cat my_ips | xargs -i host {}
      1.1.1.1.in-addr.arpa domain name pointer myhost1.mydomain.com.
      1.1.1.2.in-addr.arpa domain name pointer myhost2.mydomain.com.
      

      使用dig

      $ cat my_ips | xargs -i dig -x {} +short
      myhost1.mydomain.com.
      myhost2.mydomain.com.
      

      请注意,xargs-i 选项隐含了 -L 1 选项。

      要首先获取主机的 IP,请参阅 this answer

      【讨论】:

        【解决方案4】:

        在 bash 中你可以这样做:

        stdout | (dig -f <(cat))
        

        示例程序:

        (
        cat <<EOF
        asdf.com
        ibm.com
        microsoft.com
        nonexisting.domain
        EOF
        ) | (dig -f <(cat))
        

        这样你只调用一次'dig'。

        【讨论】:

          猜你喜欢
          • 2011-06-14
          • 1970-01-01
          • 2021-04-29
          • 2014-02-13
          • 1970-01-01
          • 1970-01-01
          • 2021-11-13
          • 1970-01-01
          • 2015-07-05
          相关资源
          最近更新 更多