getInfo 197 KB
Newer Older
Kyodev's avatar
Kyodev committed
1
#!/bin/bash
kyodev's avatar
kyodev committed
2 3
# shellcheck disable=SC1117,SC2016,SC2009
# shellcheck disable=SC2207
Kyodev's avatar
Kyodev committed
4

kyodev's avatar
kyodev committed
5
# SC1117 Backslash is literal in "\n". Prefer explicit escaping: "\\n"
kyodev's avatar
kyodev committed
6 7
# SC2016 Expressions don't expand in single quotes
# SC2009 Consider using pgrep instead of grepping ps output
kyodev's avatar
kyodev committed
8
# SC2207: Prefer mapfile or read -a to split command output (or quote to avoid splitting).
kyodev's avatar
kyodev committed
9

kyodev's avatar
kyodev committed
10
version=4.7.0
kyodev's avatar
kyodev committed
11
date="16/06/2018"
Kyodev's avatar
Kyodev committed
12
script="getInfo"
kyodev's avatar
kyodev committed
13
contact="IRC freenode.net ##sdeb@freenode.net ou https://framagit.org/sdeb/getInfo/issues"
Kyodev's avatar
Kyodev committed
14 15

##### license LPRAB/WTFPL
kyodev's avatar
kyodev committed
16
#  auteur: sdeb
Kyodev's avatar
Kyodev committed
17
#    contributeurs: kyodev, saiqui, naguam, agentcobra, amilcar
Kyodev's avatar
Kyodev committed
18 19
#####

Kyodev's avatar
Kyodev committed
20
	# détecte architecture système, assign $fu_archi: 32bits, i686 | 64bits, amd64 (x86_64)
Kyodev's avatar
Kyodev committed
21
	# return 1 on unknown architecture
Kyodev's avatar
Kyodev committed
22
	# remarque, debian: dpkg --print-architecture affiche i386
kyodev's avatar
kyodev committed
23
f__architecture(){		# 14/O6/2018
kyodev's avatar
kyodev committed
24

kyodev's avatar
kyodev committed
25
	x_architecture=1
Kyodev's avatar
Kyodev committed
26
	case "$(uname -m)" in
kyodev's avatar
kyodev committed
27 28
		amd64 | x86_64 ) 	fu_archi="64bits, amd64 (x86_64)";;
		i?86 | x86 )		fu_archi="32bits, i686";;
Kyodev's avatar
Kyodev committed
29 30
		* )
			case "$(getconf LONG_BIT)" in
kyodev's avatar
kyodev committed
31 32 33
				64 ) 	fu_archi="64bits, amd64 (x86_64)";;
				32 ) 	fu_archi="32bits, i686";;
				*) 		return 1
Kyodev's avatar
Kyodev committed
34 35 36 37
			esac ;;
	esac
}

kyodev's avatar
kyodev committed
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
	# test connexion, [$1=-4|-6] protocole -4 par défaut, return 0|1
f__cnx(){		# 15/06/2018-2
	local proto="-4" cmds cmd ip servers

	if [[ "$1" == '-6' || "$1" == '6' ]]; then
		proto="-6"
		servers=(
			2606:4700:4700::1111	# cloudfare
			2001:7fd::1				# root K RIPE
			2001:500:a8::e			# root E nasa 
			2001:4860:4860::8888	# google
		)
	else
		servers=(
			1.1.1.1					# cloudfare
			193.0.14.129			# root K RIPE
			192.203.230.10			# root E nasa 
			195.135.221.140			# conncheck.opensuse.org
			80.67.169.12			# ns0.fdn.fr
			8.8.8.8					# google
		)
	fi

	type -p ping  &>/dev/null && cmds+=( "ping -c1 -w1 -U " )
	type -p fping &>/dev/null && cmds+=( "fping --retry=0 --elapsed --stats " )
	if (( ${#cmds[@]} == 0 )); then
		echo "f__cnx(): no available commands" >&2
		return
	fi

	for cmd in "${cmds[@]}"; do
		for ip in "${servers[@]}" ; do 
			$cmd "$proto" "$ip" &>/dev/null && return 0
		done
	done
	if nc -4 -h &>/dev/null; then		# pris en charge par netcat-openbsd
		for ip in "${servers[@]}" ; do 
			nc "$proto" -z -w1 "$ip" 53 &>/dev/null && return 0		# paquet netcat-traditional debian obsolete, ne gere pas protocoles
		done
	fi
	return 1
}

kyodev's avatar
kyodev committed
81
# shellcheck disable=SC2034
kyodev's avatar
kyodev committed
82
f__color(){		# 29/05/2018
kyodev's avatar
kyodev committed
83 84

	if type -p tput &>/dev/null && tput setaf 1 &>/dev/null; then
kyodev's avatar
kyodev committed
85
		MAGENTA=$( tput setaf 5 )
kyodev's avatar
kyodev committed
86
		  BLACK=$( tput setaf 0 )
kyodev's avatar
kyodev committed
87
		   CYAN=$( tput setaf 6 )
kyodev's avatar
kyodev committed
88
		 YELLOW=$( tput setaf 3 )		# question
kyodev's avatar
kyodev committed
89
		  GREEN=$( tput setaf 2 )		# ok
kyodev's avatar
kyodev committed
90
		   BLUE=$( tput setaf 4 )		# info
kyodev's avatar
kyodev committed
91
			RED=$( tput setaf 1 )		# alerte
kyodev's avatar
kyodev committed
92 93 94 95 96
			STD=$( tput sgr0 )			# retour "normal"
		BOLD=$( tput bold )
		ITAL=$( tput sitm )
		SOUL=$( tput smul )
	else 
kyodev's avatar
kyodev committed
97 98 99 100 101
		YELLOW=$'\033[0;33m'		# ?
		 GREEN=$'\033[0;32m'		# ok
		  BLUE=$'\033[0;34m'		# info
		   RED=$'\033[0;31m'		# alerte
		   STD=$'\033[0m'			# standard
kyodev's avatar
kyodev committed
102
	fi
Kyodev's avatar
Kyodev committed
103 104
}

kyodev's avatar
kyodev committed
105 106 107 108
	# $1 option: oui|non | clear | -txy, $2=message, return 0 si oui, return 1 si non
	# options: oui|non: réponse par défaut si enter seul,  -txy timeout (sans espaces entre t et chiffres) 1 à 99s
	# clear effacement ligne message/réponse
f__dialog_oui_non(){		# 14/06/2018
Kyodev's avatar
Kyodev committed
109
	local param
kyodev's avatar
kyodev committed
110

kyodev's avatar
kyodev committed
111 112 113 114
	if (( "$#" < 2 )); then
		echo 'f__dialog_oui_non(): manque options'
		exit
	fi
kyodev's avatar
kyodev committed
115
		# extraction timeout éventuel
kyodev's avatar
kyodev committed
116 117
	if [[ $1 =~ (-t[0-9]{1,2}) ]]; then
		param=${BASH_REMATCH[1]}
kyodev's avatar
kyodev committed
118 119
	fi
		# affichage
kyodev's avatar
kyodev committed
120
	echo -en "$2"
Kyodev's avatar
Kyodev committed
121
	[[ "$1" =~ oui ]] &&  echo -n " [O/n] " || echo -n " [o/N] " 
kyodev's avatar
kyodev committed
122 123
	read "${param[@]}" -r
	[ "$param" ] && echo
kyodev's avatar
kyodev committed
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
		# réponse par défaut si saisie vide
	if [[ -z "$REPLY" && "$1" =~ oui ]]; then
		REPLY="oui"
	elif [[ -z "$REPLY" && "$1" =~ non ]]; then
		REPLY="non"
	fi
		# effacement éventuel ligne question/réponse
	if [[ "$1" =~ clear ]]; then
		tput cuu1		# une ligne plus haut
		tput dl1		# efface ligne
	else
		echo
	fi
	if [[ ${REPLY,,} =~ ^ou?i?$ ]]; then 
		return 0
	else
		return 1
Kyodev's avatar
Kyodev committed
141
	fi
Kyodev's avatar
Kyodev committed
142 143
}

kyodev's avatar
kyodev committed
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
	# use dig or drill according to availability, options: hostname [@server|type|nostd|host], display ip | n/a (equivalent + short)
	# if option --host, diplay hostname: ip
	# dig has priority over dril if the two command are presents
	# type : aaaa | a | txt | soa | any | mx | AAAA | A | TXT | SOA | ANY | MX 
	# option nostd for dig only: +timeout=1 +retry=1
f__dig(){		# 16/06/2018
	local ini options type host server hostname dig ip domain ttl class typ ipx z

	ini=$( shopt -p nocasematch )
	if (( $# == 0 )); then
		echo "f__dig: argument manquant"
		return 1
	fi
	shopt -s nocasematch
	while (( $# )) ; do
		case "$1" in
			nostd  ) options+=( '+timeout=1' '+retry=1' ) ;;
			aaaa | a | txt | soa | any | mx ) type="$1" ;;
			host ) host='yes' ;;
			@* ) server=( "$1" ) ;;
			* ) hostname="$1" ;;
		esac
		shift
	done
	type="${type:=A}"
	if type -p dig &>/dev/null; then
		dig=$( dig "${options[@]}" "$hostname" "${server[@]}" "$type" )
	elif type -p drill &>/dev/null; then
		dig=$( drill "$hostname" "${server[@]}" "${type[@]}" )
	else
		echo "dig or drill command unavailable"
		eval "$ini"
		return 1
	fi
	if [[ $dig =~ answer:\ 0 ]]; then
		dig='n/a'
	elif [[ $dig =~ answer:\ 1 ]]; then
		# shellcheck disable=SC2034
		while read -r domain ttl class typ ipx z; do
			if [[ $domain$class$typ == ${hostname}\.IN$type ]]; then
				ip+="$ipx "
			fi
		done <<< "$dig"
		dig="$ip"
	fi
	[ "$host" ] && echo -n "$hostname: "
	echo "$dig"
	eval "$ini"
	if [ "$dig" == "n/a" ]; then
		return 1
	fi
}

Kyodev's avatar
Kyodev committed
197 198
	# $1=-c|-l|-lc, $2 répertoire, [$3] motif exclusion sur nom fichier,  affiche 0|null si répertoire inexistant
	# -c: compte du nombre de fichiers dans un répertoire
kyodev's avatar
kyodev committed
199 200
	# -l: liste inline des noms de fichiers seuls (sans chemin) (similaire ls)
	# -lc: liste en colonne des noms de fichiers seuls (sans chemin) (similaire ls -1)
Kyodev's avatar
Kyodev committed
201
	# si joker (*?) sur chemin, le motif d'exclusion ne s'applique pas à un répertoire listé, juste sur le nom du fichier
kyodev's avatar
kyodev committed
202 203
	# exemple: f__dir -l $dir "lock|partial" ou "\.list"
f__dir(){		 # 05/03/2017
Kyodev's avatar
Kyodev committed
204 205
	local i=0 action="-c" stock=''

kyodev's avatar
kyodev committed
206
	[[ "$1" =~ - ]] && action="$1" || action="-c"	# si option en erreur: -c par défaut
Kyodev's avatar
Kyodev committed
207 208 209 210 211 212 213
	while read -r ; do 
		if [[ ! "$REPLY" =~ $3 ]] || [ -z "$3" ]; then
			REPLY=${REPLY#*:}				# suppression du titre de répertoire listé
			[ -z "$REPLY" ] && continue		# ligne vide, on passe
			((i++))
			stock+="$REPLY "
		fi
kyodev's avatar
kyodev committed
214
	done <<< "$( ls "$2" 2>/dev/null )"
Kyodev's avatar
Kyodev committed
215 216 217
	if [ "$action" == "-c" ]; then
		echo "$i"
	elif [ "$action" == "-l" ]; then
kyodev's avatar
kyodev committed
218
		echo "${stock% }"
Kyodev's avatar
Kyodev committed
219 220 221 222
	elif [ "$action" == "-lc" ]; then
		stock=${stock% }
		echo -e "${stock// /$'\n'}"
	fi
Kyodev's avatar
Kyodev committed
223 224
}

Kyodev's avatar
Kyodev committed
225
	# affichage $1 en rouge, $1++ optionnels en bleu, sortie script sur erreur, log $1 si $operation=upgrade
kyodev's avatar
kyodev committed
226
f__error(){		# 07/O6/2018
Kyodev's avatar
Kyodev committed
227 228
	local depart=1 i

kyodev's avatar
kyodev committed
229
	echo -e "\\n$RED  $script $version, erreur critique: $1 $STD" >&2
Kyodev's avatar
Kyodev committed
230
	for (( i=2 ; i<=$# ; i++ )); do
kyodev's avatar
kyodev committed
231
		echo -e "  $BLUE${!i}$STD" >&2
Kyodev's avatar
Kyodev committed
232 233
	done
	echo
Kyodev's avatar
Kyodev committed
234
	[ "$operation" == "upgrade" ] && f__log "$script $version: $1"
Kyodev's avatar
Kyodev committed
235 236 237
	exit 1
}

Kyodev's avatar
Kyodev committed
238
	# affichage en bleu, si $1=raw pas de ligne vide à la fin, si $1=log alors uniquement $2 logué, combiné: $1="log:raw"
kyodev's avatar
kyodev committed
239 240
f__info(){		# 07/06/2018
	local depart=1 log i
Kyodev's avatar
Kyodev committed
241

kyodev's avatar
kyodev committed
242 243 244 245
	if [[ "$1" =~ "raw"  ||  "$1" =~ "log" ]]; then
		depart=2
	fi
	if [[ "$1" =~ "log" ]]; then
kyodev's avatar
kyodev committed
246 247 248 249 250
		log="$2"
		log=${log//$'\t'}
		log=${log//$'\n'}
		f__trim log
		f__log "$log"
kyodev's avatar
kyodev committed
251
	fi
kyodev's avatar
kyodev committed
252
	for (( i=depart ; i<=$# ; i++ )); do
Kyodev's avatar
Kyodev committed
253 254
		echo -e "  $BLUE${!i}$STD"
	done
Kyodev's avatar
Kyodev committed
255
	[[ "$1" =~ raw ]] || echo 
Kyodev's avatar
Kyodev committed
256 257
}

Kyodev's avatar
Kyodev committed
258
	# log spécifique, fichier log limité à 10000 octets, $1 message à loguer
kyodev's avatar
kyodev committed
259
f__log(){		# 05/03/2018
Kyodev's avatar
Kyodev committed
260 261

	if [ -w "$script_logs" ];  then 
kyodev's avatar
kyodev committed
262
		if [ "$( stat -c %s  "$script_logs" )" -ge "10000" ]; then
Kyodev's avatar
Kyodev committed
263
			echo "$(date +%Y%m%d\ %H%M%S) $1"  &>/dev/null > "$script_logs"
Kyodev's avatar
Kyodev committed
264
		else 
Kyodev's avatar
Kyodev committed
265
			echo "$(date +%Y%m%d\ %H%M%S) $1"  &>/dev/null >> "$script_logs"
Kyodev's avatar
Kyodev committed
266 267
		fi
	fi
Kyodev's avatar
Kyodev committed
268 269
}

kyodev's avatar
kyodev committed
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
	# $1=bas-haut (ex: 0-5), nombre aléatoire entre bas & haut inclus, si [$2=seq] liste avec départ aléatoire
f__random(){		# 02/06/2018
	local bas haut max rand start sequence xyz

	[ "${1//*-*}" ] && return 1		# bad format, no -
	bas=${1%-*}
	haut=${1#*-}
    max=$(( 32768 / ( haut + 1 ) * ( haut + 1 ) )) 
    while (( (rand=RANDOM) >= max )); do : ; done
	if [ "$2" == "seq" ]; then
		start=$(( bas + ( rand % (haut+1-bas) ) ))
		for (( xyz=start; xyz <= haut; xyz++ )); do echo "$xyz"; done 
		(( (start-bas) > 0 )) && for (( xyz=bas; xyz < start; xyz++ )); do echo "$xyz"; done
		return
	fi
	echo $(( bas + ( rand % (haut+1	-bas) ) ))
}

kyodev's avatar
kyodev committed
288
	# recherche commandes/paquets, $1 liste: cmd1|cmd2[>paquet] (séparées par espaces) ex: "gawk|mawk>gawk wget"
kyodev's avatar
kyodev committed
289
	# si manque, return 1 & affiche commandes manquantes (si debian, ajout proposition paquet à installer)
kyodev's avatar
kyodev committed
290 291
f__requis(){		# 11/06/2018
	local ENV_DEBIAN c_echo ireq cmds package commands command commandsMissing packagesMissing 
Kyodev's avatar
Kyodev committed
292

kyodev's avatar
kyodev committed
293
	if type -p dpkg &>/dev/null ; then
kyodev's avatar
kyodev committed
294 295 296 297 298 299 300
		ENV_DEBIAN="oui"							# debian
	fi
	if type -t f__info &>/dev/null; then
		c_echo="f__info"
	else
		c_echo="echo -e"					# f__info existe? sinon echo
	fi
kyodev's avatar
kyodev committed
301 302 303 304 305
	for ireq in $1; do
		IFS='>' read -r cmds package <<< "$ireq"			# ex: "awk|gawk>gawk wget file tar killall>psmisc"
		mapfile -t -d '|' commands <<< "${cmds}"
		[ -z "$package" ] && package=${commands[0]%$'\n'}	# pas de packages dans les options, donc = commands[0]
		for command in "${commands[@]%$'\n'}"; do
kyodev's avatar
kyodev committed
306 307 308 309 310 311 312 313
			if type -p "$command" &>/dev/null ; then
				unset commandsMissing packagesMissing
				break
			else	# inexistant
				commandsMissing+="$command "
				packagesMissing+="$package "
			fi
		done
Kyodev's avatar
Kyodev committed
314
	done
kyodev's avatar
kyodev committed
315 316

		# dédoublonnage & triage
kyodev's avatar
kyodev committed
317 318
	commandsMissing=$( echo "$commandsMissing" | tr ' ' '\n' | sort --unique | tr '\n' ' ' )
	packagesMissing=$( echo "$packagesMissing" | tr ' ' '\n' | sort --unique | tr '\n' ' ' )
kyodev's avatar
kyodev committed
319 320 321 322 323
		# suppression éventuel espace final
	commandsMissing=${commandsMissing% }
	packagesMissing=${packagesMissing% }
		# affichage final
	if [ "$commandsMissing" ] && [ "$ENV_DEBIAN" ]; then
kyodev's avatar
kyodev committed
324
		$c_echo "${RED}erreur critique, manquant: $STD$BOLD$commandsMissing" \
kyodev's avatar
kyodev committed
325 326
						"vous devriez exécuter:$GREEN apt install $packagesMissing"
	elif [ "$commandsMissing" ]; then
kyodev's avatar
kyodev committed
327
		$c_echo "${RED}erreur critique, manquant: $STD$BOLD$commandsMissing"
Kyodev's avatar
Kyodev committed
328
	fi
kyodev's avatar
kyodev committed
329 330 331 332

	[ "$commandsMissing" ] && return 1 || return 0
}

kyodev's avatar
kyodev committed
333
	# recherche paquet deb, $1 liste paquets ("paquet1 paquet2"), si un paquet absent return 1
kyodev's avatar
kyodev committed
334
	# assigne $deb_absent & $deb_present, si hors debian: return 1 mais $deb_present & $deb_absent vides
kyodev's avatar
kyodev committed
335
f__requis_deb(){		# 14/06/2018
kyodev's avatar
kyodev committed
336 337
	local ireq 

kyodev's avatar
kyodev committed
338
	type -p 'dpkg' &>/dev/null || return 1		# si pas dpkg, sortie erreur
kyodev's avatar
kyodev committed
339

kyodev's avatar
kyodev committed
340 341 342 343 344 345 346
	unset deb_present deb_absent
	for ireq in "$@"; do
		if LC_ALL=C dpkg --get-selections | grep -qE "^${ireq}[[:space:]]+install" ; then
			deb_present+="$ireq "
		else
			deb_absent+="$ireq "
		fi
kyodev's avatar
kyodev committed
347 348 349 350 351
	done
	deb_absent=${deb_absent% }			# suppression éventuel espace final
	deb_present=${deb_present% }		# suppression éventuel espace final
	[ "$deb_absent" ] && return 1 
	[ "$deb_present" ] && return 0
Kyodev's avatar
Kyodev committed
352
}
Kyodev's avatar
Kyodev committed
353

kyodev's avatar
kyodev committed
354 355
	# $@=cmd à lancer en root avec su ou sudo. si sudo possible: prioritairesu su
f__sudo(){		# 11/06/2018
Kyodev's avatar
Kyodev committed
356

Kyodev's avatar
Kyodev committed
357
	if sudo -v &>/dev/null && [ $EUID -ne 0 ] ; then
kyodev's avatar
kyodev committed
358
		sudo su --preserve-environment -c "$@"
Kyodev's avatar
Kyodev committed
359
	else
kyodev's avatar
kyodev committed
360 361
		echo -n "[su]   "
		su --preserve-environment -c "$@"
kyodev's avatar
kyodev committed
362
	fi
Kyodev's avatar
Kyodev committed
363 364
}

kyodev's avatar
kyodev committed
365 366 367
	# $1=NOM de la variable à trimer (variable et non $variable), [$2=left|right|all], all si vide
	# gain vitesse en test: 40 à 75% par rapport à '| xargs'
	# var=$( function ) presque 2x lent que eval "var="
kyodev's avatar
kyodev committed
368
f__trim(){		# 07/03/2018
kyodev's avatar
kyodev committed
369
	local trim=${!1}
kyodev's avatar
kyodev committed
370

kyodev's avatar
kyodev committed
371 372 373
	[[ "$2" == right || "$2" == all || -z "$2" ]] && trim="${trim%${trim##*[^[:space:]]}}"		# fin right
	[[ "$2" == left  || "$2" == all  || -z "$2" ]] && trim="${trim#${trim%%[^[:space:]]*}}"		# début left
	eval "$1=\"$trim\""
kyodev's avatar
kyodev committed
374 375
}

kyodev's avatar
kyodev committed
376 377
	# $1 chaîne où convertir les unités
f__unit_french(){		# 08/03/2018
kyodev's avatar
kyodev committed
378 379 380 381 382 383 384 385
	local display

	display=${1//G/Go}
	display=${display//M/Mo}
	display=${display//K/ko}
	echo "$display"
}

kyodev's avatar
kyodev committed
386
	# $1=nombre à convertir en ko, affiche ko ou Mo ou Go
kyodev's avatar
kyodev committed
387 388
f__unit_human(){		# 05/03/2018

kyodev's avatar
kyodev committed
389
	echo -n "$( awk ' {
Kyodev's avatar
Kyodev committed
390 391 392
		if ( $1<1024 ) {unit="ko"; printf "%d%s", $1, unit; exit}
		if ( $1<1024*1024 && $1>=1024  ) {unit="Mo"; printf "%d%s", $1/1024, unit}
		else {unit="Go"; printf "%.1f%s", $1/1024/1024, unit}
root's avatar
root committed
393
		}' <<< "$1" )"
Kyodev's avatar
Kyodev committed
394 395
}

Kyodev's avatar
Kyodev committed
396 397 398
	# user ayant initié la session graphique, assigne $fu_user
	# return 1 sur échec identification user, return 2 sur absence home/
	# gestion variable environnement user avec: USER_INSTALL=<user> script
kyodev's avatar
kyodev committed
399 400
f__user(){		# 08/03/2018
	local user userid root_login
Kyodev's avatar
Kyodev committed
401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416

	root_login="$(grep ':0:' /etc/passwd | cut -d':' -f1)" || root_login="root"
	if [ "$USER_INSTALL" ]; then 	# user via variable environnement, moyen d'injecter root si pb 
		fu_user="$USER_INSTALL"; 
		return 0
	elif [[ "$TERM" =~ linux ]]; then	#debian 9 recovery ou nomodeset TERM=linux
		if [ "$USER" ]; then
			user="$USER"
		elif [ "$EUID" -eq 0 ]; then
			fu_user="$root_login"
			return 0
		fi
	fi
	if [ "$SUDO_UID" ]; then 
		userid="$SUDO_UID"; 
	elif grep -qEo '[0-9]+' <<< "$XDG_RUNTIME_DIR" ; then 
root's avatar
root committed
417
		userid=$( grep -Eo '[0-9]+' <<< "$XDG_RUNTIME_DIR" | cut -d'/' -f4 )
kyodev's avatar
kyodev committed
418
	else
root's avatar
root committed
419
		userid=$( grep -Eo '[0-9]+' <<< "$XAUTHORITY" | cut -d'/' -f4 )
Kyodev's avatar
Kyodev committed
420
	fi
kyodev's avatar
kyodev committed
421
	[ "$userid" ] && user=$( grep "$userid" /etc/passwd | cut -d ":" -f 1 )
Kyodev's avatar
Kyodev committed
422 423 424 425 426 427
	if [ "$user" ] && [ "$user" != "$root_login" ]; then
		fu_user="$user"
		return 0
	else
		if [ "$SUDO_USER" ] && [ "$SUDO_USER" != "$root_login" ]; then 
			user="$SUDO_USER"; 
kyodev's avatar
kyodev committed
428 429
		elif who | grep -qv 'root'; then 
			user=$( who | grep -v 'root' | head -n1 | cut -d ' ' -f1 );	# who | grep -v 'root' | awk 'FNR==1{print $1}'
kyodev's avatar
kyodev committed
430 431
		else
			user=$( grep -m1 'hourly.*get[A-Z].*\.anacrontab.*\.config/anacron/spool' /etc/crontab | cut -d' ' -f2 );
Kyodev's avatar
Kyodev committed
432 433 434 435 436 437 438 439
		fi
	fi
	fu_user="$user"
	[ "$fu_user" ] || return 1
	[ -d "/home/$fu_user" ] || return 2
	return 0
}

Kyodev's avatar
Kyodev committed
440
	# $1='-l' comptage ligne dans variable $2, affiche quantité
Kyodev's avatar
Kyodev committed
441 442
	# $1='-w' comptage dans variable $2 des mots
	# $1='-wv' comptage dans variable $2, des mots $3, affiche quantité
Kyodev's avatar
Kyodev committed
443
	# f__wcv -l $var ;  f__wcv -w $var  ;  f__wcv -wv $var "mot"
kyodev's avatar
kyodev committed
444
f__wcv(){		# 09/03/2018
kyodev's avatar
kyodev committed
445

kyodev's avatar
kyodev committed
446
	[[ "$1" =~ -l|-wv|-w  ]] || echo "erreur f__wcv \$1 ($1) incorrect"
kyodev's avatar
kyodev committed
447 448 449 450 451 452 453 454 455
	if [ "$1" == "-l"  ]; then
		grep -cEv '^[[:space:]]*$' <<< "$2"		# (wc -l compterait 1 pour une variable vide)
	elif [ "$1" == "-w"  ]; then
		result=$( xargs <<< "$2" | grep -o '[[:graph:]][[:blank:]][[:graph:]]' | grep -c ' ' )
		[ "$result" -ne 0 ] && result=$(( result+1 ))
		echo "$result"
	elif [ "$1" == "-wv"  ]; then
		grep -o "$3" <<< "$2" | grep -c  .
	fi
Kyodev's avatar
Kyodev committed
456 457
}

Kyodev's avatar
Kyodev committed
458 459
	# test wget, $1=url à tester, $2=''|print|loc|test
	# par défaut, sortie du script (même si url testée ok) avec affichage erreur ou ok
Kyodev's avatar
Kyodev committed
460 461 462
	# si $2=print affiche url testée & entêtes http & location, return 0
	# si $2=loc affiche seulement location, return 0
	# si $2=test return 0 si ok, return 1 si KO
kyodev's avatar
kyodev committed
463
f__wget_test(){		# 07/06/2018
Kyodev's avatar
Kyodev committed
464
	local file_test_wget="/tmp/testWget-$script" retourWget retourHttp location
Kyodev's avatar
Kyodev committed
465

Kyodev's avatar
Kyodev committed
466
	wget -Sq --timeout=5 --tries=2 --user-agent="$user_agent" --spider --save-headers "$1" &>"$file_test_wget"
Kyodev's avatar
Kyodev committed
467
	retourWget="$?"
Kyodev's avatar
Kyodev committed
468 469 470 471 472 473 474 475
	[ "$retourWget" == 1 ] && retourWget="1: code erreur générique"
	[ "$retourWget" == 2 ] && retourWget="2: parse erreur (ligne de commande?)"
	[ "$retourWget" == 3 ] && retourWget="3: erreur Entrée/sortie fichier"
	[ "$retourWget" == 4 ] && retourWget="4: défaut réseau"
	[ "$retourWget" == 5 ] && retourWget="5: défaut vérification SSL"
	[ "$retourWget" == 6 ] && retourWget="6: défaut authentification"
	[ "$retourWget" == 7 ] && retourWget="7: erreur de protocole"
	[ "$retourWget" == 8 ] && retourWget="8: réponse serveur en erreur"
Kyodev's avatar
Kyodev committed
476 477
	retourHttp=$( grep -i 'HTTP/' "$file_test_wget" | tr -d '\n' | xargs )
	location=$( grep -i 'location' $file_test_wget | xargs )
Kyodev's avatar
Kyodev committed
478
	if [ "$2" == "test" ]; then
Kyodev's avatar
Kyodev committed
479
		rm -f "$file_test_wget"
Kyodev's avatar
Kyodev committed
480
			# spécial maintenance frama.link, pas de redirection sur page status framalink
root's avatar
root committed
481
		grep -q '303' <<< "$retourHttp" && return 1			# 303 See Other
Kyodev's avatar
Kyodev committed
482 483
		[ "$retourWget" == "0" ] && return 0 || return 1
	fi
Kyodev's avatar
Kyodev committed
484
	if [ "$2" == "print" ]; then
Kyodev's avatar
Kyodev committed
485
		if [ "$retourWget" != "0" ]; then
kyodev's avatar
kyodev committed
486
			echo "  erreur wget: erreur $RED$retourWget" 
kyodev's avatar
kyodev committed
487
			echo -e "$BLUE  $1$STD\\t$RED  $retourHttp$STD"
Kyodev's avatar
Kyodev committed
488
		else
kyodev's avatar
kyodev committed
489
			echo -e "$BLUE  $1$STD\\t$GREEN  $retourHttp$STD" 
Kyodev's avatar
Kyodev committed
490 491
		fi
	fi
Kyodev's avatar
Kyodev committed
492
	if [ "$2" == "print" ] || [ "$2" == "loc" ]; then
kyodev's avatar
kyodev committed
493
		[ "$location" ] && echo -n "$YELLOW  $location" || echo -n "$YELLOW  no location"
Kyodev's avatar
Kyodev committed
494
		echo "$STD"
Kyodev's avatar
Kyodev committed
495
		rm -f "$file_test_wget"
Kyodev's avatar
Kyodev committed
496
		return 0
Kyodev's avatar
Kyodev committed
497
	fi
Kyodev's avatar
Kyodev committed
498
	if [ "$retourWget" != "0" ]; then
Kyodev's avatar
Kyodev committed
499
		rm -f "$file_test_wget"
Kyodev's avatar
Kyodev committed
500
		f__error "wget, erreur $retourWget" "$1" "$YELLOW$retourHttp" "$location"
kyodev's avatar
kyodev committed
501
		echo -e "$RED   erreur wget, $retourWget \\n  $1 \\n  $YELLOW$retourHttp \\n  $location$STD" # pour les diags
Kyodev's avatar
Kyodev committed
502
		return 1
Kyodev's avatar
Kyodev committed
503
	fi
root's avatar
root committed
504
	if grep -q '200' <<< "$retourHttp"; then 
kyodev's avatar
kyodev committed
505
		echo -e "$GREEN\\ntout est ok, réessayer$STD\\n"
Kyodev's avatar
Kyodev committed
506
	fi
Kyodev's avatar
Kyodev committed
507
	rm -f "$file_test_wget"
Kyodev's avatar
Kyodev committed
508
	exit 0
Kyodev's avatar
Kyodev committed
509 510
}

kyodev's avatar
kyodev committed
511
f_affichage(){		# 15/06/2018
Kyodev's avatar
Kyodev committed
512

kyodev's avatar
kyodev committed
513 514
	(( x_logo == 1 )) && return
	x_logo=1
kyodev's avatar
kyodev committed
515
	clear 2>/dev/null || tput clear 2>/dev/null
kyodev's avatar
kyodev committed
516 517 518 519 520 521
	echo -n "$BLUE"
	cat <<- end
	              _   ___        __       
	    __ _  ___| |_|_ _|_ __  / _| ___  
	   / _' |/ _ \ __|| || '_ \| |_ / _ \ 
	  | (_| |  __/ |_ | || | | |  _| (_) |
kyodev's avatar
kyodev committed
522
	   \__, |\___|\__|___|_| |_|_|  \___/  $STD$script_options
kyodev's avatar
kyodev committed
523 524 525
	   |___/  ${YELLOW}version $version - $date$STD
	
	end
Kyodev's avatar
Kyodev committed
526 527
}

Kyodev's avatar
Kyodev committed
528
	# $1=type de titre var|cmd|sans|+:text|+:vide, $2 variable à afficher [$3 titre] [$4 commentaire]
Kyodev's avatar
Kyodev committed
529
	# $1: cmd->`titre`, var->**titre**, sans: pas de titre
Kyodev's avatar
Kyodev committed
530
	# :text ajouté, affiche le text en liste (avec puce)
Kyodev's avatar
Kyodev committed
531
	# :vide bypass le test de contenu $2, affiche 'vide', si besoin, en liste
Kyodev's avatar
Kyodev committed
532
	# passage en paramètre VARIABLE et pas $variable
Kyodev's avatar
Kyodev committed
533
	# un test si variable $2 est vide est fait sauf ':vide'
kyodev's avatar
kyodev committed
534
	# un test si variable $2 contient 'nofile' (non trouvé par f_grep_file), remplacé par 'fichier non trouvé'
Kyodev's avatar
Kyodev committed
535
	# f_dspl "variable" "type" "titre" "commentaire"
kyodev's avatar
kyodev committed
536
f_dspl(){		# 21/02/2018
Kyodev's avatar
Kyodev committed
537
	local display='' toDisplay
Kyodev's avatar
Kyodev committed
538

Kyodev's avatar
Kyodev committed
539
	[ "$text" ] && echo -en "$text" >> "$file_output"		# flush, avant fonction, de $text parent
Kyodev's avatar
Kyodev committed
540
	unset text
Kyodev's avatar
Kyodev committed
541

kyodev's avatar
kyodev committed
542
	[[ "$2" || "$1" =~ :vide ]] || return 0		# test si contenu dans $2 ou option :vide, sinon retour
Kyodev's avatar
Kyodev committed
543
	toDisplay="$2"
Kyodev's avatar
Kyodev committed
544
	[ "$toDisplay" ] || toDisplay="vide"		# cas si :vide
Kyodev's avatar
Kyodev committed
545
		# traitement ligne de titre
Kyodev's avatar
Kyodev committed
546
	[[ "$1" =~ sans|var|cmd  ]] || display="  **⚡ erreur f_dspl \$1 ($2 $3) ⚡**  \n"	# test $1 valide
Kyodev's avatar
Kyodev committed
547 548
	[[ "$1" =~ "var" ]] && display="**$3**"		# type var, titre en gras
	[[ "$1" =~ "cmd" ]] && display="\`$3\`"		# type cmd, titre entre backtick
Kyodev's avatar
Kyodev committed
549
	[ "$4" ] && display+="$spc5( $4 )"			# +$4 en gras avec 5 espaces insécables avant
Kyodev's avatar
Kyodev committed
550 551
	[ "$1" != "sans" ] && display+="  \n"
	[[ "$1" == "sans" && "$3$4" ]] && display+="  \n"
Kyodev's avatar
Kyodev committed
552
			# traitement contenu
Kyodev's avatar
Kyodev committed
553
	if [ "$toDisplay" == "nofile" ]; then		# nofile renvoyé par f_grep
kyodev's avatar
kyodev committed
554
		display+="\n* fichier non trouvé \n\n"
kyodev's avatar
kyodev committed
555
	elif [[ "$1" =~ :text ]]; then
Kyodev's avatar
Kyodev committed
556
		display+="\n* $toDisplay \n"
Kyodev's avatar
Kyodev committed
557 558
	else
		display+='``` \n'
Kyodev's avatar
Kyodev committed
559
		display+="$toDisplay \n"
Kyodev's avatar
Kyodev committed
560
		display+='``` \n\n'
Kyodev's avatar
Kyodev committed
561
	fi
Kyodev's avatar
Kyodev committed
562
	echo -en "$display" >> "$file_output"
Kyodev's avatar
Kyodev committed
563 564
}

Kyodev's avatar
Kyodev committed
565
	# $1=variable à afficher en alerte/info, [$2 alert|info] type de message, alert par défaut
Kyodev's avatar
Kyodev committed
566
	# un test si variable $1 est vide ou non est fait
kyodev's avatar
kyodev committed
567
f_dspl_alrt(){		# 07/03/2018
Kyodev's avatar
Kyodev committed
568
	local display type
kyodev's avatar
kyodev committed
569 570

	[ "$1" ] || return 0		# test si contenu dans $1
Kyodev's avatar
Kyodev committed
571
		# flush, avant fonction, de $text parent
Kyodev's avatar
Kyodev committed
572
	[ "$text" ] && echo -en "$text\n" >> "$file_output"
Kyodev's avatar
Kyodev committed
573
	unset text
Kyodev's avatar
Kyodev committed
574

kyodev's avatar
kyodev committed
575
	type="alert"							# alert par défaut 
Kyodev's avatar
Kyodev committed
576
	[[ "$2" =~ info ]] && type="info"
kyodev's avatar
kyodev committed
577 578
	[ "$type" == "alert" ] && display="> ↯ $1  \n\n"
	[ "$type" == "info" ] && display=" ☛ $1  \n\n"
Kyodev's avatar
Kyodev committed
579
	echo -en "$display" >> "$file_output"	# flush fonction
Kyodev's avatar
Kyodev committed
580 581
}

kyodev's avatar
kyodev committed
582
	# $1=LISTE fichier(s) à grepper, [$2]: N &| novide &| notitre &| noinexist &| ligneVide &| date &| commentXY &| sources
kyodev's avatar
kyodev committed
583
	#       si aucun fichier dans la liste, retour indication: 'nofile|vide|inexistant'
kyodev's avatar
kyodev committed
584
	# exemple cumul option: "10 novide notitre lignevide" (espace obligatoire, pas d'autre séparateur)
kyodev's avatar
kyodev committed
585
	# /!\ protéger les chemins avec ", ex: f_grep_file "${toScrut[*]}". 
kyodev's avatar
kyodev committed
586 587 588 589
	# option largeur, des chiffres AU DÉBUT!
	# option 'novide', pas d'indication vide 
	# option 'notitre', pas d'énumération de fichier greppé (destiné à un seul fichier)
	# option 'noinexist', pas de titre si fichier inexistant
Kyodev's avatar
Kyodev committed
590 591
	# option 'lignevide', pas de suppression des lignes vides
	# option 'date', date de modification du fichier
kyodev's avatar
kyodev committed
592
	# option 'commentXY', A LA FIN, commentaire supplémentaire à filtrer, ex: comment//
kyodev's avatar
kyodev committed
593 594
	# option 'sources', les .sources avec largeur fixe 1ère colonne (format deb822)
	# option 'sources', affiche les fichiers !(*.sources) en colonne selon leur largeur maxi
kyodev's avatar
kyodev committed
595
	# commande de base: grep -Ersv '^#|^$' file|dossier "options"
kyodev's avatar
kyodev committed
596 597 598
f_grep_file(){		# 08/03/2018
	local motifBase="^[[:blank:]]*#" motif file content largeur un deux trois quatre cinq six sept huit reste
	local col1=0 col2=0 col3=0 col4=0 col5=0 col6=0 col7=0 col8=0 display='' 
kyodev's avatar
kyodev committed
599 600

	motif="$motifBase"
kyodev's avatar
kyodev committed
601 602 603 604 605 606
	if [[ ! "$2" =~ lignevide ]]; then
		motif+="|^[[:blank:]]*$"
	fi
	if [[ "$2" =~ comment ]]; then
		motif+="|^[[:blank:]]*${2##*comment}"		# conservation symboles de commentaires à la fin
	fi
Kyodev's avatar
Kyodev committed
607
	for file in $1; do
Kyodev's avatar
Kyodev committed
608
			# contenu
Kyodev's avatar
Kyodev committed
609
		content=$( grep -Ersv "$motif" "$file" 2>&1)
kyodev's avatar
kyodev committed
610
		if [[ "$2" =~ ^[0-9] ]]; then 	# si $2 contient des chiffres: (largeur) en tête
Kyodev's avatar
Kyodev committed
611
			largeur=${2%% *}
kyodev's avatar
kyodev committed
612
			content=$( awk -v larg="$largeur" '{ printf "%-"larg"s",$1; $1=""; print $0 }' <<< "$content" )
Kyodev's avatar
Kyodev committed
613
		fi
kyodev's avatar
kyodev committed
614 615 616 617 618
		if [[ "$2" =~ sources && "$file" =~ \.sources$ ]]; then		# nouveau format sources deb822
			content=$( grep -Ersv "$motifBase" "$file" 2>&1)		# conservation lignes vides
			content=${content#$'\n'}								# suppression éventuelle ligne vide au début
			content=$( awk '{ printf "%-12s",$1; $1=""; print $0 }' <<< "$content" )	# largeur fixe première colonne
		elif [[ "$2" =~ sources ]]; then		# fichier sources.list ou autres genre fstab
Kyodev's avatar
Kyodev committed
619
			content=$(
kyodev's avatar
kyodev committed
620
				while read -r un deux trois quatre cinq six sept huit reste; do
Kyodev's avatar
Kyodev committed
621 622 623 624
					[ ${#un} -gt "$col1" ] && col1=$(( ${#un}+1 ))
					[ ${#deux} -gt "$col2" ] && col2=$(( ${#deux}+1 ))
					[ ${#trois} -gt "$col3" ] && col3=$(( ${#trois}+1 ))
					[ ${#quatre} -gt "$col4" ] && col4=$(( ${#quatre}+1 ))
kyodev's avatar
kyodev committed
625 626 627 628
					[ ${#cinq} -gt "$col5" ] && col5=$(( ${#cinq}+1 ))
					[ ${#six} -gt "$col6" ] && col6=$(( ${#six}+1 ))
					[ ${#sept} -gt "$col7" ] && col7=$(( ${#sept}+1 ))
					[ ${#huit} -gt "$col8" ] && col8=$(( ${#huit}+1 ))
Kyodev's avatar
Kyodev committed
629
				done <<< "$content"
kyodev's avatar
kyodev committed
630 631
				while read -r un deux trois quatre cinq six sept huit reste; do
					printf "%-${col1}s %-${col2}s %-${col3}s %-${col4}s %-${col5}s %-${col6}s %-${col7}s %-${col8}s %s \n" "$un" "$deux" "$trois" "$quatre" "$cinq" "$six" "$sept" "$huit" "$reste"
Kyodev's avatar
Kyodev committed
632 633 634
				done <<< "$content"
			)
		fi
kyodev's avatar
kyodev committed
635 636 637
		if [ -d "$file" ]; then		# si répertoire, ajout / final, joli
			file+='/'
		fi
Kyodev's avatar
Kyodev committed
638 639
		content=${content//$file}			# joli, suppression de $file dans les noms de fichiers entete grep
			# mise en forme
Kyodev's avatar
Kyodev committed
640
		if [[ ! "$2" =~ notitre  ]]; then	# titre
Kyodev's avatar
Kyodev committed
641
			if [ -e "$file" ]; then
Kyodev's avatar
Kyodev committed
642
				if [[ "$2" =~ "date" ]]; then
Kyodev's avatar
Kyodev committed
643
					display+=" => $file, date de modification: $( date -r $file '+%d/%m/%Y %H:%M %z' ) \n"
Kyodev's avatar
Kyodev committed
644
				else
Kyodev's avatar
Kyodev committed
645
					display+=" => $file \n"
Kyodev's avatar
Kyodev committed
646 647 648
				fi
			elif [[ ! "$2" =~ "noinexist" ]]; then
				if touch -c $file 2>/dev/null ; then
Kyodev's avatar
Kyodev committed
649
					display+=" => $file: inexistant"$'\n\n'
Kyodev's avatar
Kyodev committed
650
				else 
Kyodev's avatar
Kyodev committed
651
					display+=" => $file: Permission non accordée"$'\n\n'
Kyodev's avatar
Kyodev committed
652
				fi
Kyodev's avatar
Kyodev committed
653 654 655 656 657 658 659
			fi
		fi
		if [ "$content" ]; then
			display+="$content"$'\n\n'
		elif [[ ! "$2" =~ novide && -e "$file" ]]; then
			display+=" ‣ vide"$'\n\n'
		fi
Kyodev's avatar
Kyodev committed
660
	done
kyodev's avatar
kyodev committed
661 662 663 664 665
	#~ display=${display%%[[:space:]]}		# suppression dernier \n
	display=${display%$'\n'}		# suppression dernier \n
	if [ -z "$display" ]; then
		display="nofile"		# si display vide, inscription 'nofile'
	fi
Kyodev's avatar
Kyodev committed
666
	echo -en "$display"
Kyodev's avatar
Kyodev committed
667 668
}

kyodev's avatar
kyodev committed
669
f_help(){		# 16/06/2018
kyodev's avatar
kyodev committed
670 671

	tput cuu1						# une ligne plus haut
kyodev's avatar
kyodev committed
672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690
	cat <<- end
		   
		  ${GREEN}./getInfo :$STD exécution script
		  ${GREEN}getInfo   :$STD exécution script installé dans le système
		  -----------------------------------------------------------------------
		  $BLUE$script  -c  :$STD (catégorie)  menu sélection catégorie d'analyse
		  $BLUE$script    -cs :$STD catégorie système     $BLUE-cs$STD : catégorie configuration
		  $BLUE$script    -cr :$STD catégorie réseau      $BLUE-ca$STD : catégorie analyse
		  $BLUE$script  -j  :$STD (journaux)  analyse démarrage système, log Xorg, kernel et système, catégorie -ca
		  $BLUE$script  -l  :$STD (list)  afficher le rapport markdown existant
		  $BLUE$script  -p  :$STD (paste) exporte le rapport markdown existant, durée standard du paste 7 jours
		  $BLUE$script  -us :$STD upgrade spécial du script en place (sans être installé)
		  $BLUE$script   --ip     :$STD affiche ip publique (ipv4/ipv6), pas de rapport markdown
		  $BLUE$script   --mac    :$STD affiche adresses Mac, pas de rapport markdown
		  $BLUE$script   --rc     :$STD gfetch, affiche un résumé, destiné à l'identification et appelé depuis .batchrc
		  $BLUE$script   --serial :$STD affiche n° série disques, batterie et châssis, pas de rapport markdown
		  $BLUE$script   --ssid   :$STD affiche configurations ssid, pas de rapport markdown,$RED root & NetworkManager$STD requis
		  
		  $GREEN    --debug :$STD messages d'erreur (stderr) logués et exportés avec le rapport
kyodev's avatar
kyodev committed
691
		  $GREEN    --dev   :$STD une version de dev du script (si existante) est recherchée
kyodev's avatar
kyodev committed
692 693 694 695 696 697 698 699
		  $GREEN     -tn    :$STD durée de conservation du paste de$GREEN n$STD jour(s)
		  -----------------------------------------------------------------------
		  $BLUE./$script -i$STD          : installation du script dans le système $RED(root)$STD
		  $BLUE$script -h$STD, --help    : affichage aide
		  $BLUE$script     --irc$STD     : installation gfetch $RED(root)$STD
		  $BLUE$script -r$STD, --remove  : désinstallation du script $RED(root)$STD
		  $BLUE$script     --rrc$STD     : désinstallation gfetch $RED(root)$STD
		  $BLUE$script -u$STD, --upgrade : mise à jour script
kyodev's avatar
kyodev committed
700
		  $BLUE$script -v$STD, --version : version du script, installée et en ligne
kyodev's avatar
kyodev committed
701 702 703 704 705 706 707
		  -----------------------------------------------------------------------
		  plus d'infos: $GREEN$url_notice$STD
		
	end
}

	# $1 [-4|-6], par défaut -4, affiche ip pub, return 1 on failure
kyodev's avatar
kyodev committed
708 709
	# requiert f__cnx & f_ip_validate & f__random & f__log & f__dig
f_ip_pub(){		# 16/06/2018-2
kyodev's avatar
kyodev committed
710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763
	local proto="-4" ip_pub base_size cmd sequence xyz server user_agent="Mozilla/5.0 Firefox"
	declare -a base cmds raw4 raw6 dns4 dns6

	{	# ip servers
		raw4=(
			v4.ident.me					# raw	ip4 only
			ipv4.whatismyip.akamai.com	# raw	ip4 only
			alma.ch/myip.cgi			# raw	ip4 only
			api.infoip.io/ip			# raw	ip4 only
			api.ipify.org				# raw	ip4 only
			myip.dnsdynamic.org			# raw	ip4 only
			ipecho.net/plain			# raw	ip4 only
			ipinfo.io/ip				# raw	ip4 only
			eth0.me						# raw	ip4 only
			# checkip.amazonaws.com		# raw 	ip4 only			lent O4/2018
			#~ ifconfig.me/ip			# raw	ip4 only			pas fiable
		)

		raw6=(
			v6.ident.me					# raw, ip6 defaut
			ipv6.whatismyip.akamai.com	# raw, ip6 defaut
			canhazip.com				# raw, ip6 defaut
			ip.tyk.nu					# raw, ip6 defaut
			myexternalip.com/raw		# raw, ip6 defaut			parfois ip4
			smart-ip.net/myip			# raw, ip6 defaut			! requiert user-agent
			diagnostic.opendns.com/myip	# raw, ip6 defaut
			icanhazip.com				# raw, ip6 defaut
			wgetip.com					# raw, ip6 defaut
			wtfismyip.com/text			# raw, ip6 defaut
			l2.io/ip					# raw, ip6 defaut
			tnx.nl/ip					# raw, ip6 defaut
			bot.whatismyipaddress.com	# raw, ip6 defaut 			pb parfois sort ip4
			#~ ipof.in/txt				# raw, ip6 defaut 			NRP
			#~ ident.me					# raw, ip6 defaut
		)

		dns4=(
			"A myip.opendns.com @resolver1.opendns.com"
			"A myip.opendns.com @resolver2.opendns.com"
			"A myip.opendns.com @resolver3.opendns.com"
			"A myip.opendns.com @resolver4.opendns.com"
			"A whoami.akamai.net @ns1-1.akamaitech.net"
		)

		dns6=(
			"AAAA myip.opendns.com  @resolver1.ipv6-sandbox.opendns.com"
			"AAAA myip.opendns.com  @resolver2.ipv6-sandbox.opendns.com"
			"TXT  o-o.myaddr.l.google.com  @ns1.google.com"
		)
	}

	[[ "$1" == '-6' || "$1" == '6' ]] && proto="-6"

	type -p host &>/dev/null && cmds+=( "host -R0 -W1 -t " )		# deprecated
kyodev's avatar
kyodev committed
764
	type -p dig &>/dev/null || type -p drill &>/dev/null && cmds+=( "f__dig nostd " )
kyodev's avatar
kyodev committed
765 766 767
	type -p wget &>/dev/null && cmds+=( "wget --user-agent=$user_agent --quiet --timeout=1 --tries=1 -o /dev/null -O - " )
	type -p curl &>/dev/null && cmds+=( "curl --silent --location --retry 0 --max-time 1 " )
	if (( ${#cmds[@]} == 0 )); then
kyodev's avatar
kyodev committed
768
		echo "f_ip_pub(): no available commands (host|dig|drill|wget|curl)" >&2
kyodev's avatar
kyodev committed
769 770 771 772
		return
	fi

	for cmd in "${cmds[@]}"; do
kyodev's avatar
kyodev committed
773
		if   [[ "$proto" == '-4' && "$cmd" =~ ^(host|f__dig) ]] ; then 
kyodev's avatar
kyodev committed
774
			base=( "${dns4[@]}" )
kyodev's avatar
kyodev committed
775
		elif [[ "$proto" == '-6' && "$cmd" =~ ^(host|f__dig) ]] ; then 
kyodev's avatar
kyodev committed
776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815
			base=( "${dns6[@]}" )
		elif [[ "$proto" == '-4' && "$cmd" =~ ^(wget|curl) ]] ; then 
			base=( "${raw4[@]}" )
		elif [[ "$proto" == '-6' && "$cmd" =~ ^(wget|curl) ]] ; then 
			base=( "${raw6[@]}" )
		fi
		base_size=$(( ${#base[@]} - 1 ))			# array index 0
		sequence=$( f__random "0-$base_size" seq )	# range: 0 to base_size
		for ii in $sequence; do
			read -r -a server <<< "${base[$ii]}"
			[[ $cmd =~ ^host ]] && server=( "${server[@]//@}" )
			ip_pub=$( $cmd "${server[@]}" )
			ip_pub=${ip_pub##*has address }			# extraction command host
			ip_pub=${ip_pub##*has IPv6 address }	# extraction command host
			ip_pub=${ip_pub#*\"}					# extraction o-o.myaddr.l.google.com
			ip_pub=${ip_pub%\"*}					# extraction o-o.myaddr.l.google.com
			f_ip_validate "$1" "$ip_pub" || unset ip_pub
			[ "$ip_pub" ] && break 2				# exit if valid ip
		done
	done
	if [ -z "$ip_pub" ]; then
		if type -t f__log &>/dev/null && ! f__cnx "$proto" ; then			# f__log() exists & no connection
			 f__log --error "f_ip_pub(): no connection"
		fi
		return 1
	fi
	echo "$ip_pub"
}

	# [-4|-6] protocole par défaut -4, IP, return 0|1|2, 1 ip invalide, 2 ipv4 privée
f_ip_validate(){		# 17/05/2018
	local proto="-4" ip reg regex regex_priv="zzz"		#ip6: https://stackoverflow.com/questions/53497/regular-expression-that-matches-valid-ipv6-addresses ()https://stackoverflow.com/a/17871737/9580455)

	while (( $# )) ; do
		case "$1" in
			-6 | 6 ) proto='-6' ;;
			-4 | 4 ) proto='-4' ;;
			* )  ip="$1" ;;
		esac
		shift 
Kyodev's avatar
Kyodev committed
816
	done
kyodev's avatar
kyodev committed
817 818 819 820 821 822 823 824 825 826 827 828 829 830

	if [[ "$proto" == "-6" ]]; then
		regex="^(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))$"
		proto="-6"
	else
		reg="(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])"  
		regex="^$reg\\.$reg\\.$reg\\.$reg$"
		regex_priv="^(127\\.$reg|10\\.$reg|172\\.(1[6-9]|2[0-9]|3[0-1])|192\\.168)\\.$reg\\.$reg$"
	fi
	if [[ "$ip" =~ $regex_priv ]]; then
		return 2
	elif [[ ! "$ip" =~ $regex ]]; then
		return 1
	fi
Kyodev's avatar
Kyodev committed
831 832
}

Kyodev's avatar
Kyodev committed
833
	# $1=texte à épurer
kyodev's avatar
kyodev committed
834
f_lifting(){		# 08/02/2018
Kyodev's avatar
Kyodev committed
835 836 837 838 839 840 841 842 843 844
	local text="$1"

	text=${text/ System Controller Hub}
	text=${text/ Advanced Micro Devices, Inc.}
	text=${text/ Electronics Co., Ltd.}
	text=${text/ Co., Ltd.}
	text=${text/ Semiconductor Co., Ltd.}
	text=${text/ Semiconductor Corp.}
	text=${text/ Series Family /Series}
	text=${text/ Series Chipset Family /Series}
kyodev's avatar
kyodev committed
845
	text=${text/ High Definition / HD }
Kyodev's avatar
Kyodev committed
846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868
	text=${text/ Semiconductor}
	text=${text/ Computer}
	text=${text/ COMPUTER}
	text=${text/ Industries}
	text=${text// Limited}
	text=${text//, Inc.}
	text=${text//, Inc}
	text=${text// Inc.}
	text=${text// INC.}
	text=${text// Corporation}
	text=${text// Corp.}
	text=${text// Co.}
	text=${text//, Ltd}
	text=${text//, Ltd.}
	text=${text// Ltd.}
	text=${text//\(R\)}
	text=${text//\(TM\)}
	text=${text//\(r\)}
	text=${text//\(tm\)}
	text=${text//  / }
	echo "$text"
}

kyodev's avatar
kyodev committed
869 870 871 872 873 874 875 876 877 878 879 880
	# $1=preferences|sources, affiche fichiers ignorés par apt
f_policy(){		# 27/01/2018
	local policy

	policy=$( LC_ALL=C apt-cache --quiet=0 policy foo 2>&1 | grep 'Ignoring file' )
	policy=$( awk -v motif="$1" '
		 $0 ~ motif { print $7$4 }
		' <<< "$policy" )
	policy=${policy//\'}
	echo "$policy"
}

Kyodev's avatar
Kyodev committed
881
	# f_prnt "1||2|3|l1|l2|l3|tit1|tit2|tit3|hl|quote|flush|code" "texte" '[CONDITION test]'
Kyodev's avatar
Kyodev committed
882
	# 	$1:	1|2|3 indentation, liste à puce, une puce par ligne
Kyodev's avatar
Kyodev committed
883
	# 		l1|l2|l3 ligne, indentation, avec espaces
Kyodev's avatar
Kyodev committed
884
	# 		tit1|tit2|tit3
Kyodev's avatar
Kyodev committed
885
	# 		quote|hl|flush
kyodev's avatar
kyodev committed
886
	# 	$2 texte à afficher, pas de contrôle si texte null ou présent
Kyodev's avatar
Kyodev committed
887 888 889
	# 	$3 test éventuel, Si CONDITION ok -> affichage
	# 	ATTENTION aux éventuelles collision $x dans ce cas:
	#	"[ \"$1\" != \"notitre\" ]"	ou	"[ $1 != notitre ]" (attention au manque de ")
Kyodev's avatar
Kyodev committed
890
	# flush (f_prnt flush) inutile si f_d(x) final
Kyodev's avatar
Kyodev committed
891
	# $1 type, $2 texte, [$3] test	/!\ assigne la variable parent $text
kyodev's avatar
kyodev committed
892
f_prnt(){		# 05/03/2018
Kyodev's avatar
Kyodev committed
893 894
	local preline='' line='' endline="  \n" 

kyodev's avatar
kyodev committed
895 896 897 898 899 900 901 902 903 904 905 906 907
	[[ "$1" =~ tit ]]   && preline="\n\n"
	[[ "$1" == tit1 ]]  && preline+="# "
	[[ "$1" == tit2 ]]  && preline+="## "
	[[ "$1" == tit3 ]]  && preline+="### "
	[[ "$1" =~ tit ]]   && endline+="\n"
	[[ "$1" == 1 ]]     && line="$( sed -E 's/(.*)/* \1  /' <<< "$2" )"
	[[ "$1" == 2 ]]     && line="$( sed -E 's/(.*)/    * \1  /' <<< "$2" )"
	[[ "$1" == 3 ]]     && line="$( sed -E 's/(.*)/        * \1  /' <<< "$2" )"
	[[ "$1" == l1 ]]    && line="$( sed -E 's/(.*)/\1/  ' <<< "$2" )"
	[[ "$1" == l2 ]]    && line="$( sed -E 's/(.*)/  \1/  ' <<< "$2" )"
	[[ "$1" == l3 ]]    && line="$( sed -E 's/(.*)/    \1/  ' <<< "$2" )"
	[[ "$1" == quote ]] && preline="> "
	[[ "$line" ]]       || line="$2"			# utilisation $2 sans traitement à faire
Kyodev's avatar
Kyodev committed
908 909

	if [[ "$3" ]]; then
kyodev's avatar
kyodev committed
910
		eval "$3" || return 0				# évaluation de la CONDITION, si erreur: sortie
Kyodev's avatar
Kyodev committed
911 912 913
	fi

	if [[ "$1" == "hl" ]]; then			# <hl>
Kyodev's avatar
Kyodev committed
914 915
		text+="\n---\n"
	elif [[ "$1" == "code" ]]; then		# ```
Kyodev's avatar
Kyodev committed
916
		text+='```\n'
Kyodev's avatar
Kyodev committed
917 918 919 920 921 922
	elif [[ "$1" ]]; then
		text+="$preline$line$endline"	# ligne formatée
	else
		text+="\n"						# newline
	fi
	if [[ "$1" == "flush" ]]; then
Kyodev's avatar
Kyodev committed
923
		echo -en "$text" >> "$file_output"
Kyodev's avatar
Kyodev committed
924 925 926 927
		unset text
	fi
}

Kyodev's avatar
Kyodev committed
928
	# $1=file à parser, [$2=marge] margin left 2 espaces
kyodev's avatar
kyodev committed
929
	# tout en bash regex par défaut non-greedy (non gourmand) comme sed ou awk
Kyodev's avatar
Kyodev committed
930 931 932
	# contrainte markdown:
	# l'italique avec _ ou * n'est pas géré, trop d'interférences potentielles
	# liste niveau2: 3 ou 4 caractères, niveau3: 6 ou 8 caractères, puce * ou -
kyodev's avatar
kyodev committed
933
f_prnt_md(){		# 08/06/2018
Kyodev's avatar
Kyodev committed
934
	local display display2 ligne margin
kyodev's avatar
kyodev committed
935

Kyodev's avatar
Kyodev committed
936 937 938 939 940
	if [ ! -f "$1" ]; then
		f__info "pas de rapport à afficher, vous devez lancer une analyse auparavant:" \
			"$GREEN$script -l$BLUE ou afficher l'aide $GREEN$script -h"
		return 0
	fi
kyodev's avatar
kyodev committed
941
	if [ "$( stat -c %s  "$1" )" -gt 100000 ]; then		# si taille en octets > 100ko pager
Kyodev's avatar
Kyodev committed
942 943 944
		pager "$1" || less "$1" || more "$1"
		return
	fi
kyodev's avatar
kyodev committed
945 946 947 948
	if [ "$2" == "marge" ]; then
		margin="  "
	fi
	display=$( < "$1" )
Kyodev's avatar
Kyodev committed
949 950
		# code
	display=${display//\`\`\`/------------}		# transforme ``` en ---, plus visibles
kyodev's avatar
kyodev committed
951 952
		# traitement par lignes, à là sed, obligatoire pour les titres #
		# plus simple pour les multi-patterns, sinon matches multilignes délicats à gérer en cas d'impairage
Kyodev's avatar
Kyodev committed
953
	while read -r ligne; do
kyodev's avatar
kyodev committed
954
		[[ "$ligne" == "------------" && "$operation" == "rc" ]] && continue
kyodev's avatar
kyodev committed
955
		ligne=${ligne//\\/\\\\}		# re echappement des antislash, utile pour efiboot
Kyodev's avatar
Kyodev committed
956
			# # TITRE 1	red
kyodev's avatar
kyodev committed
957
		[[ "$ligne" =~ ^(#[^#].*)$ ]] && ligne="\x1B[31m${BASH_REMATCH[1]}\x1B(B\x1B[m"
Kyodev's avatar
Kyodev committed
958
			# ## TITRE 2	blue
kyodev's avatar
kyodev committed
959
		[[ "$ligne" =~ ^(#{2}[^#].*)$ ]] && ligne="\x1B[34m${BASH_REMATCH[1]}\x1B(B\x1B[m"
Kyodev's avatar
Kyodev committed
960
			# ### TITRE 3	green
kyodev's avatar
kyodev committed
961
		[[ "$ligne" =~ ^(#{3}[^#].*)$ ]] && ligne="\x1B[32m${BASH_REMATCH[1]}\x1B(B\x1B[m"
Kyodev's avatar
Kyodev committed
962
			# #### TITRE 4	yellow
kyodev's avatar
kyodev committed
963
		[[ "$ligne" =~ ^(#{4}[^#].*)$ ]] && ligne="\x1B[33m${BASH_REMATCH[1]}\x1B(B\x1B[m"
Kyodev's avatar
Kyodev committed
964
			# interne message alert, red
kyodev's avatar
kyodev committed
965
		[[ "$ligne" =~ ( ↯ .*)$ ]] && ligne="\x1B[31m${BASH_REMATCH[1]}\x1B(B\x1B[m"
Kyodev's avatar
Kyodev committed
966
			# interne message indo, green
kyodev's avatar
kyodev committed
967
		[[ "$ligne" =~ ( ☛ .*)$ ]] && ligne="\x1B[32m${BASH_REMATCH[1]}\x1B(B\x1B[m"
Kyodev's avatar
Kyodev committed
968 969
			# **gras**
		while [[ "$ligne" =~ (.*)\*{2}(.*)\*{2}(.*) ]]; do
kyodev's avatar
kyodev committed
970
			ligne="${BASH_REMATCH[1]}\x1B[1m${BASH_REMATCH[2]}\x1B(B\x1B[m${BASH_REMATCH[3]}"
Kyodev's avatar
Kyodev committed
971 972
		done
			# ` backtick en italique
kyodev's avatar
kyodev committed
973 974
		regex="(.*)\`([^\`].*)\`(.*)"
		while [[ "$ligne" =~ $regex ]]; do
kyodev's avatar
kyodev committed
975
			ligne="${BASH_REMATCH[1]}\x1B[3m${BASH_REMATCH[2]}\x1B(B\x1B[m${BASH_REMATCH[3]}"
Kyodev's avatar
Kyodev committed
976 977 978 979 980
		done
			# puces niveau 1
		ligne=${ligne/#\*[^*]/• }
		ligne=${ligne/#-[^-]/• }
			# puces niveau 2
kyodev's avatar
kyodev committed
981 982
		[[ "$ligne" =~ ^([[:space:]]{3,4})\*(.*)$ ]] && ligne="${BASH_REMATCH[1]}${BASH_REMATCH[2]}"
		[[ "$ligne" =~ ^([[:space:]]{3,4})-(.*)$ ]] && ligne="${BASH_REMATCH[1]}${BASH_REMATCH[2]}"
Kyodev's avatar
Kyodev committed
983
			# puces niveau 3
kyodev's avatar
kyodev committed
984 985
		[[ "$ligne" =~ ^([[:space:]]{6,8})\*(.*)$ ]] && ligne="${BASH_REMATCH[1]}${BASH_REMATCH[2]}"
		[[ "$ligne" =~ ^([[:space:]]{6,8})-(.*)$ ]] && ligne="${BASH_REMATCH[1]}${BASH_REMATCH[2]}"
kyodev's avatar
kyodev committed
986
		display2+="$margin""$ligne"$'\n'
Kyodev's avatar
Kyodev committed
987
	done <<< "$display"
kyodev's avatar
kyodev committed
988
	echo -e "${display2%$'\n'}"
Kyodev's avatar
Kyodev committed
989 990
}

Kyodev's avatar
Kyodev committed
991
	# $1=modules [$2=repertoire spécifique de recherche], affiche emplacements et modules trouvés
kyodev's avatar
kyodev committed
992 993
f_search_ko(){		# 06/03/2018
	local dir motif emplacement imod mod_ko
Kyodev's avatar
Kyodev committed
994

kyodev's avatar
kyodev committed
995 996 997 998 999 1000 1001 1002 1003
	dir="/lib/modules/$( uname -r )"
	if [ "$1" ]; then
		motif="$1"
	else
		return 1
	fi
	if [ "$2" ]; then
		dir="$2"
	fi
Kyodev's avatar
Kyodev committed
1004 1005 1006 1007
	motif="($motif|${motif//_/-})\.ko"
	emplacement=$( du -a "$dir" | grep -Ew "$motif" )
	if [ "$emplacement" ]; then
		IFS=$'\n'
kyodev's avatar
kyodev committed
1008 1009 1010 1011
		for imod in $emplacement; do
			imod=${imod#*/}
			mod_ko+="  => /$imod"$'\n'
		done
Kyodev's avatar
Kyodev committed
1012 1013 1014 1015 1016 1017
		IFS="$IFS_INI"
		mod_ko=${mod_ko%[[:cntrl:]]}		# suppression \n final
	fi
	echo -e "$mod_ko"
}

Kyodev's avatar
Kyodev committed
1018
	# [$1=silent], assigne fe_nb_audio, fe_cards_audio
kyodev's avatar
kyodev committed
1019
fi_audio(){		# 06/03/2018
Kyodev's avatar
Kyodev committed
1020
	local cardsAudio cmd_cards version_alsa card_alsa cmd_card_alsa cmt_card_alsa pluriel text
kyodev's avatar
kyodev committed
1021
	local arecord aplay mod_alsa cmd_mod_alsa cmt_mod_alsa modAudio cmd_modAudio cmt_modAudio
Kyodev's avatar
Kyodev committed
1022
	local alert_alsa
Kyodev's avatar
Kyodev committed
1023
	x_audio=1
kyodev's avatar
kyodev committed
1024

Kyodev's avatar
Kyodev committed
1025
		# devices
kyodev's avatar
kyodev committed
1026 1027
	figet_lspci "audio" "name"
	fe_cards_audio=$( f_lifting "${lspci[name]}" )
Kyodev's avatar
Kyodev committed
1028 1029 1030
	fe_cards_audio=${fe_cards_audio% controller}
	fe_cards_audio=${fe_cards_audio% Controller}
	fe_cards_audio=${fe_cards_audio#*: }
kyodev's avatar
kyodev committed
1031
	fe_nb_audio=${lspci[nb_card]}
Kyodev's avatar
Kyodev committed
1032
	[ "$fe_nb_audio" -eq 0 ] && return 0
kyodev's avatar
kyodev committed
1033

kyodev's avatar
kyodev committed
1034
	[ "$1" == "silent" ] && return 0		# pas d'affichage
Kyodev's avatar
Kyodev committed
1035
### 
Kyodev's avatar
Kyodev committed
1036

Kyodev's avatar
Kyodev committed
1037
		# lspci
kyodev's avatar
kyodev committed
1038 1039
	figet_lspci "audio" "raw"
	cardsAudio=${lspci[card]}
kyodev's avatar
kyodev committed
1040
	cmd_cards="lspci -nnv | grep -Ei -A10 'Audio device|Audio controller'"
Kyodev's avatar
Kyodev committed
1041 1042 1043
	if [ ! -d /proc/asound ] ; then
		alert_alsa="/proc/asound : répertoire inexistant"
		alert_alsa="ALSA n'est pas installé correctement"
Kyodev's avatar
Kyodev committed
1044 1045
	else
			# ALSA
kyodev's avatar
kyodev committed
1046
		version_alsa=$( awk '{ 
Kyodev's avatar
Kyodev committed
1047 1048 1049 1050 1051
				sub(/Advanced Linux Sound Architecture Driver /,"")
				sub(/Version /,"")
				sub(/.$/,"")
				print $0
			}' /proc/asound/version )
kyodev's avatar
kyodev committed
1052
		card_alsa=$( awk ' { print "  "$0 }' /proc/asound/cards )
kyodev's avatar
kyodev committed
1053
		if [ "$( grep -c '\[.*\]' <<< "$card_alsa" )" -gt 1 ]; then pluriel="s"; else unset pluriel; fi
Kyodev's avatar
Kyodev committed
1054
		cmd_card_alsa="cat /proc/asound/cards"
kyodev's avatar
kyodev committed
1055
		cmt_card_alsa="carte$pluriel enregistée$pluriel"
kyodev's avatar
kyodev committed
1056
			# périphériques
kyodev's avatar
kyodev committed
1057 1058
		arecord=$( LC_ALL=C arecord -l 2>/dev/null | grep 'card' )
		aplay=$( LC_ALL=C aplay -l 2>/dev/null | grep 'card' )
kyodev's avatar
kyodev committed
1059 1060 1061 1062
		cmd_arecord="arecord -l | grep 'carte'"
		cmd_aplay="aplay -l | grep 'carte'"
		cmt_arecord="périphériques de capture"
		cmt_aplay="périphériques de lecture"
Kyodev's avatar
Kyodev committed
1063
			# modules alsa
kyodev's avatar
kyodev committed
1064
		mod_alsa=$( awk '{ print $2 }' /proc/asound/modules )
root's avatar
root committed
1065
		mod_alsa=$( sort <<< "$mod_alsa" )
kyodev's avatar
kyodev committed
1066
		if [ "$( f__wcv -l "$mod_alsa" )" -gt 1 ]; then pluriel="s"; else unset pluriel; fi
Kyodev's avatar
Kyodev committed
1067
		cmd_mod_alsa="cat /proc/asound/modules"
kyodev's avatar
kyodev committed
1068
		cmt_mod_alsa="module$pluriel alsa"
kyodev's avatar
kyodev committed
1069 1070 1071 1072
			# modules kernel utilisé
		figet_lspci "audio" "module"
		modAudio="${lspci[module]}"
		cmd_modAudio="lsmod | grep -Ew '${lspci[srch_mod]}'"
kyodev's avatar
kyodev committed
1073 1074
		if [ "$( f__wcv -wv "$modAudio" "^[[:alnum:]]" )" -gt 1 ]; then pluriel="s"; else unset pluriel; fi
		cmt_modAudio="$( f__wcv -wv "$modAudio" "^[[:alnum:]]" ) module$pluriel utilisé"$pluriel
Kyodev's avatar
Kyodev committed
1075 1076
	fi
### 
Kyodev's avatar
Kyodev committed
1077
	f_prnt tit2 "audio"
root's avatar
root committed
1078
	f_prnt 1    "$( sed -E 's/(.*)/**\1**   /' <<< "$fe_cards_audio" )"			# en gras
Kyodev's avatar
Kyodev committed
1079
	f_prnt
Kyodev's avatar
Kyodev committed
1080
		# lspci -nnv
Kyodev's avatar
Kyodev committed
1081
	f_dspl cmd  "$cardsAudio" "$cmd_cards"
Kyodev's avatar
Kyodev committed
1082
	f_dspl_alrt "$alert_alsa" "alert"
Kyodev's avatar
Kyodev committed
1083 1084
	f_dspl cmd  "$version_alsa" "/proc/asound/version" "driver alsa (Advanced Linux Sound Architecture)"
	f_dspl cmd  "$card_alsa" "$cmd_card_alsa" "$cmt_card_alsa"
kyodev's avatar
kyodev committed
1085 1086
	f_dspl cmd  "$arecord" "$cmd_arecord" "$cmt_arecord"
	f_dspl cmd  "$aplay" "$cmd_aplay" "$cmt_aplay"
kyodev's avatar
kyodev committed
1087
	f_dspl cmd  "$mod_alsa" "$cmd_mod_alsa" "$cmt_mod_alsa"
Kyodev's avatar
Kyodev committed
1088
	f_dspl cmd  "$modAudio" "$cmd_modAudio" "$cmt_modAudio"
Kyodev's avatar
Kyodev committed
1089 1090
}

kyodev's avatar
kyodev committed
1091
fi_batt(){		# 06/03/2018
Kyodev's avatar
Kyodev committed
1092
	local pluriel
Kyodev's avatar
Kyodev committed
1093

kyodev's avatar
kyodev committed
1094
	(( x_batt == 1 )) || figet_batt
Kyodev's avatar
Kyodev committed
1095
	[ "$fg_batt" ] || return 0		# pas de batterie
Kyodev's avatar
Kyodev committed
1096
###
kyodev's avatar
kyodev committed
1097
	if [ "$fg_nb_batt" -gt 1 ]; then pluriel="s"; else unset pluriel; fi
Kyodev's avatar
Kyodev committed
1098 1099
	f_prnt tit2  "batterie"$pluriel
	f_dspl sans  "$fg_batt"
Kyodev's avatar
Kyodev committed
1100 1101
}

Kyodev's avatar
Kyodev committed
1102
	# [$1=silent], assigne $fe_nb_bluez, $fe_cards_bluez
kyodev's avatar
kyodev committed
1103
fi_bluez(){		# 16/06/2018
Kyodev's avatar
Kyodev committed
1104
	local bluez cmd_bluez cmt_bluez mod cmd_mod cmt_mod pluriel text
Kyodev's avatar
Kyodev committed
1105
	x_bluez=1
kyodev's avatar
kyodev committed
1106

kyodev's avatar
kyodev committed
1107
	if type -p hcitool &>/dev/null ; then
kyodev's avatar
kyodev committed
1108
		fe_nb_bluez=$( hcitool dev | grep -c 'hci[[:digit:]]' )
kyodev's avatar
kyodev committed
1109 1110 1111
		fe_cards_bluez=$( hcitool dev | grep -Eo 'hci[0-9]+' )
		fe_cards_bluez=${fe_cards_bluez//$'\n'/ }
		fe_cards_bluez=${fe_cards_bluez%$'\n'}
Kyodev's avatar
Kyodev committed
1112
	else
kyodev's avatar
kyodev committed
1113 1114
		fe_nb_bluez=-99
		fe_cards_bluez="n/a"
Kyodev's avatar
Kyodev committed
1115
	fi
kyodev's avatar
kyodev committed
1116
	[[ "$1" == "silent" || "$fe_nb_bluez" -le 0 ]] && return 0
Kyodev's avatar
Kyodev committed
1117

Kyodev's avatar
Kyodev committed
1118 1119
		# état
	bluez=$( hciconfig -a )
kyodev's avatar
kyodev committed
1120
	if [ "$fe_nb_bluez" -gt 1 ]; then pluriel="s"; else unset pluriel; fi
Kyodev's avatar
Kyodev committed
1121
	cmd_bluez="hciconfig -a"
Kyodev's avatar
Kyodev committed
1122
	cmt_bluez="état périphérique"$pluriel
Kyodev's avatar
Kyodev committed
1123 1124 1125
		# modules
	figet_modules "bluetooth"
	mod="$fg_modules"								# modules chargés
Kyodev's avatar
Kyodev committed
1126
	cmd_mod="lsmod | grep -Ew '$fg_srch_mod'"
kyodev's avatar
kyodev committed
1127 1128
	if [ "$( f__wcv -l "$mod" )" -gt 1 ]; then pluriel="s"; else unset pluriel; fi
	cmt_mod="$( f__wcv -l "$mod" ) module$pluriel utilisé"$pluriel
Kyodev's avatar
Kyodev committed
1129
	fg_mod_motif=${fg_mod_motif//|/ }					# fg_mod_motif motif de recherche, avec |
Kyodev's avatar
Kyodev committed
1130
### 
Kyodev's avatar
Kyodev committed
1131 1132 1133
	f_prnt tit2 "Bluetooth"
	f_dspl cmd  "$bluez" "$cmd_bluez" "$cmt_bluez"
	f_dspl cmd  "$mod" "$cmd_mod" "$cmt_mod"
kyodev's avatar
kyodev committed
1134
	[ "$mod" ] || f_dspl  sans  "$fg_mod_motif" " " "pas de modules en mémoire parmi:"
Kyodev's avatar
Kyodev committed
1135 1136
}

kyodev's avatar
kyodev committed
1137 1138
fi_conf(){		# 21/02/2018
	local confs appArmor toScrut=(
Kyodev's avatar
Kyodev committed
1139
		'/etc/default/grub'
kyodev's avatar
kyodev committed
1140 1141
		'/etc/default/grub.d/*.cfg'
	#	'/etc/grub.d/'								# pour info
Kyodev's avatar
Kyodev committed
1142
		'/etc/sysctl.conf'
Kyodev's avatar
Kyodev committed
1143
		'/etc/sysctl.d/*.conf'
Kyodev's avatar
Kyodev committed
1144
		'/etc/hostname'								# debian
Kyodev's avatar
Kyodev committed
1145
		'/etc/sysconfig/network'					# fedora?
Kyodev's avatar
Kyodev committed
1146
		'/etc/HOSTNAME'								# suse
kyodev's avatar
kyodev committed
1147
	#	/etc/os-release								# nouveau standard systemd/freedesktop (voir figet_distrib)
Kyodev's avatar
Kyodev committed
1148
		'/etc/vconsole.conf'
kyodev's avatar
kyodev committed
1149
	#	/etc/locale.conf							# (voir fi_locale)
Kyodev's avatar
Kyodev committed
1150 1151
		'/etc/tmpfiles.d/*.conf'
		'/etc/binfmt.d/*.conf'
kyodev's avatar
kyodev committed
1152
	#	/etc/machine-id								# voir (fi_serial)
Kyodev's avatar
Kyodev committed
1153
		'/etc/modules-load.d/*.conf'				#modules to load at boot time
Kyodev's avatar
Kyodev committed
1154
		'/etc/machine-info'
Kyodev's avatar
Kyodev committed
1155
		'/etc/modprobe.d/*.conf'					# blacklist modules
Kyodev's avatar
Kyodev committed
1156
		'/etc/systemd/system/*/override.conf'		# éditions services
Kyodev's avatar
Kyodev committed
1157
		'/etc/discover-modprobe.conf'
Kyodev's avatar
Kyodev committed
1158
	)
Kyodev's avatar
Kyodev committed
1159

Kyodev's avatar
Kyodev committed
1160
	confs=$( f_grep_file "${toScrut[*]}" "noinexist" )
kyodev's avatar
kyodev committed
1161
	appArmor=$( aa-status 2>/dev/null )
Kyodev's avatar
Kyodev committed
1162
###
kyodev's avatar
kyodev committed
1163
	f_prnt tit2 "configuration, divers"
Kyodev's avatar
Kyodev committed
1164
	f_dspl cmd  "$confs" "grep -Ersv '^#|^$'  <fichiers désirés>"
kyodev's avatar
kyodev committed
1165
	f_dspl cmd  "$appArmor" "aa-status" "statut AppArmor"
Kyodev's avatar
Kyodev committed
1166 1167
}

kyodev's avatar
kyodev committed
1168
fi_cpu(){		# 08/03/2018
Kyodev's avatar
Kyodev committed
1169
	local cpu_flags text iflag qte_flags text_flags="" pluriel
Kyodev's avatar
Kyodev committed
1170 1171
	local alert_microcode

Kyodev's avatar
Kyodev committed
1172
	(( x_cpu == 1 )) 		|| figet_cpu
kyodev's avatar
kyodev committed
1173
	(( x_cpu_flags == 1 ))	|| figet_cpu_flags		# 'base' des tags
root's avatar
root committed
1174

kyodev's avatar
kyodev committed
1175
	cpu_flags=$( awk -F ': ' '/^flags/ { print $2; exit }'  /proc/cpuinfo | tr ' ' '\n' | sort | tr '\n' ' ' )		# vitesse, pas mieux
Kyodev's avatar
Kyodev committed
1176
	for iflag in $cpu_flags; do
kyodev's avatar
kyodev committed
1177
		text_flags+=$( awk -v motif="$iflag" -F '⟷' '
root's avatar
root committed
1178 1179
				BEGIN { pattern = "^"toupper(motif) }				# recherche sur majuscule
				$0 ~ pattern { $1 = motif; exit }					# $1 (flag en minuscule)
kyodev's avatar
kyodev committed
1180 1181 1182
				END { 	if( $1 != motif ) { $1 = motif; $2 = "?" } 	# si rien trouvé: ?
						printf ("%-20s %s",$1,$2 )
					}
root's avatar
root committed
1183
			'  <<< "$CPU_FLAGS" )$'\n'
Kyodev's avatar
Kyodev committed
1184
	done
kyodev's avatar
kyodev committed
1185
	text_flags=${text_flags%[[:cntrl:]]}		# suppression \n final
kyodev's avatar
kyodev committed
1186
	qte_flags=$( f__wcv -w "$cpu_flags" flags )
kyodev's avatar
kyodev committed
1187
	if ! figet_ucode ; then 					# retour fonction en erreur => pas d'installation mais possible
Kyodev's avatar
Kyodev committed
1188 1189 1190
		alert_microcode="$fg_ucode"
		[  "$ENV_DEBIAN" ] && alert_microcode+="\n    les installer: **apt install $ucode**"
	fi
kyodev's avatar
kyodev committed
1191
	if [ $(( ${fg_cpu:0:1} )) -gt 1 ]; then pluriel="s"; else unset pluriel; fi
Kyodev's avatar
Kyodev committed
1192
	unset CPU_FLAGS x_cpu_flags
Kyodev's avatar
Kyodev committed
1193
### 
Kyodev's avatar
Kyodev committed
1194 1195
	f_prnt tit2 "processeur"$pluriel
	f_dspl cmd  "$fg_cpu" "lscpu"								# affichage proc
kyodev's avatar
kyodev committed
1196
	f_dspl var  "$fg_uarch" "µ architecture processeur" 
Kyodev's avatar
Kyodev committed
1197
	f_dspl_alrt "$alert_microcode" "info"
Kyodev's avatar
Kyodev committed
1198
	f_prnt 1    "$fg_ucode" '[[ -z "$alert_microcode" && "$fg_ucode" ]]'
Kyodev's avatar
Kyodev committed
1199
	f_prnt
Kyodev's avatar
Kyodev committed
1200 1201
	f_dspl var  "$cpu_flags" "$qte_flags flags cpu"				# flags cpu bruts
	f_dspl sans "$text_flags"									# flags cpu texte
Kyodev's avatar
Kyodev committed
1202 1203
}

kyodev's avatar
kyodev committed
1204 1205 1206
# shellcheck disable=SC2086
# SC2086 Double quote to prevent globbing and word splitting
# (certaines variables ne sont pas quotées pour permettre les traitements avec liste, ex: df)
kyodev's avatar
kyodev committed
1207 1208
fi_disk(){		# 13/03/2018
	local cmd_dd_temp="" dd_temp="" liste_df disk_df disk_df_i cmd_dfi cmd_dfh disk_lsblk fstab crypttab
Kyodev's avatar
Kyodev committed
1209
	local resume idResume idSwap idisk text pluriel 
Kyodev's avatar
Kyodev committed
1210
	local alert_dd_temp alert_file_resume alert_uuidResume 
kyodev's avatar
kyodev committed
1211

Kyodev's avatar
Kyodev committed
1212
	(( x_disk == 1 )) || figet_disk
Kyodev's avatar
Kyodev committed
1213
		# hddtemp
kyodev's avatar
kyodev committed
1214
	if type -p hddtemp &>/dev/null ; then
Kyodev's avatar
Kyodev committed
1215 1216
		for idisk in $fg_disk_fixe; do
			[ -r "/dev/$idisk" ] || continue
kyodev's avatar
kyodev committed
1217
			temp=$(( $( LC_ALL=C hddtemp -n "/dev/$idisk" 2>/dev/null ) ))
Kyodev's avatar
Kyodev committed
1218
			if [ "$temp" -ge 50 ]; then
Kyodev's avatar
Kyodev committed
1219
				alert_dd_temp+="$idisk: température > 50°C  "$'\n'
Kyodev's avatar
Kyodev committed
1220
			fi
Kyodev's avatar
Kyodev committed
1221
			dd_temp+="$idisk: $temp °C"$'\n'
Kyodev's avatar
Kyodev committed
1222
			cmd_dd_temp+=" /dev/$idisk"
Kyodev's avatar
Kyodev committed
1223
		done
kyodev's avatar
kyodev committed
1224
		dd_temp=${dd_temp%$'\n'}		# suppression dernier $'\n'
Kyodev's avatar
Kyodev committed
1225
		cmd_dd_temp="hddtemp$cmd_dd_temp"
Kyodev's avatar
Kyodev committed
1226 1227
		[ "$alert_dd_temp" ] && alert_dd_temp=${alert_dd_temp::-1}		# suppression dernier $'\n'
	fi
Kyodev's avatar
Kyodev committed
1228
		# df, espaces des partitions montées seules
kyodev's avatar
kyodev committed
1229 1230
	liste_df=$( printf "/dev/%s " $fg_disk_part_fixe_m )		# $fg_disk_part_swap
	disk_df=$( df -h --total --output=source,target,fstype,size,used,avail,pcent $liste_df )
Kyodev's avatar
Kyodev committed
1231
	cmd_dfh="df -h --total --output=source,target,fstype,size,used,avail,pcent $liste_df"
kyodev's avatar
kyodev committed
1232
	disk_df=$( f__unit_french "$disk_df" )
Kyodev's avatar
Kyodev committed
1233
		# df -i, inoeuds
Kyodev's avatar
Kyodev committed
1234 1235
	disk_df_i=$( df -i $liste_df )
	cmd_dfi="df -i $liste_df"
Kyodev's avatar
Kyodev committed
1236
		# lsblk répertoire disques & partitions
Kyodev's avatar
Kyodev committed
1237
	disk_lsblk=$( lsblk -o NAME,FSTYPE,SIZE,LABEL,MOUNTPOINT,UUID )
kyodev's avatar
kyodev committed
1238
	disk_lsblk=$( f__unit_french "$disk_lsblk" )
kyodev's avatar
kyodev committed
1239
		# fstab & crypttab éventuel
kyodev's avatar
kyodev committed
1240
	fstab=$( f_grep_file "/etc/fstab" "sources notitre" )
kyodev's avatar
kyodev committed
1241
	crypttab=$( f_grep_file "/etc/crypttab" "sources notitre" )
Kyodev's avatar
Kyodev committed
1242
		# resume
Kyodev's avatar
Kyodev committed
1243
	resume=$( f_grep_file "/etc/initramfs-tools/conf.d/resume" "notitre" )
Kyodev's avatar
Kyodev committed
1244
	if [ "$resume" == "nofile" ]; then
kyodev's avatar
kyodev committed
1245