I am trying to ping several IP addresses / hostnames, and determine whether any of them respond.
Starting from this question, I'm using the echo host1 host2 host3 | xargs -n1 -P0 ping -c1 -w2 approach.
My problem is that ping will return zero if the ping succeeds and non-zero (actually 1) if the ping fails.
This means, that the overall command returns 123 if any of the pings fail, and 0 if all pings succeed, which isn't what I want.
I can write a hacky script like:
#!/bin/bash
ping "$@"
exit $(( 1 - $? ))
And then use that script in the xargs parameters instead of the real ping, which achieves what I want, but feels very messy.
Is there a better way to achieve what I need, without having to install non-standard ping tools, or have hacky external scripts.
As xargs is a command, I don't think I can use a shell function in place of my external script either, which would have given a slightly more elegant option (allowing one script to hold all the code, rather than needing a second script to call from the script where I am calling xargs from.
fpinginstead ofping?