#! /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
#         10 ping not executable (no iputils RPM installed?)
#         11 netcat not executable (no netcat RPM installed?)
# The program head is in the coreutils RPM and therefore assumed to exist.
#
# Johannes Meixner <jsmeix@suse.de> 2002, 2007, 2008
# Jiri Srain <jsrain@suse.cz>, 2002
# $Id: test_remote_socket 43943 2008-01-28 13:38:58Z mzugec $

#set -x

# Make sure to have a clean environment:
export PATH="/sbin:/usr/sbin:/usr/bin:/bin"
export LC_ALL="POSIX"
export LANG="POSIX"
umask 022

# Use the binaries of the operating system (no aliases, functions, /usr/local/):
export PING=$( type -ap ping | head -n 1 )
[ -z "$PING" ] && { echo -en "\nping not executable\n" 1>&2 ; exit 10 ; }
export NETCAT=$( type -ap netcat | head -n 1 )
[ -z "$NETCAT" ] && { echo -en "\nnetcat not executable\n" 1>&2 ; exit 11 ; }

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
[ "$?" = "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

