#! /bin/bash
#
# Test ability to connect to remote direct socket (JetDirect) server.
#
# Exits:   0 doesn't seem to have a problem
#          1 remote host $1 not set
#          2 the remote host $1 is unreachable
#          3 no connection possible to port 9100 or port $2 on host $1
#          4 port 9100 or port $2 on host $1 does not accept data
#
# Johannes Meixner <jsmeix@suse.de> 2002
# Jiri Srain <jsrain@suse.cz>, 2002
# $Id: test_remote_socket 4894 2002-08-19 15:32:16Z jsrain $

#set -x
MY_NAME=${0##*/}
HOST="$1"
[ -z "$HOST" ] && { echo -en "\nUsage:\n$MY_NAME HOST [PORT [TIMEOUT]]\n" 1>&2 ; exit 1 ; }
PORT="$2"
[ -z "$PORT" ] && PORT=9100
TIMEOUT="$3"
[ -z "$TIMEOUT" ] && TIMEOUT=10

# test whether the remote host is accessible
ping -c 1 -w $TIMEOUT $HOST || { echo -en "\nHost $HOST unreachable\n" ; exit 2 ; }

# test whether connection is possible to $PORT on the remote host
netcat -w $TIMEOUT -z $HOST $PORT || { echo -en "\nNo connection possible to port $PORT\n" ; exit 3 ; }

# test whether $PORT on the remote host accepts data
echo -en "\r" | netcat -w $TIMEOUT $HOST $PORT 2>&1
RESULT=$?
[ "$RESULT" = "0" ] \
  && { echo -en "\nPort $PORT on host $HOST accepts data\n" ; exit 0 ; } \
  || echo -en "\nPort $PORT on host $HOST does not accept data\n"
exit 4

