getIp 1.16

This commit is contained in:
kyodev 2017-12-04 13:25:57 +01:00
parent 6953ef435b
commit 1ccadc4d98
2 changed files with 192 additions and 109 deletions

View File

@ -1,7 +1,7 @@
#!/bin/bash
version=1.15.0
date="28/10/2017"
version=1.16.0
date="4/12/2017"
projet="simpledeb"
contact="IRC freenode ##sdeb ou https://framagit.org/kyodev/kyopages/issues/"
script="getIp"
@ -11,10 +11,6 @@ script="getIp"
# contributeurs: kyodev
#####
f__affichage(){ # 27/08/2017 définition couleurs
f__color
}
# $1=commande à tester, return localisation cmd si existe, return 1 si absent avec aucun message (ala debian)
# pour un test concis genre [ "$(f__cmd_exist $cmd)" ] && echo "$cmd existe"
# utilisation `type -p` pour le test, pour une bonne portabilité
@ -62,7 +58,7 @@ f__info(){ # 15/10/2017
[ "$1" == raw ] || echo
}
# log spécifique, fichier log limité à 10000octets, $1 message à loguer
# log spécifique, fichier log limité à 10000 octets, $1 message à loguer
f__log(){ # 27/10/2017
if [ -w "$fileLogs" ]; then
if [ "$(stat -c %s $fileLogs)" -ge "10000" ]; then
@ -74,52 +70,53 @@ f__log(){ # 27/10/2017
}
# 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(){ # 22/10/2017
# si manque, return 1 & info commandes manquantes, si debian proposition paquet à installer
# si $2=debOnly et si paquets manquants: return 1 et $debOnlyAbsent ( $1=liste paquets )
# si $2=debOnly et si paquets présent: return 0 et $debOnlyPresent ( $1=liste paquets )
# attention priorité $debOnlyAbsent sur $debOnlyPresent
f__requis(){ # 4/12/2017
local dependsMissing packagesMissing command package ireq compteur pluriel
unset debOnlyPackages debOnlyPresents
unset debOnlyAbsent debOnlyPresent
for ireq in $1; do
command="$(cut -d '>' -f 1 <<< $ireq)"
package="$(cut -d '>' -f 2 <<< $ireq)"
if [ "$2" == "debOnly" ]; then
if [ "$(f__cmd_exist dpkg)" ]; then # package only et debian
if type -p "dpkg" &>/dev/null ; 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"
&& debOnlyPresent+="$package " || debOnlyAbsent+="$package "
fi
elif [ -z "$(f__cmd_exist $command)" ]; then
elif ! type -p "$command" &>/dev/null ; 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
[ "$debOnlyAbsent" ] && debOnlyAbsent="$(xargs <<< $debOnlyAbsent)" # trim début & fin
[ "$debOnlyPresent" ] && debOnlyPresent="$(xargs <<< $debOnlyPresent)" # trim début & fin
[ "$debOnlyAbsent" ] && return 1
[ "$debOnlyPresent" ] && return 0
if [ "$dependsMissing" ]; then
compteur="$(wc -w <<< $dependsMissing)"
[ "$compteur" -gt "1" ] && pluriel="s" || unset pluriel
if [ -e /etc/debian_version ]; then
f__error "$compteur paquet$pluriel manquant$pluriel: $STD$BOLD$dependsMissing" \
f__info "$RED""erreur critique: $compteur paquet"$pluriel" manquant"$pluriel": $STD$BOLD$dependsMissing" \
"\n vous devriez exécuter:$GREEN apt install $packagesMissing"
else
f__error "$compteur commande$pluriel manquante$pluriel: $STD$BOLD$dependsMissing";
f__info "$RED""erreur critique: $compteur commande"$pluriel" manquante"$pluriel": $STD$BOLD$dependsMissing"
fi
return 1
fi
}
# $1=cmd si $2: nb de tentatives pour s'identifier, sinon 2 tentatives par défaut
f__sudo(){ # 22/10/2017
local nb=2 sudo isudo
# $1=cmd si $2: nb de tentatives pour s'identifier, sinon 2 tentatives par défaut, suppose bash existant
f__sudo(){ # 4/12/2017
local nb=2 sudo isudo toBash
[ $( type -p bash 2>/dev/null ) ] && toBash="$( type -p bash )" || return 2
# sudo --shell bash équivalent su ?
if sudo -v &>/dev/null && [ $EUID -ne 0 ] ; then
sudo="sudo su --shell $(f__cmd_exist bash) --preserve-environment -c "
sudo="sudo su --shell $toBash --preserve-environment -c "
else
sudo="su --shell $(f__cmd_exist bash) --preserve-environment -c "
sudo="su --shell $toBash --preserve-environment -c "
fi
[ "$2" ] && nb="$2"
for (( isudo=1 ; isudo<="$nb" ; isudo++ )); do
@ -129,44 +126,46 @@ f__sudo(){ # 22/10/2017
done
}
# user ayant initié la session graphique, assigne $user_
# 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
f__user(){ # 09/10/2017
local user_id test root_login
# gestion variable environnement user avec: USER_INSTALL=<user> script
f__user(){ # 3/12/2017
local user userid test root_login
root_login="$(grep ':0:' /etc/passwd | cut -d':' -f1)" || root_login="root"
if [ "$USER_INSTALL" ]; then # user_ via variable environnement, moyen d'injecter root
user_="$USER_INSTALL";
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"
user="$USER"
elif [ "$EUID" -eq 0 ]; then
user_="$root_login"
fu_user="$root_login"
return 0
fi
fi
if [ "$SUDO_UID" ]; then
user_id="$SUDO_UID";
userid="$SUDO_UID";
elif grep -qEo '[0-9]+' <<< "$XDG_RUNTIME_DIR" ; then
user_id="$(grep -Eo '[0-9]+' <<< $XDG_RUNTIME_DIR | cut -d'/' -f4)"
userid="$(grep -Eo '[0-9]+' <<< $XDG_RUNTIME_DIR | cut -d'/' -f4)"
elif grep -qEo '[0-9]+' <<< "$XAUTHORITY" ; then
user_id="$(grep -Eo '[0-9]+' <<< $XAUTHORITY | cut -d'/' -f4)"
userid="$(grep -Eo '[0-9]+' <<< $XAUTHORITY | cut -d'/' -f4)"
fi
[ "$user_id" ] && user_="$(grep $user_id /etc/passwd | cut -d ":" -f 1 )"
if [ "$user_" ] && [ "$user_" != "$root_login" ]; then
[ "$userid" ] && user="$(grep $userid /etc/passwd | cut -d ":" -f 1 )"
if [ "$user" ] && [ "$user" != "$root_login" ]; then
fu_user="$user"
return 0
else
if [ "$SUDO_USER" ] && [ "$SUDO_USER" != "$root_login" ]; then
user_="$SUDO_USER";
user="$SUDO_USER";
elif grep -qv 'root' <<< "$(who)"; then
user_="$(grep -v 'root' <<< $(who) | head -n1 | cut -d ' ' -f1)"; # grep -v 'root' <<< $(who) | gawk 'FNR==1{print $1}'
user="$(grep -v 'root' <<< $(who) | head -n1 | cut -d ' ' -f1)"; # grep -v 'root' <<< $(who) | gawk 'FNR==1{print $1}'
elif grep -q 'hourly.*get[A-Z].*\.anacrontab.*\.config/anacron/spool' /etc/crontab; then
user_="$(grep 'hourly.*get[A-Z].*\.anacrontab.*\.config/anacron/spool' /etc/crontab | head -n1 | cut -d' ' -f2)"; # grep 'hourly.*get[A-Z].*\.anacrontab.*\.config/anacron/spool' /etc/crontab | gawk 'FNR==1{print $2}
user="$(grep 'hourly.*get[A-Z].*\.anacrontab.*\.config/anacron/spool' /etc/crontab | head -n1 | cut -d' ' -f2)"; # grep 'hourly.*get[A-Z].*\.anacrontab.*\.config/anacron/spool' /etc/crontab | gawk 'FNR==1{print $2}
fi
fi
if [ -z "$user_" ]; then return 1; fi
if [ ! -d "/home/$user_" ]; then return 2; fi
fu_user="$user"
[ "$fu_user" ] || return 1
[ -d "/home/$fu_user" ] || return 2
return 0
}
@ -174,13 +173,11 @@ f__user(){ # 09/10/2017
# si $2=print affiche url testée & entêtes http & location (si présente) et sortie normale fonction
# si $2=loc affiche seulement location et sortie normale fonction
# si $2=test return 0 si ok, return 1 si ko
f__wget_test(){ # 17/10/2017
f__wget_test(){ # 28/11/2017
local file_test_wget retourWget retourHttp location
file_test_wget="/tmp/testWget-$$-$RANDOM"
wget -Sq --timeout=10 --user-agent="$user_agent" --spider --save-headers "$1" &>"$file_test_wget"
retourWget="$?"
[ "$2" == "test" ] && rm "$file_test_wget"
[ "$2" == "test" ] && [ "$retourWget" == "0" ] && return 0 || return 1
[ "$retourWget" == 1 ] && retourWget="code erreur générique"
[ "$retourWget" == 2 ] && retourWget="parse erreur (ligne de commande?)"
[ "$retourWget" == 3 ] && retourWget="erreur Entrée/sortie fichier"
@ -190,6 +187,12 @@ f__wget_test(){ # 17/10/2017
[ "$retourWget" == 7 ] && retourWget="erreur de protocole"
[ "$retourWget" == 8 ] && retourWget="réponse serveur en erreur"
retourHttp="$(grep -i 'HTTP/' "$file_test_wget" | tr -d '\n' | xargs)"
if [ "$2" == "test" ]; then
rm "$file_test_wget" 2>/dev/null
# spécial maintenance frama.link
[ "$(grep -c '303' <<< $retourHttp)" -ne 0 ] && return 1 # 303 See Other
[ "$retourWget" == "0" ] && return 0 || return 1
fi
location="$(grep -i 'location' $file_test_wget | xargs)"
if [ "$2" == "print" ]; then
if [ "$retourWget" ]; then
@ -206,17 +209,40 @@ f__wget_test(){ # 17/10/2017
fi
if [ "$retourWget" ]; then
rm "$file_test_wget"
f__error "erreur wget, $retourWget" "$1" "$YELLOW$retourHttp"
f__error "wget, $retourWget" "$1" "$YELLOW$retourHttp" "$location"
# echo -e "$RED errur wget, $retourWget \n $1 \n $YELLOW$retourHttp \n $location"
fi
if [ "$(grep -c '200' <<< $retourHttp)" -ne 0 ]; then
echo -e "$GREEN\ntout est ok, réessayer\n$STD"
fi
rm "$file_test_wget"
rm "$file_test_wget" 2>/dev/null
exit 0
}
f_help(){ # 28/10/2017
f__info "\n $GREEN$script $version$BLUE"
# $1=commande à tester, return localisation cmd si existe, return 1 si absent avec aucun message (à là debian)
# pour un test concis genre [ "$(f__which $cmd)" ] && echo "$cmd existe"
# utilisation `type -p` pour le test, pour une bonne portabilité
f__which(){ # 27/11/2017
if type -p "$1" &>/dev/null ; then
echo $( type -p $1 2>/dev/null ) # équivalent: command -v
else
return 1
fi
}
f_affichage(){
local affichage_text=" _ ___
__ _ ___| |_|_ _|_ __
/ _' |/ _ \ __|| || '_ \
| (_| | __/ |_ | || |_) |
\__, |\___|\__|___| .__/
|___/ |_|
"
echo -e "$GREEN$affichage_text\n $version$STD - $GREEN$date$STD"
}
f_help(){ # 4/12/2017
cat << 'EOF'
----------------------------------------------------------------------
./getIp : exécution normale
@ -227,9 +253,13 @@ cat << 'EOF'
options:
-h, --help : affichage aide
-4, --ip4 : affiche ipv4 public
-6, --ip6 : affiche ipv6 public
-46 --public : afiche ipv4 & ipv6 public
'', --local : affiche adresses mac / ip privées / passerelle
-us : upgrade spécial du script (pas de mise à jour auto, maj du script en place)
-i, --install : installation du script dans le système
@ -241,9 +271,9 @@ echo -e "$STD plus d'infos: $GREEN$urlNotice\n$STD"
}
# assigne $fg_ip, $fg_ip_tp, $fg_gws, $fg_gws_tp, $fg_ifn_prior, $fg_ifn, $fg_mac, fg_mac_tp
figet_ip(){ # 27/10/2017
figet_ip(){ # 27/11/2017
local ifn
[ "$(f__cmd_exist ip)" ] || return 1
[ $( f__which ip ) ] || return 1
fg_ip="$(sed '/[[:digit:]]:[[:blank:]]lo.*inet/d; /inet6.*scope/d' <<< $(ip -o a) | gawk '{print " ",$4,"(",$2,")"}')"
fg_ip_tp="$(sed -E 's/(^.*wl.*)/\1 (wifi)/;s/(^.*en.*|^.*eth.*)/\1 (ethernet)/' <<< $fg_ip)"
fg_gws="$(LC_ALL=C ip -4 route | gawk '/default via/ {print " ",$3,"(",$5,")"}')"
@ -252,17 +282,18 @@ figet_ip(){ # 27/10/2017
fg_ifn_prior="$(ip route get 255.255.255.255 | sed -nr 's/.*src ([0-9.]+).*/\1/p')"
for ifn in $(ls /sys/class/net/) ; do
[ "$ifn" != "lo" ] && fg_ifn+=" $ifn"$'\n'
[ "$ifn" != "lo" ] && fg_mac+=" $ifn: $(< /sys/class/net/$ifn/address)"$'\n'
[ "$ifn" != "lo" ] && fg_mac+=" $ifn: $(cat /sys/class/net/$ifn/address)"$'\n'
done
fg_ifn="$(sed '/^$/d' <<< $fg_ifn)" # suppression \n final
fg_mac="$(sed '/^$/d' <<< $fg_mac)" # suppression \n final
# fg_ifn="$(sed '/^[[:blank:]]*$/d' <<< $fg_ifn)" # suppression lignes vides
# fg_mac="$(sed '/^[[:blank:]]*$/d' <<< $fg_mac)" # suppression lignes vides
# voir cat /sys/class/net/wlp1s0/uevent
fg_mac_tp="$(sed -E 's/(^.*wl.*)/\1 (wifi)/;s/(^.*en.*|^.*eth.*)/\1 (ethernet)/' <<< $fg_mac)"
fg_ifn=${fg_ifn::-1} # suppression dernier $'\n'
fg_mac=${fg_mac::-1} # suppression dernier $'\n'
fg_ifn=${fg_ifn%[[:cntrl:]]} # suppression dernier $'\n'
fg_mac_tp=${fg_mac_tp%[[:cntrl:]]} # suppression dernier $'\n'
}
# $1=4|6, assigne $fg_public
figet_ip_pub(){ # 27/10/2017
figet_ip_pub(){ # 20/11/2017
local dig_test ip_test iip
list_ip4(){
ip_test+=" http://whatismyip.akamai.com"
@ -313,14 +344,14 @@ figet_ip_pub(){ # 27/10/2017
ip_telnet=6.ifcfg.me
fi
if [ "$(f__cmd_exist dig)" ] && [ -z "$fg_public" ]; then
if [ $( f__which dig ) ] && [ -z "$fg_public" ]; then
for iip in $dig_test ; do
fg_public="$(dig +short $(sed 's;/; ;g' <<< $iip))"
[ "$fg_public" ] && break
done
fi
if [ "$(f__cmd_exist wget)" ] && [ -z "$fg_public" ]; then
if [ $( f__which wget ) ] && [ -z "$fg_public" ]; then
cmd="wget --quiet --timeout=5 -O - "
for iip in $ip_test ; do
fg_public="$($cmd $iip)"
@ -328,7 +359,7 @@ figet_ip_pub(){ # 27/10/2017
done
fi
if [ "$(f__cmd_exist curl)" ] && [ -z "$fg_public" ]; then
if [ $( f__which curl ) ] && [ -z "$fg_public" ]; then
cmd="curl --silent --location --retry 0 --max-time 5" #--location pour aider redirections
for iip in $ip_test ; do
fg_public="$($cmd $iip)"
@ -336,11 +367,11 @@ figet_ip_pub(){ # 27/10/2017
done
fi
if [ "$(f__cmd_exist telnet)" ] && [ -z "$fg_public" ]; then
if [ $( f__which telnet ) ] && [ -z "$fg_public" ]; then
fg_public="$(telnet $ip_telnet 23 2>/dev/null | grep $1 | cut -d ' ' -f 4)"
fi
if [ "$(f__cmd_exist nc)" ] && [ -z "$fg_public" ] && [ "$1" != "IPv6" ]; then
if [ $( f__which nc ) ] && [ -z "$fg_public" ] && [ "$1" != "IPv6" ]; then
fg_public="$(nc $ip_telnet 23 2>/dev/null | grep $1 | cut -d ' ' -f 4)"
fi
@ -354,10 +385,10 @@ figet_ip_pub(){ # 27/10/2017
}
# inscription dans tache upgrade en anacron hebdomadaire, via cron horaire, $1=upgrade|install|remove
fscript_cronAnacron(){ # 17/09/2017
fscript_cronAnacron(){ # 2/12/2017
local dirAnacron dirSpool fileAnacron
[ "$(type -t fscript_cronAnacron_special)" ] && fscript_cronAnacron_special # test, si fonction spécifique, appel
dirAnacron="/home/$user_/.config/anacron"
dirAnacron="/home/$fu_user/.config/anacron"
dirSpool="$dirAnacron/spool"
fileAnacron="$dirAnacron/$script.anacrontab"
[ "$EUID" -eq 0 ] && sed -i "/$script.anacrontab/d" /etc/crontab
@ -365,17 +396,17 @@ fscript_cronAnacron(){ # 17/09/2017
install | upgrade )
mkdir -p "$dirAnacron"
# table anacron
echo "7 10 $script nice /opt/bin/$script --upgrade 1>/dev/null" > "$fileAnacron" # juste erreurs en syslog
echo "7 10 $script nice $fileInstall --upgrade 1>/dev/null" > "$fileAnacron" # juste erreurs en syslog
## anacron journalier pour dev logname
if [ -e "$fileDev" ]; then
echo "1 00 $script""Dev nice /opt/bin/$script --upgrade 1>/dev/null" >> "$fileAnacron"
echo "1 00 $script""Dev nice $fileInstall --upgrade 1>/dev/null" >> "$fileAnacron"
fi
# création spool anacron utilisateur
mkdir -p "$dirSpool"
chown -R "$user_:" "$dirAnacron" "$dirSpool"
chown -R "$fu_user:" "$dirAnacron" "$dirSpool"
if [ "$EUID" -eq 0 ]; then
# crontab pour activation horaire anacron
echo "@hourly $user_ /usr/sbin/anacron -t $fileAnacron -S $dirSpool" >> /etc/crontab
echo "@hourly $fu_user /usr/sbin/anacron -t $fileAnacron -S $dirSpool" >> /etc/crontab
fi
[ "$(grep "$script" /etc/crontab)" ] || echo f__error "inscription crontab" \
"certains systèmes semblent poser poser problème, merci de rapporter ce bug à $projet, $contact"
@ -388,25 +419,27 @@ fscript_cronAnacron(){ # 17/09/2017
esac
}
# version script en ligne, assigne $versionScript, $script_aJour=ok|ko
fscript_get_version(){ # 27/10/2017
# version script en ligne, [$1=update], assigne $versionScript, $script_aJour=ok|ko
fscript_get_version(){ # 06/11/2017
local var_temp
f__info "raw" "$GREEN""version script en cours: $version"
versionScript=$(wget -q --timeout=15 -O - "$urlScript" | grep -m1 '^version=' | cut -d'=' -f2)
if [ "$versionScript" ]; then
if [ "$version" != "$versionScript" ]; then
f__info "version script en ligne: $versionScript, mise à jour possible"
[ "$1" = "update" ] && var_temp=", mise à jour en cours"
[ "$1" = "update" ] || var_temp=", mise à jour possible"
script_aJour="ko"
else
f__info "version script en ligne: $versionScript"
script_aJour="ok"
fi
f__info "version script en ligne: $versionScript$var_temp"
else
f__info "version script en ligne$RED non accessible"
fi
}
# installation du script dans le système
fscript_install(){ # 08/10/2017
fscript_install(){ # 2/12/2017
if grep -q 'bin' <<< "$(dirname $0)" ; then
f__info "$RED""l'installation dans le système doit se faire depuis un script local $GREEN(./$script -i )"
return 1
@ -417,29 +450,29 @@ fscript_install(){ # 08/10/2017
return $?
fi
[ "$(type -t fscript_install_special)" ] && fscript_install_special # test, si fonction spécifique, appel
f__requis "wget anacron cron"
f__requis "wget anacron cron" || exit 1
# install /opt
mkdir -p /opt/bin/
cp -d "$(basename $0)" "/opt/bin/$script"
ln -s "/opt/bin/$script" "/usr/bin/$script" &>/dev/null
chmod 775 "/opt/bin/$script" # rwx rwx r-x, proprio user_
cp -d "$(basename $0)" "$fileInstall"
ln -s "$fileInstall" "/usr/bin/$script" &>/dev/null
chmod 775 "$fileInstall" # rwx rwx r-x, proprio fu_user
# cron/anacron install
fscript_cronAnacron "install"
# création fichier log
touch "$fileLogs"
chmod 664 "$fileLogs" # rw- rw- r--, proprio user_
chown "$user_:" "$fileLogs" "/opt/bin/$script"
chmod 664 "$fileLogs" # rw- rw- r--, proprio fu_user
chown "$fu_user:" "$fileLogs" "$fileInstall"
[ -e "$fileDev" ] || rm "$(basename $0)" &>/dev/null ## on efface pas si fileDev (dev)
f__info "log" "$script $version installé dans le système." "maintenant, appel du script par: $GREEN$script$BLUE (sans ./)"
}
# suppression du script dans le système
fscript_remove(){ # 08/10/2017
fscript_remove(){ # 06/11/2017
if ! grep -q 'bin' <<< "$(dirname $0)" ; then
f__info "$RED""cette fonction doit être appelée depuis le script installé dans le système $GREEN($script -r)"
return 1
fi
if [ ! -x "/opt/bin/$script" ];then
if [ ! -x "$fileInstall" ];then
f__info "$RED$script n'est pas installé"
return 1
fi
@ -450,7 +483,7 @@ fscript_remove(){ # 08/10/2017
fi
[ "$(type -t fscript_remove_special)" ] && fscript_remove_special # test, si fonction spécifique, appel
# suppression de /opt
rm "/opt/bin/$script" &>/dev/null
rm "$fileInstall" &>/dev/null
unlink "/usr/bin/$script" &>/dev/null
# cron/anacron remove
fscript_cronAnacron "remove"
@ -458,15 +491,14 @@ fscript_remove(){ # 08/10/2017
}
# mise à jour script si dispo, v2, +update spécifique
# à tester avant généraliser fileInstall="/opt/bin/$script" ?
fscript_update(){ # 27/10/2017
fscript_update(){ # 2/11/2017
local dirTemp="/tmp/$script-$RANDOM"
[ $(type -t fscript_update_special) ] && fscript_update_special # test, si fonction spécifique, appel
if [ -z "$updateSpecial" ] && ! grep -q 'bin' <<< "$(dirname $0)" ; then
f__info "$RED""cette fonction doit être appelée depuis le script installé dans le système $GREEN($script -u)"
return 1
fi
fscript_get_version
fscript_get_version "update"
if [ "$script_aJour" == "ok" ]; then
f__info "log" "pas de mise à jour disponible pour $script $version"
return 0
@ -474,38 +506,66 @@ fscript_update(){ # 27/10/2017
mkdir -p "$dirTemp"
wget -q --tries=2 --timeout=15 -O "$dirTemp/$script" "$urlScript"
if [ "$?" != "0" ]; then f__wget_test "$urlScript"; fi
cp -d "$dirTemp/$script" "$fileInstall"
if grep -q '#!/bin/bash' "$dirTemp/$script" ; then
cp -d "$dirTemp/$script" "$fileInstall"
chmod 775 "$fileInstall" # rwx rwx r-x, proprio fu_user
chown "$fu_user:" "$fileInstall"
[ -z "$updateSpecial" ] && fscript_cronAnacron "upgrade"
f__info "log" "$script mis à jour en version $versionScript $updateSpecial"
else
rm -rf "$dirTemp/"
f__error "échec update" "mauvais téléchargement, réessayer plus tard"
fi
rm -rf "$dirTemp/"
chmod 775 "$fileInstall" # rwx rwx r-x, proprio user_
chown "$user_:" "$fileInstall"
[ -z "$updateSpecial" ] && fscript_cronAnacron "upgrade"
f__info "log" "$script mis à jour en version $versionScript $updateSpecial"
}
testuser(){ # 06/10/2017
testuser(){ # 4/12/2017
f__user
retourFUser="$?"
[ "$retourFUser" -eq 1 ] && f__error "user indéterminé" \
"pour contourner, lancer le script avec:\n$GREEN USER_INSTALL=<user> $0 \n"
if [ "$retourFUser" -eq 2 ]; then
[ "$EUID" -eq 0 ] && user_="root" || f__error "user détecté, mais pas de home: /home/$user_"
[ "$EUID" -eq 0 ] && fu_user="root" || f__error "user détecté, mais pas de home: /home/$fu_user"
f__info "user root"
fi
}
######## début script / initialisation
PATH='/usr/sbin:/usr/bin:/sbin:/bin'; TERM=xterm; IFS=$' \t\n'
export PATH TERM IFS
prg_init(){ # 2/12/2017
PATH='/usr/sbin:/usr/bin:/sbin:/bin'
TERM=xterm
IFS_INI="$IFS"
IFS=$' \t\n'
export PATH TERM IFS
# whereis script retourne vide si installé
DIRNAME=$( dirname $0 )
DIRNAME=${DIRNAME#/usr/bin} # suppression /usr/bin éventuel au début ( lien )
DIRNAME=${DIRNAME#/opt/bin} # suppression /opt/bin éventuel au début ( install )
[ "$DIRNAME" ] && DIRNAME+="/"
# logo et définition couleurs
f__affichage
# test bash v4
[ "$BASH_VERSINFO" == 4 ] || f__error "bash v4 requis" "version installée: $BASH_VERSION"
# détermination user derrière root
f__user
retourFUser="$?"
[ "$retourFUser" -eq 1 ] && f__error "user indéterminé" \
"pour contourner, lancer le script avec:\n$GREEN USER_INSTALL=<user> $0 \n"
if [ "$retourFUser" -eq 2 ]; then
[ "$EUID" -eq 0 ] && fu_user="root" || f__error "user détecté, mais pas de home: /home/$fu_user"
f__info "user root"
fi
# requis pour fonctionnement programme
f__requis "gawk wget ip>iproute2"
}
######## début script / initialisation
# définition couleurs
f__color
# tests au démarrage
bashVersion=($(grep -o 'version 4' <<< $(bash --version)))
[ ${bashVersion[1]} -ge 4 ] || f__error "bash v4 requis" "version installée: $(sed -n '1p' <<< $(bash --version))"
# requis pour fonctionnement programme
f__requis "gawk wget ip>iproute2"
prg_init
# paramètres généraux
fileLogs="/var/log/sdeb_$script.log"
@ -519,6 +579,9 @@ options="$@"
if [[ "$options" =~ -i|-r|-u|--install|--remove|--update|-us ]]; then
testuser
fi
if [[ ! "$options" =~ -46|-4|-6|--public ]]; then
f_affichage
fi
for j in "$options"; do
case $j in
-4 | --ip4 )
@ -535,6 +598,18 @@ for j in "$options"; do
echo "pas de connectivité ipv6"
fi
;; # affiche ip v6 public
-46 | --public )
if figet_ip_pub "4" ; then
echo "$fg_public"
else
echo "pas de connectivité ipv4"
fi
if figet_ip_pub "6" ; then
echo "$fg_public"
else
echo "pas de connectivité ipv6"
fi
;; # ip v4 & v6
'' | --local )
figet_ip || f__error "iproute2 doit être installé" "il remplace net-tools qui n'est plus développé"
echo -e "\n ip locale(s):\n$BLUE$fg_ip_tp\n$STD"
@ -565,7 +640,7 @@ for j in "$options"; do
;; # affichage help
esac
done
unset j
exit 0
wget -nv -O getIp https://frama.link/getip

View File

@ -10,9 +10,17 @@
---
## 1.16 04/12/2017
* nouveau: option - ( ou -46 ), ipv4 & ipv6
* nouveau: prg_init
* révision: syncro fonctions communes
* révision: renommage $user_
## 1.15.0 28/10/2017
* nouveau: option -us
* nouveau: option -us (maj an place)
## 1.14.0 27/10/2017