Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Q: Why does the second iteration exits after 10.175.192.16? Can someone explain that? Or I just found a "while/netcat" bug?

a.txt's content:

$ 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
$

Exit codes:

$ 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.

share|improve this question
add comment

2 Answers 2

up vote 3 down vote accepted

I think I have an answer: when nc tries an IP address that has a server listening on port 22 (which is typically SSH server), it reads the rest of the input and passes it to the server on port 22. The SSH server I have running on my home machines just eats the input.

The nc I have (Slackware 13.1 system) has a "-z" option, for "zero I/O". Try

nc -z -w 3 "$oneline" >& /dev/null

as the command in your while-loop. I think that other nc implementations exist. There's a GNU netcat for example, but it takes a "-z" option as well.

share|improve this answer
    
omg. it works. you're the god. –  LanceBaynes Jan 26 '12 at 4:00
add comment

May be it breaks that you have any special symbol at the end of 10.175.192.16? You could try to analyze file without 'read' command:

HOSTS=`cat a.txt`
for host in $HOSTS; do 
    code=`nc -w 3 -z "$host" 22 >& /dev/null; echo $?`
    if [ "$code" == "0" ]; then 
        echo $host >> reachable.txt
    else
        echo $host >> unreachable.txt
    fi
done
share|improve this answer
    
$HOSTS is HOSTS –  LanceBaynes Jan 26 '12 at 3:55
    
and "fi" is missing –  LanceBaynes Jan 26 '12 at 3:55
    
thank you, this one works too. –  LanceBaynes Jan 26 '12 at 4:00
    
Thanks for correction –  Sergei Lomakov Jan 26 '12 at 7:34
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.