将 stdin 传递到 Unix 主机或 dig 命令

发布于 2024-07-30 06:35:09 字数 252 浏览 3 评论 0原文

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

1.1.1.1
1.1.1.2
1.1.1.3 

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

tail -f access.log | host - 

Which 失败,因为主机无法以这种方式理解来自 stdin 的输入。 无需编写静态文件或回退到 perl/python/等的最简单方法是什么?

Let's say I have a list of IPs coming into a log that I'm tailing:

1.1.1.1
1.1.1.2
1.1.1.3 

I'd like to easily resolve them to host names. I'd like to be able to

tail -f access.log | host - 

Which fails as host doesn't understand input from stdin in this way. What's the easiest way to do with without having to write a static file or fallback to perl/python/etc.?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

淤浪 2024-08-06 06:35:10

在 bash 中,您可以执行以下操作:

stdout | (dig -f <(cat))

示例程序:

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

这样您只需调用“dig”一次。

In bash You can do:

stdout | (dig -f <(cat))

Example program:

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

This way You only invoke 'dig' once.

最美不过初阳 2024-08-06 06:35:09

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

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

You could also use the read builtin:

tail -f access.log | while read line; do host $line; done
剪不断理还乱 2024-08-06 06:35:09

使用 xargs -l :

tail -f access.log | xargs -l host

Use xargs -l:

tail -f access.log | xargs -l host
清泪尽 2024-08-06 06:35:09

如果需要,在下面的命令中,将 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.

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

要首先获取主机的 IP,请参阅此答案

In the commands below, replace cat with tail -f, etc. if needed.

Using 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.

Using dig:

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

Note that the -i option to xargs implies the -L 1 option.

To first get the IPs of one's host, see this answer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文