SHOW:
|
|
- or go back to the newest paste.
| 1 | #!/bin/bash | |
| 2 | # Author: Robert Schrenk | |
| 3 | # Date: 2011-10-31, 12:45 | |
| 4 | # URL: www.schrenk.cc | |
| 5 | # Details: http://rschrenk.wordpress.com/?p=744 | |
| 6 | # This Script enables Printersharing for CUPS and creates an airprint-compatible virtual printing device for any physical printer on your system. This Script should be compatible to any Linux-System using CUPS. Tested on Ubuntu 11.10. | |
| 7 | ||
| 8 | prnt=`lpstat -p` | |
| 9 | ||
| 10 | echo Checking if you are starting this Script as sudo | |
| 11 | if [ `id -u` -eq 0 ]; | |
| 12 | then | |
| 13 | echo Ok, you have sudo-privs | |
| 14 | else | |
| 15 | echo Sorry, please start this script again with sudo ./airprint.sh. Exit. | |
| 16 | exit 0 | |
| 17 | fi | |
| 18 | ||
| 19 | declare -a ips | |
| 20 | ips[0]=0 | |
| 21 | readdev=(eth0 eth1 eth2 eth3 eth4 eth5 eth6 eth7 eth8 eth9 wlan1 wlan2 wlan3 wlan4 wlan5 wlan6 wlan7 wlan8 wlan9) | |
| 22 | for dev in $readdev | |
| 23 | do | |
| 24 | ip=`ifconfig $dev | head -n 2 | tail -n 1 | cut -d: -f2 | cut -d" " -f 1` | |
| 25 | #echo Found IP $ip | |
| 26 | if [ $ip != "" ]; then | |
| 27 | ind=${ips[0]}
| |
| 28 | ind=$[$ind+1] | |
| 29 | ips[$ind]=$ip | |
| 30 | ips[0]=$ind | |
| 31 | fi | |
| 32 | done | |
| 33 | ||
| 34 | amount=${ips[0]}
| |
| 35 | if [ $amount -eq 0 ]; then | |
| 36 | echo No working network devices found. Exit. | |
| 37 | exit 0 | |
| 38 | fi | |
| 39 | if [ $amount -eq 1 ]; then | |
| 40 | CUPSIP=${ips[1]}
| |
| 41 | echo Using the only working network device $CUPSIP | |
| 42 | fi | |
| 43 | if [ $amount -gt 1 ]; then | |
| 44 | echo Select Network Interface to publish AirPrint | |
| 45 | i=1 | |
| 46 | for ip in $ips | |
| 47 | do | |
| 48 | echo $i...${ips[i]}
| |
| 49 | done | |
| 50 | read sel | |
| 51 | if [ "${ips[sel]}" = "" ]; then
| |
| 52 | echo Invalid Input. Exit. | |
| 53 | exit 0 | |
| 54 | fi | |
| 55 | if [ $sel -eq 0 ]; then | |
| 56 | echo Invalid Input. Exit. | |
| 57 | exit 0 | |
| 58 | fi | |
| 59 | CUPSIP=${ips[sel]}
| |
| 60 | fi | |
| 61 | ||
| 62 | echo Activating AirPrint on $CUPSIP | |
| 63 | echo ---------------------------------------- | |
| 64 | echo Making CUPS share printers on network | |
| 65 | cupsctl --share-printers | |
| 66 | echo ---------------------------------------- | |
| 67 | ||
| 68 | declare -a printers | |
| 69 | printers[0]=0 | |
| 70 | index=0 | |
| 71 | foundprinter=0 | |
| 72 | for word in $prnt | |
| 73 | do | |
| 74 | if [ $foundprinter -eq 1 ]; then | |
| 75 | index=${printers[0]}
| |
| 76 | index=$[$index+1] | |
| 77 | #echo Found Printer $word for Index $index | |
| 78 | printers[$index]=$word | |
| 79 | printers[0]=$index | |
| 80 | foundprinter=0 | |
| 81 | fi | |
| 82 | if [ "$word" = "printer" ]; then | |
| 83 | foundprinter=1 | |
| 84 | fi | |
| 85 | done | |
| 86 | ||
| 87 | amount=${printers[0]}
| |
| 88 | if [ $amount -eq 0 ]; then | |
| 89 | echo No working printer found. Exit. | |
| 90 | exit 0 | |
| 91 | fi | |
| 92 | if [ $amount -eq 1 ]; then | |
| 93 | CUPSPRINTER=${printers[1]}
| |
| 94 | echo Using the only working printer $CUPSPRINTER | |
| 95 | fi | |
| 96 | if [ $amount -gt 1 ]; then | |
| 97 | echo Select Printer | |
| 98 | i=1 | |
| 99 | for prnt in $printers | |
| 100 | do | |
| 101 | echo $i...${printers[i]}
| |
| 102 | done | |
| 103 | read sel | |
| 104 | if [ "${printers[sel]}" = "" ]; then
| |
| 105 | echo Invalid Input. Exit. | |
| 106 | exit 0 | |
| 107 | fi | |
| 108 | if [ $sel -eq 0 ]; then | |
| 109 | echo Invalid Input. Exit. | |
| 110 | exit 0 | |
| 111 | fi | |
| 112 | CUPSPRINTER=${ips[sel]}
| |
| 113 | fi | |
| 114 | ||
| 115 | echo Activating AirPrint for $CUPSPRINTER | |
| 116 | echo ---------------------------------------- | |
| 117 | ||
| 118 | echo Creating printer.service | |
| 119 | ||
| 120 | targ=/etc/avahi/services/printer.service | |
| 121 | unlink $targ | |
| 122 | echo \<?xml version=“1.0″ standalone=’no’?\>\<!–*-nxml-*–\> >> $targ | |
| 123 | echo \<!DOCTYPE service-group SYSTEM „avahi-service.dtd“\> >> $targ | |
| 124 | echo \<service-group\> >> $targ | |
| 125 | echo \<name\>$CUPSPRINTER\</name\> >> $targ | |
| 126 | echo \<service\> >> $targ | |
| 127 | echo \<type\>_ipp._tcp\</type\> >> $targ | |
| 128 | echo \<subtype\>_universal._sub._ipp._tcp\</subtype\> >> $targ | |
| 129 | echo \<port\>631\</port\> >> $targ | |
| 130 | echo \<txt-record\>txtver=1\</txt-record\> >> $targ | |
| 131 | echo \<txt-record\>qtotal=1\</txt-record\> >> $targ | |
| 132 | echo \<txt-record\>rp=printers/$CUPSPRINTER\</txt-record\> >> $targ | |
| 133 | echo \<txt-record\>ty=$CUPSPRINTER\</txt-record\> >> $targ | |
| 134 | echo \<txt-record\>adminurl=http://$CUPSIP:631/printers/$CUPSPRINTER\</txt-record\> >> $targ | |
| 135 | echo \<txt-record\>note=$CUPSPRINTER\</txt-record\> >> $targ | |
| 136 | echo \<txt-record\>priority=0\</txt-record\> >> $targ | |
| 137 | echo \<txt-record\>product=virtual Printer\</txt-record\> >> $targ | |
| 138 | echo \<txt-record\>printer-state=3\</txt-record\> >> $targ | |
| 139 | echo \<txt-record\>printer-type=0×801046\</txt-record\> >> $targ | |
| 140 | echo \<txt-record\>Transparent=T\</txt-record\> >> $targ | |
| 141 | echo \<txt-record\>Binary=T\</txt-record\> >> $targ | |
| 142 | echo \<txt-record\>Fax=F\</txt-record\> >> $targ | |
| 143 | echo \<txt-record\>Color=T\</txt-record\> >> $targ | |
| 144 | echo \<txt-record\>Duplex=F\</txt-record\> >> $targ | |
| 145 | echo \<txt-record\>Staple=F\</txt-record\> >> $targ | |
| 146 | echo \<txt-record\>Copies=T\</txt-record\> >> $targ | |
| 147 | echo \<txt-record\>Collate=F\</txt-record\> >> $targ | |
| 148 | echo \<txt-record\>Punch=F\</txt-record\> >> $targ | |
| 149 | echo \<txt-record\>Bind=F\</txt-record\> >> $targ | |
| 150 | echo \<txt-record\>Sort=F\</txt-record\> >> $targ | |
| 151 | echo \<txt-record\>Scan=F\</txt-record\> >> $targ | |
| 152 | echo \<txt-record\>pdl=application/octet-stream,application/pdf,application/postscript,image/jpeg,image/png,image/urf\</txt-record\> >> $targ | |
| 153 | echo \<txt-record\>URF=W8,SRGB24,CP1,RS600\</txt-record\> >> $targ | |
| 154 | echo \</service\> >> $targ | |
| 155 | echo \</service-group\> >> $targ | |
| 156 | echo ---------------------------------------- | |
| 157 | echo Editing CUPS-Daemon Config | |
| 158 | ||
| 159 | targ=/etc/cups/cupsd.conf | |
| 160 | echo ServerAlias * >> $targ | |
| 161 | echo Port 631 >> $targ | |
| 162 | - | exit 0 |
| 162 | + | |
| 163 | ||
| 164 | echo ---------------------------------------- | |
| 165 | echo Restarting CUPS | |
| 166 | service cups restart | |
| 167 | echo Finished. Exit. | |
| 168 | exit 0 | |
| 169 | ||
| 170 |