#24 Introduce ippeve-tool
Opened a year ago by zdohnal. Modified 8 months ago
rpms/ zdohnal/cups ippeve_tool  into  rawhide

file added
+358
@@ -0,0 +1,358 @@ 

+ #!/usr/bin/bash

+ 

+ #

+ # Helper script which can check whether the destination(s) supports IPP Everywhere

+ # and can permanently install the destination(s).

+ #

+ 

+ 

+ #

+ # Global variables

+ #

+ 

+ DEVICE_URI=""

+ HELP=0

+ HOST=""

+ INSTALL=0

+ PATTERN=""

+ QUEUE=""

+ QUEUE_LIST=""

+ SERVER=0

+ UNINSTALL=0

+ 

+ 

+ #

+ # Check if the IPP port on the device is opened and accepting data...

+ #

+ 

+ check_connectivity()

+ {

+   nc -z "$HOST" 631 1> /dev/null 

+   if test $? -ne 0

+   then

+     echo "ERROR: Port 631 on \"$HOST\" is not opened."

+     exit 1

+   fi

+ }

+ 

+ 

+ #

+ # Check if we have CLI tools we need

+ #

+ 

+ check_dependencies()

+ {

+   command -v grep 1> /dev/null || fail "ERROR: grep is not installed.\n"

+ 

+   command -v ipptool 1> /dev/null || fail "ERROR: ipptool is not installed.\n"

+ 

+   if test $INSTALL -eq 1 -o $UNINSTALL -eq 1

+   then

+     command -v lpadmin 1> /dev/null || fail "ERROR: lpadmin is not installed."

+   fi

+ 

+   command -v lpstat 1> /dev/null || fail "ERROR: lpstat is not installed.\n"

+ 

+   command -v nc 1> /dev/null || fail "ERROR: nc is not installed.\n"

+ 

+   command -v timeout 1> /dev/null || fail "ERROR: timeout is not installed.\n"

+ }

+ 

+ 

+ #

+ # Create device uri needed for IPP request...

+ #

+ 

+ create_device_uri()

+ {

+   local check_queue="$QUEUE"

+   local dest="$1"

+ 

+   if test $SERVER -eq 1

+   then

+     #

+     # If the host is server, either get queue name:

+     # 1) from parameter --queue/-q

+     # 2) the first queue name from server, which matches the pattern provided by --pattern/-p

+     # 3) the first queue name from server

+     #

+     # This is used only when checking the IPP Everywhere support on server.

+     #

+ 

+     if test "$QUEUE" = ""

+     then

+       check_queue=$(echo ${QUEUE_LIST} | tr ' ' '\n' | grep ${PATTERN:-""} | head -n 1)

+     fi

+ 

+     DEVICE_URI="ipp://$dest/printers/$check_queue"

+   else

+     #

+     # Printer...

+     #

+ 

+     DEVICE_URI="ipp://$dest/ipp/print"

+   fi

+ }

+ 

+ 

+ #

+ # Report error and exit with 1...

+ #

+ 

+ fail()

+ {

+   echo -e "$1"

+   exit 1

+ }

+ 

+ 

+ #

+ # Get remote printers from server...

+ #

+ 

+ get_remote_printers()

+ {

+   QUEUE_LIST=$(timeout 5 lpstat -h $1:631 -e)

+   if test $? -ne 0 -o "$QUEUE_LIST" = ""

+   then

+     SERVER=0

+   else

+     SERVER=1

+   fi

+ }

+ 

+ 

+ #

+ # Print help message...

+ #

+ 

+ help()

+ {

+   echo "Help:"

+   echo "ippeve-tool help                                         Show this help"

+   echo "ippeve-tool check host                                   Check whether the host has IPP Everywhere enabled and working"

+   echo "ippeve-tool check host --queue|-q queue_name             Check whether the remote CUPS queue supports IPP Everywhere"

+   echo "ippeve-tool install host                                 Install all remote CUPS queues as IPP Everywhere printers"

+   echo "ippeve-tool install host --queue|-q queue_name           Install the remote CUPS queue or local printer as IPP Everywhere printer"

+   echo "ippeve-tool install host --pattern|-p pattern            Install remote CUPS queues matching the pattern"

+   echo "ippeve-tool uninstall                                    Uninstall all queues created by ippeve-tool"

+ 

+   exit 0

+ }

+ 

+ 

+ #

+ # Install the printers...

+ #

+ 

+ install_printers()

+ {

+   for printer in $@

+   do

+     #

+     # Handle different URIs...

+     #

+ 

+     if test $SERVER -eq 0

+     then

+       device_uri="ipp://$HOST/ipp/print"

+     else

+       device_uri="ipp://$HOST/printers/$printer"

+     fi

+ 

+     lpadmin -p IPPEVET_$printer -v ${device_uri} -m everywhere -E

+     if test $? -ne 0

+     then

+       echo -e "ERROR: Cannot add a new printer $QUEUE pointed to \"$HOST\".\n"

+       uninstall_printers

+       exit 1

+     fi

+     sleep 1.5

+   done

+ }

+ 

+ 

+ #

+ # Process arguments the script got...

+ #

+ 

+ process_arguments()

+ {

+   if test $# -lt 1 -o $# -gt 4

+   then

+     echo -e "ERROR: Insufficient number of parameters.\n"

+     help

+     exit 1

+   fi

+ 

+   #

+   # Take care of command mode which is the first argument...

+   #

+ 

+   case "$1" in

+     help)

+         HELP=1

+         return 0

+         ;;

+     check)

+         INSTALL=0

+         ;;

+     install)

+         INSTALL=1

+         ;;

+     uninstall)

+         UNINSTALL=1

+         return 0

+         ;;

+     *)

+         echo -e "ERROR: Invalid parameter - $1.\n"

+         help

+         exit 1

+   esac

+ 

+   #

+   # Pass the first argument, because we took care of it...

+   #

+ 

+   shift

+ 

+   #

+   # Get hostname/IP of destination...

+   #

+ 

+   if test "$1" = ""

+   then

+     echo -e "ERROR: No host defined as parameter.\n"

+     help

+     exit 1

+   else

+     HOST="$1"

+ 

+     shift

+   fi

+ 

+   #

+   # Check if we have a queue name or pattern defined...

+   #

+ 

+   case "x$1" in

+     x--queue|x-q)

+         #

+         # If we don't install a queue and the host is not server, queue name is not needed.

+         #

+ 

+         if test $SERVER -eq 0 -a $INSTALL -eq 0

+         then

+           echo -e "INFO: There is no need for queue name when checking local printer - skiping the parameter.\n"

+         elif test "$2" = ""

+         then

+           echo -e "ERROR: Parameter --queue|-q is supplied, but there is no queue name.\n"

+           help

+           exit 1

+         else

+           QUEUE="$2"

+         fi

+         ;;

+     x--pattern|x-p)

+         #

+         # We don't apply pattern for local printers...

+         #

+ 

+         if test $SERVER -eq 0

+         then

+           echo -e "ERROR: Pattern is not available for local printers.\n"

+           exit 1

+         fi

+ 

+         if test "$2" = ""

+         then

+           echo -e "ERROR: Parameter --pattern|-p is supplied, but there is no pattern.\n"

+           help

+           exit 1

+         else

+           PATTERN="$2"

+         fi

+         ;;

+     x)

+         #

+         # If we install local printer, we need a queue name for it...

+         #

+ 

+         if test $SERVER -eq 0 -a $INSTALL -eq 1

+         then

+           echo -e "ERROR: Queue name is required for local printer.\n"

+           help

+           exit 1

+         fi

+         ;;

+     *)

+         echo -e "ERROR: Incorrect parameter - $1.\n"

+         help

+         exit 1

+         ;;

+   esac

+ }

+ 

+ 

+ #

+ # Uninstall ippeve-tool printers...

+ #

+ 

+ uninstall_printers()

+ {

+   for printer in $(lpstat -e | grep "IPPEVET")

+   do

+     lpadmin -x "$printer"

+   done

+ }

+ 

+ 

+ process_arguments $@

+ 

+ test $HELP -eq 1 && help

+ 

+ check_dependencies

+ 

+ test $UNINSTALL -eq 1 && uninstall_printers

+ 

+ #

+ # Find out whether the host is CUPS server

+ #

+ 

+ get_remote_printers "$HOST"

+ 

+ check_connectivity "$HOST"

+ 

+ create_device_uri "$HOST"

+ 

+ #

+ # Check if the destination supports IPP Everywhere...

+ #

+ 

+ ipptool -tv ${DEVICE_URI} get-printer-attributes.test || fail "ERROR $?: Destination doesn't reply with valid IPP 2.0 response.\n"

+ 

+ #

+ # Get out if we run in check mode...

+ #

+ 

+ test $INSTALL -eq 0 && exit 0

+ 

+ #

+ # Get queue names which are going to be installed...

+ #

+ 

+ WANTED_PRINTERS=()

+ 

+ if test "$QUEUE" != ""

+ then

+   WANTED_PRINTERS+=($QUEUE)

+ else

+   for remote_printer in $(echo ${QUEUE_LIST} | tr ' ' '\n' | grep ${PATTERN:-""})

+   do

+     WANTED_PRINTERS+=($remote_printer)

+   done

+ fi

+ 

+ install_printers ${WANTED_PRINTERS[@]}

+ 

+ exit 0

no initial comment

1 new commit added

  • Remove cleanup_printers, no longer needed
11 months ago

Some comments on the script:

It would be nice if the help command and --help was processed before the dependencies are checked, so one can see what is the script supposed to do without installing the dependencies.

Should be the "timeout" command be checked as a dependency? It's in coreutils, but maybe the script could find its way on some other system that doesn't have it?

When the lpadmin command is missing, there is no error message. Should it be added to check_dependencies()?

In install_printers() the "printers" variable is unnecessary. The loop can iterate over the function's arguments directly using "for printer".

The "grep IPPEVET" command could be a bit more specific "grep ^IPPEVET" .

There are many instances of missing double quotes. Please run shellcheck on the script.

There are two instances of "$?" used when it no longer contains the exit code of the command, but a following that followed it. This is reported by shellcheck.

1 new commit added

  • WIP: save work
8 months ago
Metadata