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.

John
  • 23
  • 1
  • 3
  • 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 05 '18 at 16:25

1 Answers1

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.

Rory Alsop
  • 1,184
  • 11
  • 21