Given an array of domains I would like to obtain their IP addresses, in the following format:

This script

domains=(yahoo.com yahoo.ca)
printf "%s\0" "${domains[@]}" | xargs -0 -t -n 1  ping -c 1 -w 3 | grep -E 'PING.*' | sed -r 's:^.*\((([0-9]{1,3}\.){3}[0-9]{1,3})\).*$:\1:'

gives the expected output. But initially I tried: printf "%s\0" "${domains[@]}" | xargs -0 -t -n 1 ping -c 1 -w 3 | head -n 1 | sed -r 's:^.*\((([0-9]{1,3}\.){3}[0-9]{1,3})\).*$:\1:' and the command exited with a signal 13 ( which,from what I understand, means broken pipe ?!?). Obviously the issue is with head . Now I would like to explore why it does not work and if and eventually how I could make this long “wiener” work with head. I have not tried it yet but I am pretty sure it would work using a for construct and using head for each ping. Using xargs with the -P option also does not work. I guess the main issue would be that head is fed xargs output as one continous file ( did I say right or is it “input” ?).

EDIT1: My goal was to obtain the IP addresses of a set of website addresses and then check to what organisations are those IPs registered too by usingwhois and piping its output to sed and then maybe do some grouping so I have only unique names, no repetitions. I agree that dig or host are much more thorough than ping but I am fairly certain the websites are small enough that only one IP address will be found for each domain. Nevertheless I think now I will go with dig ( or host; I have not decided yet) as others suggested.

To make it clear why I am interested in using head and not grep for what I need: I am just curious how it would be done(I am quite new to bash and linux in general) and I felt that it might clear up how piping works. That's all

  • I will be AFK for the next hour so don't expect a reply earlier folks – user293496 Jun 25 '14 at 11:18
  • It's not clear why you want to use head, or what is your purpose. If you use head to only output one result it will kill the rest of results, obviously. – LatinSuD Jun 25 '14 at 11:23
  • 1
    Moreover, if you only want to convert names to ip addresses using ping is one of the less efficient ways (rather use dig, host, nslookup). – LatinSuD Jun 25 '14 at 11:25
  • 1
    As host yahoo.com (or reading about DNS) will show, a hostname can be bound to multiple IP addresses. Why would you want only an arbitrary single IP address? – l0b0 Jun 25 '14 at 12:14
  • @LatinSuD see my edit. – user293496 Jun 25 '14 at 14:01

head -n 1 exits after the first line has been printed, and sends SIGPIPE to the writer (xargs) to say that there's no process left to read whatever it writes. In response xargs does what most processes do and kills its subprocesses (ping), prints a warning, probably sends SIGPIPE to its own parent, and then kills itself.

You'll notice, however, that since you haven't set pipefail in your script the exit code of the full script is zero - the exit code of the sed command.

echo|xargs -n1 ping -w 1

or with grep, see only non ping nodes

echo|xargs -n1 ping -w 1|grep -b1 100

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.