File test_remote_ipp.without_ping of Package yast2-printer

#! /bin/bash
#
# Test ability to connect to remote IPP server.
#
# Exits:   0 queue on host accepts print jobs
#          1 host $1 or queue $2 not set
#          2 no connection possible to port 631 on host
#          4 connection possible to port 631 on host but queue does not accept a print job
#            (queue may not exist or queueing disabled?)
#         15 connection possible to port 631 on host but lp not executable (no cups-client RPM installed?)
#         16 queue on host does not accept print jobs but lpstat not executable (no cups-client RPM installed?)
# The program head is in the coreutils RPM and therefore assumed to exist.
# The program rpm is assumed to exist and even if not it triggers only a non-matching warning message.
#
# Johannes Meixner <jsmeix@suse.de>, 2000, 2002, 2007, 2008, 2009, 2010, 2011, 2014
# Jan Holesovsky <kendy@suse.cz>, 2000
# Jiri Srain <jsrain@suse.cz>, 2002
# $Id: test_remote_ipp 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
# Disable bash file name globbing:
set -f

MY_NAME=${0##*/}
HOST="$1"
QUEUE="$2"
[ -z "$HOST" -o -z "$QUEUE" ] && { echo -en "\nUsage:\n$MY_NAME HOST QUEUE\n" 1>&2 ; exit 1 ; }

# Basic test whether connection is possible to port 631 on host:
# If the test fails, show an error message and exit with non-zero exit code.
# The outermost subshell avoids job control messages like "[1] job_pid" and "[1]+ Done..." or "[1]+ Terminated...".
# The hardcoded 2 seconds timeout is waited in any case so that the test needs always basically that timeout time.
# In POSIX shells wait returns the exit code of the job even if it had already terminated when wait was started,
# see http://pubs.opengroup.org/onlinepubs/9699919799/utilities/wait.html that reads:
# "This volume of POSIX.1-2008 requires the implementation to keep the status
#  of terminated jobs available until the status is requested".
if ! ( ( echo -n "" >/dev/tcp/$HOST/631 ) & ECHO_PID=$! ; sleep 2s ; kill $ECHO_PID &>/dev/null ; wait $ECHO_PID )
then # The basic test failed:
     echo -en "\nNo connection possible to IPP port 631 on host '$HOST'."
     echo -en "\n(Network issue or wrong host or no server running or firewall active there?)\n\n"
     exit 2
fi

# The basic test succeeded:
echo -en "\nConnection possible to IPP port 631 on host '$HOST'\n"

SERVER="$HOST"
# When on a CUPS >= 1.6 client system a CUPS <= 1.5 server should be used
# there must be a "/version=1.1" suffix to enforce using IPP version 1.1
# because CUPS <= 1.5 servers reject higher IPP version requests with "Bad Request":
if zypper versioncmp $( cups-config --version ) 1.6 | grep -q 'newer'
then # On a CUPS >= 1.6 system "lpstat -h cups_1.5_server -p" results on stderr:
     #   lpstat: Error - add '/version=1.1' to server name.
     if lpstat -h $SERVER -p 2>&1 1>/dev/null | grep -q "add '/version=1.1' to server name"
     then SERVER="${SERVER}/version=1.1"
     fi
fi

# Use the binaries of the operating system (no aliases, functions, /usr/local/):
export LP=$( type -ap lp | head -n 1 )
export LPSTAT=$( type -ap lpstat | head -n 1 )

# Test whether lp is executable:
if test -z "$LP"
then echo -en "\nFailed to test if queue '$QUEUE' on host '$HOST' accepts print jobs" 1>&2
     echo -en "\nbecause 'lp' not executable (no 'cups-client' RPM installed?)\n" 1>&2
     exit 15
fi
# Warn if lp is not from the cups-client RPM e.g. from self-compiled CUPS (o.k.) or e.g. from LPRng (bad):
if ! rpm -qf "$LP" | grep -q '^cups-client'
then echo -en "\nIt may fail to test if queue '$QUEUE' on host '$HOST' accepts print jobs"
     echo -en "\nbecause '$LP' is not the expected one from the 'cups-client' RPM\n"
fi
# Test whether the queue on the server accepts print jobs:
echo -en "\nTesting queue '$QUEUE' on host '$HOST':\n"
if echo -en "\r" | $LP -d $QUEUE -h $SERVER 2>&1
then echo -en "\nQueue '$QUEUE' on host '$HOST' accepts print jobs\n"
     exit 0
fi
echo -en "\nQueue '$QUEUE' on host '$HOST' does not accept print jobs (queue may not exist or queueing disabled?)\n"
# If queue on host does not accept jobs, print queue status:
# Test whether lpstat is executable:
if test -z "$LPSTAT"
then echo -en "\nFailed to determine the status of queue '$QUEUE' on host '$HOST'" 1>&2
     echo -en "\nbecause 'lpstat' not executable (no 'cups-client' RPM installed?)\n" 1>&2
     exit 16
fi
# Warn if lpstat is not from the cups-client RPM e.g. from self-compiled CUPS (o.k.) or e.g. from LPRng (bad):
if ! rpm -qf "$LPSTAT" | grep -q '^cups-client'
then echo -en "\nIt may fail to determine the status of queue '$QUEUE' on host '$HOST'"
     echo -en "\nbecause '$LPSTAT' is not the expected one from the 'cups-client' RPM\n"
fi
echo -en "\nStatus of the queue '$QUEUE' (possibly empty or not available):\n\n"
$LPSTAT -h $SERVER -a $QUEUE -p $QUEUE
exit 4

openSUSE Build Service is sponsored by