Pass stdin into Unix host or dig command

前端 未结 4 1626
悲&欢浪女
悲&欢浪女 2021-02-09 11:57

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

相关标签:
4条回答
  • 2021-02-09 12:30

    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.

    0 讨论(0)
  • 2021-02-09 12:34

    Use xargs -l:

    tail -f access.log | xargs -l host
    
    0 讨论(0)
  • 2021-02-09 12:40

    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.

    0 讨论(0)
  • 2021-02-09 12:45

    You could also use the read builtin:

    tail -f access.log | while read line; do host $line; done
    
    0 讨论(0)
提交回复
热议问题