Q: Why does the second iteration exits after 10.175.192.16? Can someone explain that? Or I just found a "while/netcat" bug?
$ cat a.txt 10.175.192.14 10.175.192.16 10.175.192.17 $
First iteration, this is ok, just outputs the file content:
$ while read oneline; do echo $oneline; done < a.txt 10.175.192.14 10.175.192.16 10.175.192.17 $
Second iteration, this is bad, it exists when coming to 10.175.192.16:
$ while read oneline; do nc -w 3 "$oneline" 22 >& /dev/null; echo $oneline; done < a.txt 10.175.192.14 10.175.192.16 $
$ nc -w 3 "10.175.192.14" 22 >& /dev/null; echo $? 1 $ nc -w 3 "10.175.192.16" 22 >& /dev/null; echo $? 0
So there is no SSH server behind 10.175.192.14. But there is one behind 10.175.192.16. But the "while" iteration shouldn't exit before going through all the lines, no? Why does it miss 10.175.192.17? What am I missing here?
My main purpose is that I'm having a file (a.txt) that contains IP addresses. I need to sort these IP addresses in two files:
A) The ones that are reachable (ssh is prompting, so server is available)
B) The ones that are not reachable (timeout after 3 seconds, because there aren't anything listening on port 22)
"reachable" means that are there anything listening on port 22 behind the IP.