My overall task is to ping each EC2 server I happen to be connected to.

I am using this command to do that:

netstat -W | grep ec2 | cut -d " " -f 18 | sort -u | cut -d "." -f 1,2,3,4 | xargs -0 -p "ping -c 10"

It outputs the right output, but is not calling xargs per line:

ping -c 10 ec2-107-20-154-211.compute-1.amazonaws.com

The written explanation of each step is:

  1. Run netstat with the -W flag to get complete FQDNs and not short hostnames
  2. grep for ec2
  3. cut the output to get just the hostname column
  4. sort unique hostnames
  5. cut the hostname again to drop the portnumber from the end e.g. ec2-1-2-3.amazon.com.80 -> ec2-1-2-3.amazon.com
  6. xargs the output to ping each host ten times

I think I am running into an issue with either the OS X variant of xargs because piping the same output to wc -l shows eight lines:

netstat -W | grep ec2 | cut -d " " -f 18 | sort -u | cut -d "." -f 1,2,3,4 | wc -l

I am focusing on the xargs aspect here. That is the only part of this task that is not apparently working.

1 Answer 1


I solved this myself with:

netstat -W | grep ec2 | cut -d " " -f 18 | sort -u | cut -d "." -f 1,2,3,4 | xargs -p -L 1 ping -c 10

It provides the expected eight calls to ping, one for each host found.

I think this solution works because the -L flag limits it to one line per command.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .