home *** CD-ROM | disk | FTP | other *** search
- #!/bin/sh
- #Tag 0x00000700
- #**************************************************************************
- #*
- #* Copyright (c) 1995 Silicon Graphics, Inc.
- #* All Rights Reserved
- #*
- #* THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF SGI
- #*
- #* The copyright notice above does not evidence any actual of intended
- #* publication of such source code, and is an unpublished work by Silicon
- #* Graphics, Inc. This material contains CONFIDENTIAL INFORMATION that is
- #* the property of Silicon Graphics, Inc. Any use, duplication or
- #* disclosure not specifically authorized by Silicon Graphics is strictly
- #* prohibited.
- #*
- #* RESTRICTED RIGHTS LEGEND:
- #*
- #* Use, duplication or disclosure by the Government is subject to
- #* restrictions as set forth in subdivision (c)(1)(ii) of the Rights in
- #* Technical Data and Computer Software clause at DFARS 52.227-7013,
- #* and/or in similar or successor clauses in the FAR, DOD or NASA FAR
- #* Supplement. Unpublished - rights reserved under the Copyright Laws of
- #* the United States. Contractor is SILICON GRAPHICS, INC., 2011 N.
- #* Shoreline Blvd., Mountain View, CA 94039-7311
- #**************************************************************************
- #*
- #* File: mkbsdnetpr
- #*
- #* $Revision: 1.2 $
- #*
- #* Description: Shell script for setting up an lp printer to a remote
- #* server that uses BSD protocols. The remote system should be
- #* a true spooler that can spool files, print multiple copies,
- #* support multiple connections, etc. (most printer network
- #* boards do not do these things -- use mkjtpr for printers
- #* connected directly to a network).
- #*
- #* Usage: mkbsdnetpr [<local printer name>]
- #* [<name or IP address of remote bsd server>]
- #* [<remote printer name>]
- #*
- #**************************************************************************
-
-
- # Well known directories and programs
-
- LPUTIL_DIR=/usr/lib
- LPUTIL=$LPUTIL_DIR/lputil
- AWK_DIR=/usr/bin
- AWK=$AWK_DIR/nawk
- PING_CMD="/usr/etc/ping -q -c3"
- GREP_CMD="/bin/egrep"
- OUT_FILT="/usr/bin/pg -s -p more... -e"
- SPOOL_DIR=/var/spool/lp
- INTERFACE_DIR=$SPOOL_DIR/interface
- LPSTAT=/usr/bin/lpstat
- CHKCONFIG=/sbin/chkconfig
-
- # Global variables
-
- PNAME=""
- REMOTE_PNAME=""
- REMOTE_HOST=""
-
- #
- # Prints the program usage string
- #
- PrintUsage()
- {
- echo "Usage: mkbsdnetpr [<local printer name>] "
- echo "\t[<name or IP address of network adaptor>] [<name of remote printer>]"
- }
-
- #
- # Checks that we are the super-user and verifies
- # that the System V spooling system is present
- #
- CheckPermSpooler()
- {
- cid=`/usr/bin/id | $AWK '{print substr($1,index($1,"("))}'`;
- if [ "$cid" != "(root)" ]; then
- echo "You must be logged in as root to use this program."
- exit 1;
- fi
- if [ ! -r $SPOOL_DIR ]; then
- echo "The System V spooling system is not installed on this system."
- exit 1
- fi
- }
-
- #
- # Test for a valid printer name (i.e. 14 chars max, A-Za-z0-9_
- # only and no spaces)
- #
- VerifyPname()
- {
- if [ $# -gt 1 ]; then
- return 1
- fi
- echo $1 | $GREP_CMD -s '^[A-Za-z0-9_]*$'
- if [ $? -eq 1 ]; then
- return 1
- fi
- if [ `echo $1 | $AWK '{ print length($1) }'` -gt 14 ]; then
- return 1
- else
- return 0
- fi
- }
-
- #
- # Test for a properly constructed remote host name
- #
- VerifyHostname()
- {
- if [ $# -gt 1 ]; then
- return 1
- fi
- return 0
- }
-
- # Check if we can ping the remote system.
-
- checkconnet()
- {
-
- if $CHKCONFIG network; then
- :
- else
- echo "Networking has not been enabled on this system." 1>&2
- return 1
- fi
-
- host=$1
-
- $PING_CMD $host 2> /dev/null 1> /dev/null
-
- if [ $? != 0 ]; then
- echo "Printer $host name not known. Check /etc/hosts or yp server entries." 1>&2
- return 1
- fi
-
- return 0
-
- }
-
-
- # Check for a duplicate printer name. Returns 0 if dup,
- # 1 if name is unique
- #
- CheckDupName()
- {
- /bin/ls $SPOOL_DIR/member/$1 2> /dev/null 1> /dev/null
- return $?
- }
-
-
- #
- # Query user for a name for the remote printer
- #
- QueryRemoteName()
- {
- while [ "$REMOTE_PNAME" = "" ]; do
- echo "Enter remote printer name on BSD print server: \c"
- read REMOTE_PNAME
- if [ "$PNAME" = "" ]; then
- continue
- fi
- done
- }
-
- #
- # Query user for a name for the local printer
- #
- QueryLocalName()
- {
- while [ "$PNAME" = "" ]; do
- echo ""
- echo "Enter new printer name (14 chars max.): \c"
- read PNAME
- VerifyPname $PNAME
- if [ $? -eq 1 ]; then
- echo "Invalid response: Name must be 14 chars (A-Za-z0-9_ only)"
- PNAME=""
- fi
- if [ "$PNAME" = "" ]; then
- continue
- fi
- CheckDupName $PNAME
- if [ $? -eq 0 ]; then
- echo " "
- echo "WARNING: Printer $PNAME already exists."
- echo '\nChoose a new name (y/n)[y]? \c'
- read yn
- if [ \( "$yn" != "n" \) -a \( "$yn" != "N" \) ]; then
- PNAME=""
- fi
- fi
- done
- }
-
-
- #########################################################################
- #
- # Main program
- #
-
- #
- # Ensure that we are root and that the main System V
- # spooling system directory is present
- #
- CheckPermSpooler
-
- #
- # Get command line arguments, if any.
- #
- case $# in
- 0) ;;
- 1) PNAME=$1
- ;;
- 2) PNAME=$1
- REMOTE_HOST=$2
- ;;
- 3) PNAME=$1
- REMOTE_HOST=$2
- REMOTE_PNAME=$3
- ;;
- *) PrintUsage
- exit 1
- ;;
- esac
-
- #
- # Make sure we have a remote printer host
- #
- if [ "$REMOTE_HOST" = "" ]; then
- while [ "$REMOTE_HOST" = "" ]; do
- echo "Enter remote system name (network name or IP address): \c"
- read REMOTE_HOST
- VerifyHostname $REMOTE_HOST
- if [ $? -eq 1 ]; then
- echo "Invalid response: Improperly constructed hostname"
- REMOTE_HOST=""
- fi
- done
- echo " "
- fi
-
- echo "Testing connection to printer: $REMOTE_HOST...\n"
-
- checkconnet $REMOTE_HOST
- if [ $? -eq 1 ]; then
- echo "\nContinue anyway using $REMOTE_HOST(y/n)[y]? \c"
- read yn
- if [ \( "$yn" != "y" \) -a \( "$yn" != "Y" \) ]; then
- exit 1
- fi
- fi
-
- #
- # Query user for a name for the remote printer
- #
- if [ "$REMOTE_PNAME" = "" ]; then
- QueryRemoteName
- fi
-
- #
- # Query user for a name for this printer
- #
- if [ "$PNAME" = "" ]; then
- QueryLocalName
- echo " "
- else
- echo "Requested local printer name: $PNAME\n"
- VerifyPname $PNAME
- if [ $? -eq 1 ]; then
- echo "Error: Local printer name must be 14 chars max (A-Za-z0-9_ only)"
- PNAME=""
- QueryLocalName
- echo " "
- fi
- CheckDupName $PNAME
- if [ $? -eq 0 ]; then
- echo " "
- echo "WARNING: Printer $PNAME already exists."
- echo '\nChoose a new name (y/n)[y]? \c'
- read yn
- if [ \( "$yn" != "n" \) -a \( "$yn" != "N" \) ]; then
- PNAME=""
- QueryLocalName
- echo " "
- fi
- fi
- fi
-
- #
- # Inform the user of what comes next
- #
- echo " "
- echo "Installing printer $PNAME..."
- echo " "
-
- #
- # Install the printer.
- #
- $LPUTIL addnet $REMOTE_HOST $REMOTE_PNAME $PNAME bsd
- if [ $? -ne 0 ]; then
- echo "ERROR: Installation of new printer failed."
- echo " $LPUTIL command failed."
- exit 1
- fi
-
- #
- # Inform the user of what has happened
- #
- echo " "
- echo "Network Printer $PNAME has been installed"
- echo " "
- echo "Here is your printing environment:"
- echo " "
- $LPSTAT -t | $OUT_FILT
- #
- # Return a successful exit code
- #
- exit 0
-