This commit is contained in:
kyodev 2017-08-18 20:14:39 +02:00
parent 879b624af5
commit 616e430934
2 changed files with 60 additions and 18 deletions

View File

@ -1,6 +1,6 @@
#!/bin/bash
version=0.7.4
version=0.7.5
date="18/08/2017"
projet="simpledeb"
contact="IRC freenode ##sdeb ou https://framagit.org/kyodev/kyopages/issues"
@ -43,6 +43,38 @@ f__log(){ # v08/2017
fi
}
# test dépendances/paquets, $1 liste commande[>paquet] (ex: killall>psmisc)
# si manque, SORTIE & info commandes manquantes, si debian, SORTIE & proposition paquet à installer
# si $2=debOnly et si paquets manquants: return 1 et $debOnlyPackages ( $1=liste paquets )
# si $2=debOnly et si paquets présent: return 0 et $debOnlyPresents ( $1=liste paquets )
# attention priorité $debOnlyPackages sur $debOnlyPresents
f__requis(){ # v17/08/2017-3
local dependsMissing packagesMissing
unset debOnlyPackages debOnlyPresents
for i in $1; do
local command="$(cut -d '>' -f 1 <<< $i)"
local package="$(cut -d '>' -f 2 <<< $i)"
if [ "$2" == "debOnly" ]; then
if [ "$(which dpkg)" ]; then # package only et debian
LC_ALL=C dpkg --get-selections | grep -qE "^$package[[:space:]]+install" \
&& debOnlyPresents+="$package " || debOnlyPackages+="$package "
else f__error "dpkg n'est pas disponible sur ce système"; fi
elif [ -z "$(which $command)" ]; then
dependsMissing+="$command "
packagesMissing+="$package "
fi
done
[ "$debOnlyPackages" ] && debOnlyPackages="$(xargs <<< $debOnlyPackages)" # trim début & fin
[ "$debOnlyPresents" ] && debOnlyPresents="$(xargs <<< $debOnlyPresents)" # trim début & fin
[ "$debOnlyPackages" ] && return 1
[ "$debOnlyPresents" ] && return 0
if [ "$dependsMissing" ]; then
if [ -e /etc/debian_version ]; then f__error "paquet(s) manquant(s): " " $dependsMissing" \
" vous devriez exécuter:$GREEN apt install $packagesMissing"
else f__error "commandes(s) manquante(s): " "$dependsMissing"; fi
fi
}
# user ayant initié la session graphique, assigne $user_, testé avec mate, gnome, xfce. manque KDE
# return 1 sur echec ident user, return 2 sur absence home
# gestion variable environnement user avec: USER_INSTALL=user script
@ -69,6 +101,13 @@ f__user(){ # v17/08/2017-4
fi
}
# test wget, $1 url à tester, sortie script sur erreur
f__wget_test(){ # v07/2017
echo " $1"
wget -Sq --tries=1 --timeout=15 --user-agent="$user_agent" -O /dev/null "$1"
f__error "Erreur wget" " réessayer et si persistance, contacter $projet $contact" " en indiquant les messages précédents."
}
# inscription dans tache upgrade en anacron hebdomadaire, via cron horaire
fscript_cronAnacron(){ # v18/08/2017-2
local dirAnacron="/home/$user_/.config/anacron"
@ -81,7 +120,7 @@ fscript_cronAnacron(){ # v18/08/2017-2
# table anacron
echo "7 01 $script nice /opt/bin/$script --upgrade" > "$fileAnacron"
## dev: anacron journalier:
[ "$user_" == "$lognameDev" ] && echo "1 01 $script""test nice /opt/bin/$script --upgrade 1>/dev/null" >> "$fileAnacron"
[ "$user_" == "$lognameDev" ] && echo "1 01 $script""Dev nice /opt/bin/$script --upgrade 1>/dev/null" >> "$fileAnacron"
# création spool anacron utilisateur
mkdir -p "$dirSpool"
chown -R "$user_": "$dirAnacron" "$dirSpool"
@ -103,11 +142,11 @@ fscript_cronAnacron(){ # v18/08/2017-2
}
# mise à jour script si dispo
fscript_dl(){ # v17/08/2017
fscript_dl(){ # v18/08/2017
f__requis "wget"
if [ "$script_aJour" == "ok" ]; then return 0; fi
local dirTemp="/tmp/$script-$RANDOM/"
mkdir -p "$dirTemp"
opType="upgrade"
wget -q --show-progress --tries=2 --timeout=15 -O "$dirTemp$script" "$urlScript"
if [ "$?" != "0" ]; then f__wget_test "$urlScript"; fi
chmod 755 "$dirTemp$script"
@ -121,10 +160,9 @@ fscript_dl(){ # v17/08/2017
}
# version script en ligne, assigne $versionScript, $script_aJour=ok|ko
fscript_get_version(){ # v08/2017
if ! which wget &>/dev/null; then f__info "wget manquant"; return 1; fi #spécifique getIp
fscript_get_version(){ # v18/08/2017
f__requis "wget"
versionScript="$(wget -q --tries=2 --timeout=15 -O - "$urlScript" | grep '^version=' | cut -d '=' -f 2)"
if [ "$?" != "0" ]; then f__error "erreur chargement, réessayer plus tard"; fi #spécifique getIp
if [ "$versionScript" ]; then
if [ "$version" != "$versionScript" ]; then
f__info "version script en ligne: $versionScript, mise à jour possible"
@ -137,13 +175,13 @@ fscript_get_version(){ # v08/2017
}
# installation du script dans le système
fscript_install(){ # v08/2017
if ! which wget &>/dev/null; then f__error "wget manquant pour exploiter ce script"; fi #spécifique getIp
fscript_install(){ # v18/08/2017
if dirname "$0" | grep -q 'bin' ; then
f__info "$script $version est déjà installé"
return 1
fi
[ $EUID -eq 0 ] || f__error "vous devez être ROOT pour installer ce script dans le système"
f__requis "wget anacron cron"
# install /opt
mkdir -p /opt/bin/
cp -d "$(basename $0)" "/opt/bin/$script"
@ -208,7 +246,7 @@ get_ip() { # $ip_local, $gateway, $gateway_type, $gateway_one, $ip_local, $ip_lo
ifname_type="$(sed -E 's/(^wl.*)/\1 (wifi)/;s/(^en.*|^eth.*)/\1 (ethernet)/' <<< $ifname)"
}
get_ip_public(){ # $1=IPv4|IPv6, assigne $ip_public
get_ip_public(){ # $1=IPv4|IPv6, assigne $ip_public # v18/08/2017
list_ip4(){
ip_test+=" http://whatismyip.akamai.com"
@ -259,14 +297,14 @@ get_ip_public(){ # $1=IPv4|IPv6, assigne $ip_public
ip_telnet=6.ifcfg.me
else return 1; fi
if which dig &>/dev/null && [ -z "$ip_public" ]; then
if [ "$(which dig)" ] && [ -z "$ip_public" ]; then
for ii in $dig_test ; do
ip_public="$(dig +short $(sed 's;/; ;g' <<< $ii))"
[ "$ip_public" ] && break
done
fi
if which wget &>/dev/null && [ -z "$ip_public" ]; then
if [ "$(which wget)" ] && [ -z "$ip_public" ]; then
cmd="wget --quiet --tries=1 --timeout=5 -O - "
for ii in $ip_test ; do
ip_public="$($cmd $ii)"
@ -274,7 +312,7 @@ get_ip_public(){ # $1=IPv4|IPv6, assigne $ip_public
done
fi
if which curl &>/dev/null && [ -z "$ip_public" ]; then
if [ "$(which curl)" ] && [ -z "$ip_public" ]; then
cmd="curl --silent --location --retry 0 --max-time 5" #--location pour aider redirections
for ii in $ip_test ; do
ip_public="$($cmd $ii)"
@ -282,11 +320,11 @@ get_ip_public(){ # $1=IPv4|IPv6, assigne $ip_public
done
fi
if which telnet &>/dev/null && [ -z "$ip_public" ]; then
if [ "$(which telnet)" ] && [ -z "$ip_public" ]; then
ip_public="$(telnet $ip_telnet 23 2>/dev/null | grep $1 | cut -d ' ' -f 4)"
fi
if which nc &>/dev/null && [ -z "$ip_public" ] && [ "$1" != "IPv6" ]; then
if [ "$(which nc)" ] && [ -z "$ip_public" ] && [ "$1" != "IPv6" ]; then
ip_public="$(nc $ip_telnet 23 2>/dev/null | grep $1 | cut -d ' ' -f 4)"
fi
@ -308,6 +346,7 @@ PATH='/usr/sbin:/usr/bin:/sbin:/bin' ; IFS=' '
COLOR="$(printf "\033[0m")" # standard
fileLogs="/var/log/sdeb_$script.log"
f__requis "gawk wget ip>iproute2" # pour fonctionnement programme
options="$@"
@ -316,8 +355,8 @@ for i in "$options"; do
--install | -i ) f__user && fscript_install ;; # installation du script dans le système
--remove | -r ) f__user && fscript_remove ;; # suppression du script dans le système
--upgrade | -u )
opType="upgrade"
f__user || f__error "user indéterminé ou home inexistant"
opType="upgrade"
fscript_get_version
fscript_dl ;; # upgrade script si maj possible
--ip4 | -4 )

View File

@ -2,12 +2,15 @@
## 0.7.4 18/08/2017
## 0.7.5 18/08/2017
* maj fscript_cronAnacron lors upgrade et spécial pour dev
* révision ip locales
* protection absence iproute
*
* révision test sur which
* + f__requis
* vérification requis pour fonctionnement script
* maj fscript_get_version, fscript_dl, fscript_install
## 0.7.1 17/08/2017