Barra laterale

programmazione:bash:script_per_gestire_stampanti

Uno script grafico per gestire stampanti cups

Autore: Fabio Di Matteo
Ultima revisione: 09/03/2016 - 13:37

Sono ottime le gui per gestire Cups disponibili nelle varie distribuzioni. Ho avuto pero' l'esigenza di assegnare un ppd manualmente ad una stampante, e l'operazione non credo sia possibile se non da riga di comando. Ecco le istruzioni per gestire cups da riga di comando. Prendendo spunto da esse ho scritto il seguente script grafico:

#!/bin/bash
#Deps: zenity
 
VER=0.1
 
 
 
 
function checkRoot(){
	if [ "$(id -u)" != "0" ]; then
		echo "Devi avere i permessi di root per usare questo script"  ;
		exit ;
	fi
}
 
 
function exitNow(){
	zenity --question --text "Vuoi veramente terminare lo script?";
	res=$?
	if [ "$res" == "0" ]; then
		echo "Uscita dallo script..."  ;
		#zenity --info --text "Script terminato dall'utente."
		exit ;
	fi
}
 
 
 
function showPrinters(){
	lpstat -a > /tmp/printers.txt
 
 
 
	printers=$(cat /tmp/printers.txt|grep -Eo '^[^ ]+')
 
	res=$(zenity --width=400 \
	--height=350 \
	--title="addPrinter $VER" \
	--list \
	--text="Seleziona una tra queste stampanti rilevate: " \
	--column="Stampante"  \
	$printers
	)
 
	rm -f /tmp/printers.txt  
	mainMenu
}
 
 
 
 
function deletePrinter(){
	lpstat -a > /tmp/printers.txt
 
 
 
	printers=$(cat /tmp/printers.txt|grep -Eo '^[^ ]+')
 
	res=$(zenity --width=400 \
	--height=350 \
	--title=" Elimina stampante- addPrinter $VER" \
	--list \
	--text="Seleziona una tra queste stampanti rilevate: " \
	--column="Stampante"  \
	$printers
	)
 
	rm -f /tmp/printers.txt 
	if  [ "$res" == "" ]; then
		mainMenu
	fi
 
	cupsreject $res
	cupsdisable $res
	lpadmin -x $res
 
 
 
	deletePrinter
 
}
 
 
function showPdds(){
	zenity  --timeout=15 --info --text "<b>Il rilevamento potrebbe impiegare del tempo, attendere prego...</b> \n \n Questo messaggio verra chiuso fra poco" &
 
	find /usr -name "*.ppd*" > /tmp/ppds.txt
	find /etc -name "*.ppd*" >> /tmp/ppds.txt
	find /opt -name "*.ppd*" >> /tmp/ppds.txt
 
	ppds=$(cat /tmp/ppds.txt)
 
	res=$(zenity --width=600 \
	--height=450 \
	--title="addPrinter $VER" \
	--list \
	--editable \
	--text="Ecco tutti i file ppd rilevati: " \
	--column="Stampante"  \
	$ppds
	)
 
	rm -f /tmp/ppds.txt  
	mainMenu
 
}
 
 
function setName(){
	res=$(zenity --entry \
	--title="Nome della stampante" \
	--text="Nome stampante:" \
	--entry-text "myPrinter")
 
	if  [ "$res" == "" ]; then
		mainMenu
	fi
 
	echo $res >/tmp/printername.txt
 
 
	PPD=$(cat /tmp/ppd.txt)
	DEVICE=$(cat /tmp/device.txt)
	PRINTERNAME=$(cat /tmp/printername.txt)
 
 
	lpadmin -p $PRINTERNAME -E -v "$DEVICE"  -P $PPD
	echo "Nome stampante: $PRINTERNAME"
	echo "Device: $DEVICE"
	echo "PPD: $PPD"
	rm -f /tmp/ppd.txt /tmp/device.txt /tmp/printername.txt
 
	mainMenu
}
 
 
function setPPD(){
 
	res=$(zenity --width=400 \
	--height=350 \
	--title="Seleziona un file ppd per la stampante - addPrinter $VER" \
	--file-selection )
	if  [ "$res" == "" ]; then
		mainMenu
	fi
 
	echo $res >/tmp/ppd.txt
 
	setName
}
 
 
function detectDirectPrinters(){
 
	zenity  --timeout=5 --info --text "<b>Il rilevamento potrebbe impiegare del tempo, attendere prego...</b> \n \n Questo messaggio verra chiuso fra poco" &
 
 
	printers=$(lpinfo -v |grep direct | sed -e 's/\<direct\>//g')
 
	echo $printers
 
 
	res=$(zenity --width=400 \
	--height=350 \
	--title="addPrinter $VER" \
	--list \
	--text="Seleziona una tra queste stampanti rilevate: " \
	--column="Stampante"  \
	$printers
	)
	if  [ "$res" == "" ]; then
		mainMenu
	fi
 
	echo $res >/tmp/device.txt
	setPPD
}
 
 
 
 
function mainMenu(){
	res=$(zenity --width=400 \
	--height=350 \
	--title="addPrinter $VER" \
	--list \
	--text="Seleziona una operazione" \
	--hide-column=1 \
	--column=id --column="Operazione" \
			0 	"Aggiungi stampante " \
			1	"Mostra stampanti installate" \
			2	"Elimina stampante" \
			3	"Mostra file ppd nel sistema" \
			200	"Esci" )
 
 
 
	if  [ "$res" == "0" ]; then
		detectDirectPrinters 
	fi
 
	if  [ "$res" == "1" ]; then
		showPrinters
	fi
	if  [ "$res" == "2" ]; then
		deletePrinter
	fi
	if  [ "$res" == "3" ]; then
		showPdds
	fi
 
 
	if  [ "$res" == "200" ]; then
		exitNow
	fi
	if  [ "$res" == "" ]; then
		exitNow
	fi
}
 
 
checkRoot
mainMenu

programmazione/bash/script_per_gestire_stampanti.txt · Ultima modifica: 18/04/2018 - 15:48 (modifica esterna)