Pass stdin into Unix host or dig command

做~自己de王妃 提交于 2019-12-21 00:19:10

问题


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


回答1:


Use xargs -l:

tail -f access.log | xargs -l host



回答2:


You could also use the read builtin:

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



回答3:


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.



来源:https://stackoverflow.com/questions/1246814/pass-stdin-into-unix-host-or-dig-command

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!