1

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.

1
  • The SSH command might be a better alternative: ssh -o StrictHostKeyChecking=no -o PasswordAuthentication=no -o ConnectTimeout=1 -p {port} {hostname} exit Pipe the output into grep to check for various results
    – roostergx
    Jun 5, 2018 at 16:25

1 Answer 1

1

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.

2
  • It's a good idea but how implements in bash?
    – John
    Oct 6, 2011 at 7:00
  • Is that helpful @John? the time command is really straightforward.
    – Rory Alsop
    Oct 6, 2011 at 9:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .