Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I create a bash script that check the connection to an host with telnet. That's the core code:

telnet $X 2404 < $TEST &>/dev/null
if [ $? -ne 0 ] ; then
    echo -n "$X " >> $LOG_FILE
    echo "OFFLINE <---" >> $LOG_FILE
else
    echo -n "$X " >> $LOG_FILE
    echo "ONLINE" >> $LOG_FILE
fi

I want to have three separated message:

  • ONLINE for telnet estabilished
  • OFFLINE for connection refused
  • TIMEOUT for connection timeout

The problem is that both "connection refused" and "connection timeout" return an exit status 1.

Any idea? Thanks to all.

share|improve this question
add comment

1 Answer

I'd be tempted to time it- if it comes back quickly assume refused, otherwise timed out. You may get some edge cases where it is refused just before time out period, but at least you should get reasonable results.

Using bash's time command will give you what you need. You'll need to figure out what the thresholds should be for a refused and a timeout, maybe through trial and error.

share|improve this answer
    
It's a good idea but how implements in bash? –  John Oct 6 '11 at 7:00
    
Is that helpful @John? the time command is really straightforward. –  Rory Alsop Oct 6 '11 at 9: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.