Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
    echo -n "$X " >> $LOG_FILE
    echo "ONLINE" >> $LOG_FILE

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

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

Your Answer


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.