#!/bin/ksh
#
# Someone deciced to block ICMP on the gateway of last resort.
# This means that we cannot use 'ping' anymore for various scripts.
# This is an VERY UGLY hack to work around this issue.
# Call on ssh_ping instead of ping in these scripts.
#
# This version of 'ssh_ping' makes use of the executable 'tcping'.
# This is a program that tries to open a tcp socket on a given port.
# The nice thing is that it allows you to specify timeouts, so
# that you dont have to wait ages for a connection to a host that is down.
#




MY_HOST=$(echo "$*" | awk '{ print $NF }')
MY_PORT=22
TIMEOUT_CONN=10

./tcping -t "$TIMEOUT_CONN" "$MY_HOST" "$MY_PORT" >/dev/null 2>&1

RET_CODE=$?


if [ "$RET_CODE" = "-1" ]
then
	echo "ERROR: $0 $_HOST\n\tconnection failed, undetermined error"
	return 120
fi

if [ "$RET_CODE" = "0" ]
then
	echo "INFO: $MY_HOST\n\tconnection succeeded successfully."
	return 0
fi

if [ "$RET_CODE" = "1" ]
then
	echo "ERROR: $MY_HOST\n\tconnection failed, nothing running on this port."
	return 1
fi

if [ "$RET_CODE" = "2" ]
then
	echo "ERROR: $MY_HOST\n\tconnection failed due to timeout, probably host was down or blocked by a firewall somewhere."
	return 1
fi

if [ "$RET_CODE" = "255" ]
then
	echo "ERROR: $MY_HOST\n\tconnection failed, probably a non existant host."
	return 1
fi


# end of file
