0

I am writing a dns tool and require "| xargs host" and this works however the host command is doing the lookups agains the dns servers configured in /etc/resolv.conf.

I was running some tests and read the man page for xargs but cant figure out how to specify the dns server with host + xargs.

ex.

printf "google.com\nyahoo.com\nbing.com\n" > hosts.txt
cat hosts.txt | xargs -n1 host

This will lookup the 3 domains and output, but the dns server doing the lookup is the one configured under /etc/resolv.conf

To specify a dns server with host you typically just append the server at the end of the command:

"host google.com 8.8.8.8"

I am looking for something like this:

Mock up:

server_to_query=8.8.8.8 ; cat hosts.txt | xargs -n1 host [xargs input] $server_to_query

However, this is what is happening:

server_to_query=8.8.8.8 ; cat hosts.txt | xargs -n1 host $server_to_query [xargs input]

Results are $server_to_query is being looked up against [xargs input] and I'm looking for the opposite to happen.

If I cant do it with xargs I suppose I can just use a for loop, or use dig, but would prefer to use xargs and host.

1

Specify a replace-str with the -I option to xargs and it will do the replacement any where within the command, rather than just taking the argument to the end.

$ echo -e "google.com\nfarrellit.net\nexample.com" | xargs -I % -n 1 host % 8.8.8.8

Works perfectly for me in bash / osx:

Using domain server:
Name: 8.8.8.8
Address: 8.8.8.8#53
Aliases:

google.com has address 172.217.9.46
google.com has IPv6 address 2607:f8b0:4009:802::200e
google.com mail is handled by 40 alt3.aspmx.l.google.com.
google.com mail is handled by 50 alt4.aspmx.l.google.com.
google.com mail is handled by 20 alt1.aspmx.l.google.com.
google.com mail is handled by 10 aspmx.l.google.com.
google.com mail is handled by 30 alt2.aspmx.l.google.com.
Using domain server:
Name: 8.8.8.8
Address: 8.8.8.8#53
Aliases:

farrellit.net has address 99.84.254.112
farrellit.net has address 99.84.254.4
farrellit.net has address 99.84.254.33
farrellit.net has address 99.84.254.62
farrellit.net mail is handled by 10 mail3.farrellit.net.
Using domain server:
Name: 8.8.8.8
Address: 8.8.8.8#53
Aliases:

example.com has address 93.184.216.34
example.com has IPv6 address 2606:2800:220:1:248:1893:25c8:1946
example.com mail is handled by 0 .
|improve this answer|||||
  • Ahh okay I see. I was poorly interpreting the use of -I thank you very much! – Kirin Sep 27 '19 at 15:44
0

Typically the reason why you want to force which nameserver to ask is when you want to ask multiple different nameservers.

Using GNU Parallel you can do:

parallel host {1} {2} :::: hostlist.txt ::: 8.8.8.8 1.1.1.1

To see the results use --tag:

parallel --tag host {1} {2} :::: hostlist.txt ::: 8.8.8.8 1.1.1.1

Often your hostlist is quite big and by adding -j100 you can resolve 100 hosts in parallel.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.