rschrenk

AirPrint unter Ubuntu automatisch aktivieren

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