8

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

Asclepius
  • 57,944
  • 17
  • 167
  • 143
tomasz
  • 323
  • 1
  • 4
  • 13

4 Answers4

26

Use xargs -l:

tail -f access.log | xargs -l host
zb226
  • 9,586
  • 6
  • 49
  • 79
Sinan Taifour
  • 10,521
  • 3
  • 33
  • 30
  • This will actually hiccup as host will actually be run with host 1.1.1.1 1.1.1.2 Causing a dns lookup on an invalid DNS server. Setting "-d '\n'" doesn't seem to help it any. – tomasz Aug 07 '09 at 20:53
  • 7
    Use "xargs -l" (or "xargs -L 1") to ensure that the command is run for each line. – Jukka Matilainen Aug 07 '09 at 22:56
6

You could also use the read builtin:

tail -f access.log | while read line; do host $line; done
Jukka Matilainen
  • 9,608
  • 1
  • 25
  • 19
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.

Community
  • 1
  • 1
Asclepius
  • 57,944
  • 17
  • 167
  • 143
0

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.

user2692263
  • 475
  • 4
  • 8