0

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
ec2-107-20-169-186.compute-1.amazonaws.com
ec2-13-58-191-91.us-east-2.compute.amazonaws
ec2-18-204-248-223.compute-1.amazonaws.com
ec2-18-207-50-150.compute-1.amazonaws.com
ec2-18-234-32-173.compute-1.amazonaws.com
ec2-34-192-54-86.compute-1.amazonaws.com
ec2-34-195-196-96.compute-1.amazonaws.com
ec2-34-206-216-146.compute-1.amazonaws.com
?...^C

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
       8

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

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 .