Bon, ben tout est dans le titre, je pense que cela peut etre utile
Si on veut changer l'emplacement du fichier de log, il suffit de changer la variable VAR_DIR
Ca peut aussi servir à surveiller l'activité du script
Il suufit d'apeler la fonction et ensuite le truc qu'on veut faire en root
par ex.: rootify && sudo -S apt-get update, mais bon c'est adaptable au besoin
#!/bin/bash
VAR_DIR=`pwd`
ROOTIFY_COUNTER=1
rootify(){
echo -n "`date | cut -d"(" -f1`: Authentification de l'utilisateur " >> $VAR_DIR/rootify.log
sudo -S echo -n $USER >> $VAR_DIR/rootify.log
AUTH=$(cat $VAR_DIR/rootify.log | sed -n $((`cat $VAR_DIR/rootify.log | wc -l`+1))'p' | cut -d":" -f4 | cut -d" " -f5)
if [ "$AUTH" != "$USER" ] ; then zenity --title="Authentification" --password |
sudo -S echo -n $USER >> $VAR_DIR/rootify.log ; fi
AUTH=$(cat $VAR_DIR/rootify.log | sed -n $((`cat $VAR_DIR/rootify.log | wc -l`+1))'p' | cut -d":" -f4 | cut -d" " -f5)
if [ "$AUTH" != "$USER" ] ; then echo "$USER [FAILED]" >> $VAR_DIR/rootify.log ; ((ROOTIFY_COUNTER++))
if [ $ROOTIFY_COUNTER -lt 3 ] ; then rootify
elif [ $ROOTIFY_COUNTER -eq 3 ] ; then zenity --warning \
--text="Attention, ceci est votre 3° et dernier essai\r\rEn cas d'échec, ce programme sera fermé automatiquement."
rootify
else exit 1 ; fi
else echo " [PASSED]" >> $VAR_DIR/rootify.log ; $ROOTIFY_COUNTER=1 ; fi
}
rootify
exit 0