mirror of https://framagit.org/kyodev/kyopages.git
142 lines
5.1 KiB
Bash
Executable File
142 lines
5.1 KiB
Bash
Executable File
#!/bin/bash
|
|
|
|
version=1.5.0
|
|
date=28.11.2017
|
|
|
|
BOLD=$(tput bold)
|
|
STD=$(tput sgr0)
|
|
|
|
f__error(){ # version spécifique
|
|
echo -e "\n $0 $version, erreur critique: $1\n"
|
|
exit 1
|
|
}
|
|
|
|
f__requis(){ # version spécifique
|
|
local command paquet debian
|
|
command=$(cut -d'>' -f1 <<< $1)
|
|
paquet=$(cut -d'>' -f2 <<< $1)
|
|
[ -f /etc/debian_version ] && debian=$(< /etc/debian_version)
|
|
if [ -z "$(which $command)" ]; then
|
|
echo -e "\n commande $command manquante\n"
|
|
[ "$debian" ] && echo -e " le paquet $paquet est nécessaire, installation\n apt install $paquet\n"
|
|
return 1
|
|
fi
|
|
}
|
|
|
|
# $1=rep à scanner [$2=profondeur max|4 défaut] [$3=profondeur encours|0 défaut]
|
|
# affichage stdout si $fileOutput non définis, /usr/bin/strings (binutils) requis
|
|
f__scandir(){ # 28/11/2017
|
|
[ -d "$1" ] || f__error "erreur sur le répertoire à scanner"
|
|
f__requis "strings>binutils" # requis pour fonctionnement programme
|
|
local repToScan irep rc text prof prof_max tempo
|
|
[ "${1: -1}" == "/" ] && repToScan="$1" || repToScan="$1/" # ajout / final si besoin
|
|
[ "$2" ] && prof_max="$2" || prof_max=4 # profondeur max par défaut si besoin
|
|
[ "$3" ] && prof=$3 || prof=0 # initialisation compteur profondeur en cours si nécessaire (début)
|
|
text=" >>> répertoire: $repToScan \n"
|
|
for irep in $(ls $repToScan); do # contenu du répertoire
|
|
prof=$(( $prof+1 )) # niveau++
|
|
if [ -d "$repToScan$irep" ]; then # si c'est un répertoire
|
|
# si compteur niveau <= max, scandir, sinon suivant dans la boucle
|
|
[ "$prof" -le "$prof_max" ] && f__scandir "$repToScan$irep/" "$prof_max" "$prof" || continue
|
|
else # si c'est pas un répertoire
|
|
if [ ! -r "$repToScan$irep" ]; then # si fichier non lisible (read)
|
|
text+="**:( $repToScan$irep : inaccessible en lecture \n"
|
|
continue # suivant dans la boucle
|
|
fi
|
|
# traitements fichier
|
|
[[ "$irep" == "uevent" || "$irep" == "modalias" ]] && rc=" \n" || unset rc
|
|
tempo="$(strings -aw -n1 $repToScan$irep)" # au moins 1 caractère, inclus white space, all tout le fichier
|
|
[ "$tempo" ] && text+=" fichier <$irep>: $rc$tempo \n" || text+=" fichier <$irep>: <vide> \n"
|
|
fi
|
|
prof=$(( prof-1 )) # niveau--
|
|
done
|
|
[ "$fileOutput" ] && echo -e "$text" >> "$fileOutput" # sauvegarde dans fichier si $fileOutput défini
|
|
[ "$fileOutput" ] || echo -e "$text" # affichage si $fileOutput non défini
|
|
}
|
|
|
|
help(){ # 15/10/2017
|
|
echo
|
|
echo "$BOLD"
|
|
echo " scan répertoire $version"
|
|
echo "$STD"
|
|
echo
|
|
echo " $0 répertoire [profondeur]"
|
|
echo " répertoire obligatoire"
|
|
echo " profondeur par defaut: 4"
|
|
echo
|
|
echo " si répertoire = all : scan des répertoires enregistrés"
|
|
echo " si répertoire absent: affichage de cette aide"
|
|
echo
|
|
echo " inscription dans fichier: _rep_a_scanner (répertoire: /rep/a/scanner/)"
|
|
echo
|
|
exit 1
|
|
}
|
|
|
|
# fileOutput, [ $1=_end ] [ $2=profondeur <> de $profondeur ]
|
|
fileSortie(){ # 17/10/2017
|
|
local prof
|
|
# fin file output
|
|
if [ "$1" == "_end" ]; then
|
|
echo '``` ' >> "$fileOutput"
|
|
return 0
|
|
fi
|
|
[ "$2" ] && prof="$2" || prof="$profondeur"
|
|
# formation nom fileOutput
|
|
fileOutput=$(sed 's#/#_#g' <<< $1) # remplace / par _
|
|
[ "${fileOutput: -1}" == "_" ] && fileOutput=${fileOutput::-1} # suppression dernier _ éventuel
|
|
fileOutput="$(dirname $0)/$fileOutput" #fichier de sauvegarde
|
|
# entete fileOutput
|
|
echo "$(basename $0) $version " > "$fileOutput"
|
|
echo "# scan: $1 " >> "$fileOutput"
|
|
echo "profondeur: $prof " >> "$fileOutput"
|
|
echo "hôte: $(uname -n) " >> "$fileOutput"
|
|
echo "$(date '+%d/%m/%Y %H:%M %z')" >> "$fileOutput"
|
|
echo >> "$fileOutput"
|
|
if [ "$EUID" -eq 0 ] || [ "$SUDO_UID" ]; then
|
|
echo "Attendion exécution en root/superutilisateur"
|
|
echo "exécution en root/superutilisateur " >> "$fileOutput"
|
|
fi
|
|
}
|
|
|
|
|
|
#------ départ script ----------------------------------------------------------
|
|
|
|
# traitement options
|
|
[[ -d "$1" || "$1" == "all" ]] || help # si appel incorrect, help
|
|
[ "$2" ] && profondeur=$2 || profondeur=4 # profondeur par défaut
|
|
|
|
# syntaxe pour une profondeur spécifique autre que 4: répertoire>profSpécifiq
|
|
# * pour expansion nom de fichier/répertoire permis
|
|
# /!\ biensur, pas d'espaces dans les répertoire à scanner
|
|
liste="
|
|
/proc/acpi/ /proc/sys/vm/
|
|
/sys/class/>2 /sys/class/dmi/ /sys/class/hwmon/ /sys/class/net/>2 /sys/class/power_supply/
|
|
/sys/class/sound/>3 /sys/class/thermal/>3
|
|
/sys/devices/>2 /sys/devices/platform/coretemp.*/>2 /sys/devices/system/cpu /sys/devices/virtual/>3
|
|
/sys/class/thermal/thermal_zone0/>2 /sys/devices/virtual/thermal/thermal_zone0/>2 "
|
|
|
|
#### scandir
|
|
if [ "$1" == "all" ];then # scan liste
|
|
for ifile in $liste; do
|
|
for ifilex in $(cut -d '>' -f1 <<< $ifile); do # décompose le nom si *
|
|
echo "scan: $ifilex"
|
|
if [ "$(cut -d '>' -f2 <<< $ifile | grep -c '[0-9]')" -eq 1 ]; then #2e champs numérique
|
|
fileSortie "$ifilex" "$(cut -d '>' -f2 <<< $ifile)"
|
|
f__scandir "$ifilex" "$(cut -d '>' -f2 <<< $ifile)"
|
|
else
|
|
fileSortie "$ifilex"
|
|
f__scandir "$ifilex" "$profondeur"
|
|
fi
|
|
fileSortie "_end"
|
|
done
|
|
done
|
|
else # scan unitaire
|
|
fileSortie "$1"
|
|
f__scandir "$1" "$profondeur"
|
|
fileSortie "_end"
|
|
# file -bi "$fileOutput"
|
|
[ "$(which less)" ] && less "$fileOutput" || more "$fileOutput"
|
|
fi
|
|
|
|
exit 0
|