This commit is contained in:
kyodev 2017-09-03 09:39:12 +02:00
parent 329eb52df8
commit e9b8c12a24
2 changed files with 43 additions and 29 deletions

View File

@ -1,7 +1,7 @@
#!/bin/bash
version=1.5.1
date="01/09/2017"
version=1.6.0
date="03/09/2017"
projet="simpledeb"
contact="IRC freenode ##sdeb ou https://framagit.org/kyodev/kyopages/issues/"
script="getIp"
@ -18,7 +18,7 @@ f__affichage(){ # v27/08/2017 définition couleurs
BLUE="$(printf "\033[0;34m")" # info
RED="$(printf "\033[0;31m")" # alerte
COLOR="$(printf "\033[0m")" # standard
echo
# echo
}
# affichage $1 en rouge, $1++ optionnels en bleu, sortie script sur erreur, log $1 si $opType=upgrade
@ -151,8 +151,8 @@ f__wget_test(){ # v31/08/2017
exit 0
}
f_help(){
f__info "$GREEN$script $version$BLUE"
f_help(){ # v03/09/2017
f__info "\n $GREEN$script $version$BLUE"
cat << 'EOF'
----------------------------------------------------------------------
./getIp : exécution normale
@ -165,7 +165,7 @@ cat << 'EOF'
-h, --help : affichage aide
-4, --ip4 : affiche ipv4 public
-6, --ip6 : affiche ipv6 public
"", --local : affiche ip / passerelle privées
'', --local : affiche ip / passerelle privées
-i, --install : installation du script dans le système
-r, --remove : suppression du script dans le système
@ -175,18 +175,20 @@ EOF
echo "$COLOR"
}
fgi_ip() { # $ip_local, $gateway, $gateway_type, $gateway_one, $ip_local, $ip_local_type, $ifname, $ifname_type
# $ip_local, $gateway, $gateway_type, $gateway_one, $ip_local, $ip_local_type, $ifname, $ifname_type
figet_ip() { # v03/09/2017
[ -z "$(which ip)" ] && return 1
ip_local="$(sed '/lo.*inet/d; /inet6.*scope global/d' <<< "$(ip -o a)" | awk '{print $4,"(",$2,")"}')"
ip_local="$(sed '/lo.*inet/d; /inet6.*scope global/d' <<< "$(ip -o a)" | awk '{print " ",$4,"(",$2,")"}')"
ip_local_type="$(sed -E 's/(^.*wl.*)/\1 (wifi)/;s/(^.*en.*|^.*eth.*)/\1 (ethernet)/' <<< $ip_local)"
gateway="$(LC_ALL=C ip -4 route | awk '/default via/ {print $3,"(",$5,")"}'; LC_ALL=C ip -6 route | awk '/default via/ {print $3,"(",$5,")"}')"
gateway_type="$(sed -E 's/(^.*wl.*)/\1 (wifi)/;s/(^.*en.*|^.*eth.µ)/\1 (ethernet)/' <<< $gateway)"
gateway_one="$(head -n 1 <<< $gateway | awk '{print $1}')"
ifname="$(ip -4 route | awk '/proto kernel/ {print $3}')"
gateway="$(LC_ALL=C ip -4 route | awk '/default via/ {print " ",$3,"(",$5,")"}'; LC_ALL=C ip -6 route | awk '/default via/ {print " ",$3,"(",$5,")"}')"
gateway_type="$(sed -E 's/(^.*wl.*)/\1 (wifi)/;s/(^.*en.*|^.*eth.*)/\1 (ethernet)/' <<< $gateway)"
gateway_one="$(head -n 1 <<< $gateway | awk '{print " ",$1}')"
ifname="$(ip -4 route | awk '/proto kernel/ {print " ",$3}')"
ifname_type="$(sed -E 's/(^wl.*)/\1 (wifi)/;s/(^en.*|^eth.*)/\1 (ethernet)/' <<< $ifname)"
}
fgi_ip_public(){ # $1=IPv4|IPv6, assigne $ip_public # v18/08/2017
# $1=4|6, assigne $ip_public
figet_ip_public(){ # v03/09/2017
list_ip4(){
ip_test+=" http://whatismyip.akamai.com"
@ -226,15 +228,17 @@ fgi_ip_public(){ # $1=IPv4|IPv6, assigne $ip_public # v18/08/2017
unset ip_public
local dig_test ip_test
if [ "$1" == "IPv4" ]; then
if [ "$1" == "4" ]; then
ping -4 -c1 google.com &>/dev/null || ping -4 -c1 free.fr &>/dev/null || return 1 # test connectivité
list_ip4_dig
list_ip4
ip_telnet=4.ifcfg.me
elif [ "$1" == "IPv6" ]; then
elif [ "$1" == "6" ]; then
ping -6 -c1 google.com &>/dev/null || ping -6 -c1 free.fr &>/dev/null || return 1 # test connectivité
list_ip6_dig
list_ip6
ip_telnet=6.ifcfg.me
else return 1; fi
fi
if [ "$(which dig)" ] && [ -z "$ip_public" ]; then
for ii in $dig_test ; do
@ -416,18 +420,24 @@ f__requis "gawk wget ip>iproute2" # requis pour fonctionnement programme
options="$@"
for i in "$options"; do
case $i in
--ip4 | -4 )
fgi_ip_public "IPv4"
echo "$ip_public"
-4 | --ip4 )
if figet_ip_public "4" ; then
echo "$ip_public"
else
echo "pas de connectivité ipv4"
fi
;; # affiche ip v4 public
--ip6 | -6 )
fgi_ip_public "IPv6"
echo "$ip_public"
-6 | --ip6 )
if figet_ip_public "6" ; then
echo "$ip_public"
else
echo "pas de connectivité ipv6"
fi
;; # affiche ip v6 public
--local | "" )
fgi_ip || f__error "iproute2 doit être installé" "il remplace net-tools qui n'est plus développé"
echo -e "\tip locale(s):\n$ip_local_type"
echo -e "\tpasserelle(s):\n$gateway_type"
'' | --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$ip_local_type\n$COLOR"
echo -e " passerelle(s):\n$BLUE$gateway_type\n$COLOR"
;; # affiche ip locales
-i | --install )
testuser
@ -441,7 +451,7 @@ for i in "$options"; do
testuser
opType="upgrade"
fscript_update || f_help
exit ;; # upgrade script si maj possible
exit ;; # upgrade script si maj possible
-v | --version )
fscript_get_version
;; # version du script, en ligne et exécuté
@ -455,4 +465,3 @@ exit 0
wget -O getIp https://framagit.org/kyodev/kyopages/raw/master/scripts/getIp
chmod +x getIp && ./getIp

View File

@ -1,11 +1,16 @@
# changelog getIp
## 1.6.0 03/09/2017
* mieux: test connectivité avant recherche ip public pour éviter timeout, figet_ip_public
* révision figet_ip: 2 espaces préliminaires pour meilleure présentation
## 1.5.1 01/09/2017
* révision mineure f__wget_test
## 30/08/2017
## 1.5.0 30/08/2017
* révision f__requis, f__user, f__wget_test, fscript_cronAnacron
* révision déclaration local