I use nc as part of a verification script, and check the output of each nc command listed against what the expected output should be.e.g.,nc -zvw1 serv1.host.com 443 | gawk '{print $7}'Expected output:  succeeded!The problem I'm facing is if I want to check a range of ports, while some tests may in fact return succeeded!, there's no guarantee that all ports returned as such. This is an issue for me because I compare cmd:output on a 1:1 basis, based off a configuration file that lists the commands and the expected output.Instead of listing something like:nc -zvw1 serv1.host.com 443 | gawk '{print $7}' nc -zvw1 serv1.host.com 444 | gawk '{print $7}' nc -zvw1 serv1.host.com 445 | gawk '{print $7}'Expected result: succeeded! Expected result: succeeded! Expected result: succeeded!I'd like to be able to force nc to fail if any ports in the range fail; so in this case my configuration could be condenced to:nc -zvw1 serv1.host.com 443-445 | gawk '{print $7}'Expected result: succeeded!This is a long-winded description of a straight-forward question, unfortunately. Apologies.
Can you force netcat to succeed only if all ports in port range succeed?
linux;netcat
This could probably work:seq 443 445 \| xargs -n1 sh -c 'nc -zvw1 serv1.host.com $0 >/dev/null 2>&1 || (echo error! && exit 255)' 2>/dev/null \&& echo succeeded!It basicly emulates port ranges with seq and xargs and utilizes nc's return value to either echo error! and setting exit 255 to abort xargs or echo succeeded! when it successfully connected to each given port number.