home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / languages / tcl / expect / expect-4.7 / example / lpunlock < prev    next >
Encoding:
Text File  |  1993-04-12  |  2.5 KB  |  98 lines

  1. #!../expect -f
  2.  
  3. # This script unhangs a printer which claims it is "waiting for lock".
  4. # Written by Don Libes.  Based on English instructions from Scott Paisley.
  5.  
  6. # lpunlock figures out if the printer is on a server, and if so which,
  7. # by looking in the local printcap file.  (You can override this by
  8. # supplying a server name as an additional argument.)  It then rlogins
  9. # to the server, recreates the device and resets the queue via lpc.
  10.  
  11. # assumes user has root privs on remote host via /.rhosts
  12.  
  13. # assumes printer is name of device on remote system
  14.  
  15. proc usage {} {
  16.     send_user "usage: lpunlock <printer> \[<server>\]\n"
  17.     send_user "example: lpunlock lw-isg durer\n"
  18.     exit
  19. }
  20.  
  21. trap exit SIGINT
  22. set argc [llength $argv]
  23. if {$argc < 2} usage
  24. set printer [lindex $argv 1]
  25.  
  26. set client [exec hostname]
  27.  
  28. if {$argc == 2} {
  29.     # if no arg2, look in local printcap for info
  30.     spawn ed /etc/printcap
  31.     expect "\n"            ;# discard character count
  32.     send "/$printer/\r"
  33.     for {} 1 {} {
  34.         expect -re ".*:rm=(\[^:]*):.*\r\n" {
  35.             set server $expect_out(1,string)
  36.             break
  37.         } "\r\n*\\\r\n" {    ;# look at next line of entry
  38.             send "\r"
  39.         } "\r\n*\n" {        ;# no more lines of entry - give up
  40.             set server $client
  41.             break
  42.         }
  43.     }
  44. } else {
  45.     if {$argc == 3} {
  46.         set server [lindex $argv 2]
  47.     } else usage
  48. }
  49.  
  50. set whoami [exec whoami]
  51. if {[string match $server $client] && [string match $whoami "root"]} {
  52.     spawn csh
  53.     expect "# "
  54. } else {
  55.     # login to the print server as root.
  56.     # Set timeout high because login is slow.
  57.     set timeout 60
  58.     spawn rlogin $server -l root
  59.     expect    timeout    exit \
  60.         eof exit \
  61.         "Password*" {
  62.             send_user "\ncouldn't login to $server as root\n"
  63.             exit
  64.         } "1#*"
  65.     set timeout 10
  66. }
  67.  
  68. # run lpc and 'stop printer'
  69. send lpc\r                ; expect "lpc>*"
  70. send stop $printer\r            ; expect "unknown*" exit \
  71.                         "disabled*lpc>*"
  72.  
  73. # exit lpc and cd /dev
  74. send quit\r                ; expect "#*"
  75. send cd /dev\r                ; expect "#*"
  76.  
  77. # figure out major/minor device numbers
  78. send ls -l /dev/$printer\r        ; expect timeout {
  79.     send_user "\nbad device - couldn't get major/minor numbers\n"; exit
  80.                         } "crw*#*"
  81. scan $expect_out(buffer) "ls -l %*s %*s 1 root %d, %d" major minor
  82.  
  83. # delete the lock and the printer device itself
  84. send rm /var/spool/$printer/lock /dev/$printer\r    ; expect #*
  85.  
  86. # recreate the printer device
  87. send mknod $printer c $major $minor\r    ; expect #*
  88.  
  89. # run lpc and 'start printer'
  90. send lpc\r                ; expect lpc>*
  91. send start $printer\r            ; expect started*lpc>*
  92. send quit\r                ; expect #*
  93.  
  94. # logout
  95. send exit\r                ; expect eof
  96.  
  97. send_user Printer unlocked and restarted.\n
  98.