| 1 | #!/bin/sh |
| 2 | # \ |
| 3 | exec expect -f "$0" ${1+"$@"} |
| 4 | |
| 5 | # This script unhangs a printer which claims it is "waiting for lock". |
| 6 | # Written by Don Libes. Based on English instructions from Scott Paisley. |
| 7 | |
| 8 | # lpunlock figures out if the printer is on a server, and if so which, |
| 9 | # by looking in the local printcap file. (You can override this by |
| 10 | # supplying a server name as an additional argument.) It then rlogins |
| 11 | # to the server, recreates the device and resets the queue via lpc. |
| 12 | |
| 13 | # assumes user has root privs on remote host via /.rhosts |
| 14 | |
| 15 | # assumes printer is name of device on remote system |
| 16 | |
| 17 | proc usage {} { |
| 18 | send_user "usage: lpunlock <printer> \[<server>\]\n" |
| 19 | send_user "example: lpunlock lw-isg durer\n" |
| 20 | exit |
| 21 | } |
| 22 | |
| 23 | if {$argc==0} usage |
| 24 | set printer [lindex $argv 0] |
| 25 | |
| 26 | set client [exec hostname] |
| 27 | |
| 28 | if {$argc == 1} { |
| 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 == 2} { |
| 46 | set server [lindex $argv 1] |
| 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 |