home *** CD-ROM | disk | FTP | other *** search
/ IRIX 6.4 Applications 1997 August / SGI IRIX 6.4 Applications 1997 August.iso / dist / print.idb / usr / sbin / mkbsdnetpr.z / mkbsdnetpr
Encoding:
Text File  |  1997-07-30  |  7.3 KB  |  327 lines

  1. #!/bin/sh 
  2. #Tag 0x00000700
  3. #**************************************************************************
  4. #*
  5. #*           Copyright (c) 1995 Silicon Graphics, Inc.
  6. #*            All Rights Reserved
  7. #*
  8. #*       THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF SGI
  9. #*
  10. #* The copyright notice above does not evidence any actual of intended
  11. #* publication of such source code, and is an unpublished work by Silicon
  12. #* Graphics, Inc. This material contains CONFIDENTIAL INFORMATION that is
  13. #* the property of Silicon Graphics, Inc. Any use, duplication or
  14. #* disclosure not specifically authorized by Silicon Graphics is strictly
  15. #* prohibited.
  16. #*
  17. #* RESTRICTED RIGHTS LEGEND:
  18. #*
  19. #* Use, duplication or disclosure by the Government is subject to
  20. #* restrictions as set forth in subdivision (c)(1)(ii) of the Rights in
  21. #* Technical Data and Computer Software clause at DFARS 52.227-7013,
  22. #* and/or in similar or successor clauses in the FAR, DOD or NASA FAR
  23. #* Supplement. Unpublished - rights reserved under the Copyright Laws of
  24. #* the United States. Contractor is SILICON GRAPHICS, INC., 2011 N.
  25. #* Shoreline Blvd., Mountain View, CA 94039-7311
  26. #**************************************************************************
  27. #*
  28. #* File: mkbsdnetpr
  29. #*
  30. #* $Revision: 1.2 $
  31. #*
  32. #* Description: Shell script for setting up an lp printer to a remote 
  33. #*              server that uses BSD protocols.  The remote system should be
  34. #*              a true spooler that can spool files, print multiple copies,
  35. #*              support multiple connections, etc. (most printer network 
  36. #*              boards do not do these things -- use mkjtpr for printers
  37. #*              connected directly to a network).
  38. #*
  39. #*    Usage: mkbsdnetpr  [<local printer name>] 
  40. #*                         [<name or IP address of remote bsd server>] 
  41. #*                         [<remote printer name>]
  42. #*
  43. #**************************************************************************
  44.  
  45.  
  46. # Well known directories and programs
  47.  
  48. LPUTIL_DIR=/usr/lib
  49. LPUTIL=$LPUTIL_DIR/lputil
  50. AWK_DIR=/usr/bin
  51. AWK=$AWK_DIR/nawk
  52. PING_CMD="/usr/etc/ping -q -c3"
  53. GREP_CMD="/bin/egrep"
  54. OUT_FILT="/usr/bin/pg -s -p more... -e"
  55. SPOOL_DIR=/var/spool/lp
  56. INTERFACE_DIR=$SPOOL_DIR/interface
  57. LPSTAT=/usr/bin/lpstat
  58. CHKCONFIG=/sbin/chkconfig 
  59.  
  60. # Global variables
  61.  
  62. PNAME=""
  63. REMOTE_PNAME=""
  64. REMOTE_HOST=""
  65.  
  66. #
  67. # Prints the program usage string
  68. #
  69. PrintUsage()
  70. {
  71.     echo "Usage: mkbsdnetpr [<local printer name>] "
  72.     echo "\t[<name or IP address of network adaptor>] [<name of remote printer>]"
  73. }
  74.  
  75. #
  76. # Checks that we are the super-user and verifies
  77. # that the System V spooling system is present
  78. #
  79. CheckPermSpooler()
  80. {
  81.     cid=`/usr/bin/id | $AWK '{print substr($1,index($1,"("))}'`;
  82.     if [ "$cid" != "(root)" ]; then
  83.         echo "You must be logged in as root to use this program."
  84.         exit 1;
  85.     fi
  86.     if [ ! -r $SPOOL_DIR ]; then
  87.         echo "The System V spooling system is not installed on this system."
  88.         exit 1
  89.     fi
  90. }
  91.  
  92. #
  93. # Test for a valid printer name (i.e. 14 chars max, A-Za-z0-9_
  94. # only and no spaces)
  95. #
  96. VerifyPname()
  97. {
  98.     if [ $# -gt 1 ]; then
  99.     return 1
  100.     fi
  101.     echo $1 | $GREP_CMD -s '^[A-Za-z0-9_]*$'
  102.     if [ $? -eq 1 ]; then
  103.     return 1
  104.     fi
  105.     if [ `echo $1 | $AWK '{ print length($1) }'` -gt 14 ]; then
  106.     return 1
  107.     else
  108.     return 0
  109.     fi
  110. }
  111.  
  112. #
  113. # Test for a properly constructed remote host name
  114. #
  115. VerifyHostname()
  116. {
  117.     if [ $# -gt 1 ]; then
  118.         return 1
  119.     fi
  120.     return 0
  121. }
  122.  
  123. # Check if we can ping the remote system.  
  124.  
  125. checkconnet()
  126. {
  127.  
  128.   if $CHKCONFIG network; then
  129.      :
  130.   else
  131.      echo "Networking has not been enabled on this system." 1>&2
  132.      return 1
  133.   fi
  134.  
  135.   host=$1
  136.  
  137.   $PING_CMD $host 2> /dev/null 1> /dev/null
  138.  
  139.   if [ $? != 0 ]; then
  140.      echo "Printer $host name not known.  Check /etc/hosts or yp server entries." 1>&2
  141.      return 1
  142.   fi
  143.  
  144.   return 0
  145.  
  146. }
  147.  
  148.  
  149. # Check for a duplicate printer name. Returns 0 if dup,
  150. # 1 if name is unique
  151. #
  152. CheckDupName()
  153. {
  154.     /bin/ls $SPOOL_DIR/member/$1 2> /dev/null 1> /dev/null
  155.     return $?
  156. }
  157.  
  158.  
  159. #
  160. # Query user for a name for the remote printer
  161. #
  162. QueryRemoteName()
  163. {
  164.     while [ "$REMOTE_PNAME" = "" ]; do
  165.         echo "Enter remote printer name on BSD print server: \c"
  166.         read REMOTE_PNAME
  167.         if [ "$PNAME" = "" ]; then
  168.             continue
  169.         fi
  170.     done
  171. }
  172.  
  173. #
  174. # Query user for a name for the local printer
  175. #
  176. QueryLocalName()
  177. {
  178.     while [ "$PNAME" = "" ]; do
  179.         echo ""
  180.         echo "Enter new printer name (14 chars max.): \c"
  181.         read PNAME
  182.         VerifyPname $PNAME
  183.         if [ $? -eq 1 ]; then
  184.             echo "Invalid response: Name must be 14 chars (A-Za-z0-9_ only)"
  185.             PNAME=""
  186.         fi
  187.         if [ "$PNAME" = "" ]; then
  188.             continue
  189.         fi
  190.         CheckDupName $PNAME
  191.         if [ $? -eq 0 ]; then
  192.             echo " "
  193.             echo "WARNING: Printer $PNAME already exists."
  194.             echo '\nChoose a new name (y/n)[y]? \c'
  195.             read yn
  196.             if [ \( "$yn" != "n" \) -a \( "$yn" != "N" \) ]; then
  197.                 PNAME=""
  198.             fi
  199.         fi
  200.     done
  201. }
  202.  
  203.  
  204. #########################################################################
  205. #
  206. # Main program
  207. #
  208.  
  209. #
  210. # Ensure that we are root and that the main System V
  211. # spooling system directory is present
  212. #
  213. CheckPermSpooler
  214.  
  215. #
  216. # Get command line arguments, if any.
  217. #
  218. case $# in
  219.     0) ;;
  220.     1) PNAME=$1
  221.        ;;
  222.     2) PNAME=$1
  223.        REMOTE_HOST=$2
  224.        ;;
  225.     3) PNAME=$1
  226.        REMOTE_HOST=$2
  227.        REMOTE_PNAME=$3
  228.        ;;
  229.     *) PrintUsage
  230.        exit 1
  231.        ;;
  232. esac
  233.  
  234. #
  235. # Make sure we have a remote printer host
  236. #
  237. if [ "$REMOTE_HOST" = "" ]; then
  238.     while [ "$REMOTE_HOST" = "" ]; do
  239.         echo "Enter remote system name (network name or IP address): \c"
  240.         read REMOTE_HOST
  241.         VerifyHostname $REMOTE_HOST
  242.         if [ $? -eq 1 ]; then
  243.             echo "Invalid response: Improperly constructed hostname"
  244.             REMOTE_HOST=""
  245.         fi
  246.     done
  247.     echo " "
  248. fi
  249.  
  250. echo "Testing connection to printer: $REMOTE_HOST...\n"
  251.  
  252. checkconnet $REMOTE_HOST
  253. if [ $? -eq 1 ]; then
  254.    echo "\nContinue anyway using $REMOTE_HOST(y/n)[y]? \c"
  255.    read yn
  256.    if [ \( "$yn" != "y" \) -a \( "$yn" != "Y" \) ]; then
  257.       exit 1
  258.    fi
  259. fi
  260.  
  261. #
  262. # Query user for a name for the remote printer
  263. #
  264. if [ "$REMOTE_PNAME" = "" ]; then
  265.     QueryRemoteName
  266. fi
  267.  
  268. #
  269. # Query user for a name for this printer
  270. #
  271. if [ "$PNAME" = "" ]; then
  272.     QueryLocalName
  273.     echo " "
  274. else
  275.     echo "Requested local printer name: $PNAME\n"
  276.     VerifyPname $PNAME
  277.     if [ $? -eq 1 ]; then
  278.         echo "Error: Local printer name must be 14 chars max (A-Za-z0-9_ only)"
  279.         PNAME=""
  280.         QueryLocalName
  281.         echo " "
  282.     fi
  283.     CheckDupName $PNAME
  284.     if [ $? -eq 0 ]; then
  285.         echo " "
  286.         echo "WARNING: Printer $PNAME already exists."
  287.         echo '\nChoose a new name (y/n)[y]? \c'
  288.         read yn
  289.         if [ \( "$yn" != "n" \) -a \( "$yn" != "N" \) ]; then
  290.             PNAME=""
  291.             QueryLocalName
  292.             echo " "
  293.         fi
  294.     fi
  295. fi
  296.  
  297. #
  298. # Inform the user of what comes next
  299. #
  300. echo " "
  301. echo "Installing printer $PNAME..."
  302. echo " "
  303.  
  304. #
  305. # Install the printer.
  306. #
  307. $LPUTIL addnet  $REMOTE_HOST $REMOTE_PNAME $PNAME bsd
  308. if [ $? -ne 0 ]; then
  309.     echo "ERROR: Installation of new printer failed."
  310.     echo "       $LPUTIL command failed."
  311.     exit 1
  312. fi
  313.  
  314. #
  315. # Inform the user of what has happened
  316. #
  317. echo " "
  318. echo "Network Printer $PNAME has been installed"
  319. echo " "
  320. echo "Here is your printing environment:"
  321. echo " "
  322. $LPSTAT -t | $OUT_FILT
  323. #
  324. # Return a successful exit code
  325. #
  326. exit 0
  327.