getIp 1.19.0

This commit is contained in:
kyodev 2017-12-11 22:50:13 +01:00
parent b0ec4d3925
commit f2327b7052
2 changed files with 153 additions and 127 deletions

View File

@ -1,8 +1,8 @@
#!/bin/bash
#!/bin/bash
version=1.18.0
date="10/12/2017"
version=1.19.0
date="11/12/2017"
projet="simpledeb"
contact="IRC freenode ##sdeb ou https://framagit.org/kyodev/kyopages/issues/"
script="getIp"
@ -175,15 +175,26 @@ f__user(){ # 3/12/2017
return 0
}
# $1='-l' comptage ligne dans variable $2, affiche quantité
# $1='-w' comptage dans variable $2 des mots
# $1='-wv' comptage dans variable $2, des mots $3, affiche quantité
# f__wcv -l $var ; f__wcv -w $var ; f__wcv -wv $var "mot"
f__wcv(){ # 09/11/2017
[[ "$1" =~ -l|-wv|-w ]] || display="erreur f__wcv \$1 ($1) incorrect \n"
[ "$1" == "-l" ] && echo "$2" | grep -cEv '^[[:space:]]*$' # (wc -l compterait 1 pour une variable vide)
[ "$1" == "-w" ] && echo "$(xargs <<< $2) " | grep -o " " | grep -c . # echo $(( $(grep -c .)+1 ))
[ "$1" == "-wv" ] && echo "$2" | grep -o "$3" | grep -c .
}
# test wget, $1=url à tester, sortie du script (même si url testée ok) avec affichage erreur ou ok
# 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
f__wget_test(){ # 5/12/2017
f__wget_test(){ # 11/12/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"
wget -Sq --timeout=5 --tries=2 --user-agent="$user_agent" --spider --save-headers "$1" &>"$file_test_wget"
retourWget="$?"
[ "$retourWget" == 1 ] && retourWget="1: code erreur générique"
[ "$retourWget" == 2 ] && retourWget="2: parse erreur (ligne de commande?)"
@ -193,8 +204,8 @@ f__wget_test(){ # 5/12/2017
[ "$retourWget" == 6 ] && retourWget="6: défaut authentification"
[ "$retourWget" == 7 ] && retourWget="7: erreur de protocole"
[ "$retourWget" == 8 ] && retourWget="8: réponse serveur en erreur"
retourHttp="$( grep -i 'HTTP/' "$file_test_wget" | tr -d '\n' | xargs )"
location="$( grep -i 'location' $file_test_wget | xargs )"
retourHttp=$( grep -i 'HTTP/' "$file_test_wget" | tr -d '\n' | xargs )
location=$( grep -i 'location' $file_test_wget | xargs )
if [ "$2" == "test" ]; then
rm "$file_test_wget" 2>/dev/null
# spécial maintenance frama.link, pas de redirection sur page status framalink
@ -218,8 +229,9 @@ f__wget_test(){ # 5/12/2017
fi
if [ "$retourWget" != "0" ]; then
rm "$file_test_wget" 2>/dev/null
# f__error "wget, $retourWget" "$1" "$YELLOW$retourHttp" "$location"
echo -e "$RED erreur wget, $retourWget \n $1 \n $YELLOW$retourHttp \n $location$STD"
f__error "wget, $retourWget" "$1" "$YELLOW$retourHttp" "$location"
echo -e "$RED erreur wget, $retourWget \n $1 \n $YELLOW$retourHttp \n $location$STD" # pour les diags
return 1
fi
if [ "$(grep -c '200' <<< $retourHttp)" -ne 0 ]; then
echo -e "$GREEN\ntout est ok, réessayer$STD\n"
@ -316,7 +328,7 @@ f_pr(){ # 10/12/2017
}
# assigne $fg_ip, $fg_gws6, $fg_ifn, $fg_mac
figet_ip(){ # 10/12/2017
figet_ip(){ #v 11/12/2017
local target ifn
[ $( f__which ip ) ] || return 1
@ -343,11 +355,11 @@ figet_ip(){ # 10/12/2017
# passerelles
fg_gws=$(
gawk '
BEGIN { if (proto == -6) esp=32; else esp=12 }
/default via/ { printf"%-"esp"s(%s)\n",$3,$5 }
BEGIN { if (proto == -6) esp=42; else esp=12 }
/default via/ { printf "%-"esp"s (%s)\n",$3,$5 }
' <<< $( ip $proto route )
)
# interface sortante
if [ "$proto" == "-6" ]; then
target=$( gawk '/proto ra / { print $1; exit}' <<< $( ip $proto -o route ) )
fg_ifn_prior=$( ip $proto route get $target | sed -En 's/.*dev (.*) proto.*src ([0-9a-f:]+).*/\1/p' )
@ -366,118 +378,110 @@ figet_ip(){ # 10/12/2017
fg_mac=${fg_mac%[[:cntrl:]]} # suppression dernier $'\n'
}
print_local(){
local text
figet_ip "-4" || f__error "iproute2 doit être installé" "il remplace net-tools qui n'est plus développé"
f_pr titre1 "IPv4"
f_pr l2 "$fg_ip"
f_pr
f_pr l2 "passerelles: ${fg_gws//$'\n'/ ⊗ }"
f_pr
figet_ip "-6"
f_pr titre1 "IPv6"
f_pr l2 "$fg_ip"
f_pr
f_pr l2 "interface sortante: $fg_ifn_prior" '[ "$fg_ifn_prior" ]'
f_pr
f_pr l2 "passerelles: ${fg_gws//$'\n'/ ⊗ }"
f_pr
f_pr titre1 "adresses Mac:"
f_pr l2 "$fg_mac"
echo -en "$text\n" #>> "$fileOutput"
}
# $1=4|6, assigne $fg_public
figet_ip_pub(){ # 20/11/2017
# $1=-4|-6, assigne $fg_ip_pub
figet_ip_pub(){ #v 11/12/2017
local dig_test ip_test iip
list_ip4(){
ip_test+=" http://whatismyip.akamai.com"
ip_test+=" http://ipof.in/txt"
ip_test+=" http://eth0.me"
ip_test+=" http://ipecho.net/plain"
ip_test+=" http://alma.ch/myip.cgi"
ip_test+=" http://checkip.amazonaws.com"
ip_test+=" http://eth0.me"
ip_test+=" http://ipecho.net/plain"
ip_test+=" api.infoip.io/ip" # http & https
ip_test+=" api.ipify.org" # http & https
ip_test+=" ipinfo.io/ip" # http & https
}
list_ip6(){
ip_test+=" http://ipv6.whatismyip.akamai.com"
ip_test+=" http://bot.whatismyipaddress.com"
ip_test+=" ip.tyk.nu" # http & https
ip_test+=" l2.io/ip" # http & https
ip_test+=" ident.me" # http & https
ip_test+=" icanhazip.com" # http & https
ip_test+=" wgetip.com" # http & https
ip_test+=" https://canhazip.com"
ip_test+=" https://tnx.nl/ip"
}
list_ip4_dig(){
dig_test+=" whoami.akamai.net/@ns1-1.akamaitech.net"
dig_test+=" myip.opendns.com/@resolver1.opendns.com"
dig_test+=" myip.opendns.com/@resolver2.opendns.com"
dig_test+=" myip.opendns.com/@resolver3.opendns.com"
dig_test+=" myip.opendns.com/@resolver4.opendns.com"
}
list_ip6_dig(){
dig_test+=" -6/myip.opendns.com/aaaa/@resolver1.ipv6-sandbox.opendns.com"
dig_test+=" -6/myip.opendns.com/aaaa/@resolver2.ipv6-sandbox.opendns.com"
list_ip4(){ # testé 12/2017
ip_test=( # pas de https
http://whatismyip.akamai.com
http://eth0.me
ipinfo.io/ip # http & https
http://alma.ch/myip.cgi # peu fiable curl
http://checkip.amazonaws.com # peu fiable wget
api.infoip.io/ip # http & https fiable wget
api.ipify.org # http & https fiable wget
http://ipecho.net/plain # peu fiable wget / curl
# http://ipof.in/txt # fail wget
)
}
unset fg_public
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(){ # testé 12/2017
dig_test=(
whoami.akamai.net/@ns1-1.akamaitech.net
myip.opendns.com/@resolver1.opendns.com
myip.opendns.com/@resolver2.opendns.com
myip.opendns.com/@resolver3.opendns.com
myip.opendns.com/@resolver4.opendns.com
)
}
list_ip6(){ # testé 12/2017
ip_test=(
http://ipv6.whatismyip.akamai.com
ip.tyk.nu # http & https
wgetip.com # http & https
l2.io/ip # http & https peu fiable wget
icanhazip.com # http & https peu fiable wget
http://bot.whatismyipaddress.com # peu fiable wget
https://canhazip.com # peu fiable wget
https://tnx.nl/ip # peu fiable wget
ident.me # http & https peu fiable curl
)
}
list_ip6_dig(){ # testé 12/2017
dig_test=(
-6/myip.opendns.com/aaaa/@resolver1.ipv6-sandbox.opendns.com
-6/myip.opendns.com/aaaa/@resolver2.ipv6-sandbox.opendns.com
)
}
option="$1"
unset fg_ip_pub
# assignation variables
if [ "$1" == "-4" ]; then
ping -4 -c1 conncheck.opensuse.org &>/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" == "6" ]; then
ping -6 -c1 google.com &>/dev/null || ping -6 -c1 free.fr &>/dev/null || return 1 # test connectivité
elif [ "$1" == "-6" ]; then
ping -6 -c1 conncheck.opensuse.org &>/dev/null || ping -6 -c1 free.fr &>/dev/null || return 1 # test connectivité
list_ip6_dig
list_ip6
ip_telnet=6.ifcfg.me
fi
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
# DIG
if type -p dig &>/dev/null && [ -z "$fg_ip_pub" ]; then
for iip in ${dig_test[*]}; do
fg_ip_pub=$( dig +short ${iip//\// } )
[ "$fg_ip_pub" ] && break
done
fi
if [ $( f__which wget ) ] && [ -z "$fg_public" ]; then
cmd="wget --quiet --timeout=5 -O - "
for iip in $ip_test ; do
fg_public="$($cmd $iip)"
[ "$fg_public" ] && break
# WGET
if type -p wget &>/dev/null && [ -z "$fg_ip_pub" ]; then
cmd="wget --quiet --timeout=2 --tries=1 -O - "
for iip in ${ip_test[*]}; do
fg_ip_pub=$( $cmd $iip )
[ "$fg_ip_pub" ] && break
done
fi
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)"
[ "$fg_public" ] && break
# CURL
if type -p curl &>/dev/null && [ -z "$fg_ip_pub" ]; then
cmd="curl --silent --location --retry 0 --max-time 5"
for iip in ${ip_test[*]}; do
fg_ip_pub=$( $cmd $iip )
[ "$fg_ip_pub" ] && break
done
fi
if [ $( f__which telnet ) ] && [ -z "$fg_public" ]; then
fg_public="$(telnet $ip_telnet 23 2>/dev/null | grep $1 | cut -d ' ' -f 4)"
# TELNET
if type -p telnet &>/dev/null && [ -z "$fg_ip_pub" ]; then
fg_ip_pub=$( telnet $ip_telnet 23 2>/dev/null | grep "Your IPv${option/-}" )
fg_ip_pub=${fg_ip_pub##* }
fi
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)"
# NC
if type -p nc &>/dev/null && [ -z "$fg_ip_pub" ] && [ "$option" != "-6" ]; then
fg_ip_pub=$( nc $ip_telnet 23 2>/dev/null | grep "Your IPv${option/-}" )
fg_ip_pub=${fg_ip_pub##* }
fi
if [ -z "$fg_public" ]; then
if [ -z "$fg_ip_pub" ]; then
f__error "il manque une des commandes suivantes:\n" \
"dig / wget / curl / telnet / nc \n" \
"ou les ip de test sont devenues défaillantes\n" \
"réessayer après avoir installé dig (dnsutils) et wget\n" \
"si l'erreur persiste, merci de prévenir $projet, $contact"
"réessayer après avoir installé dig (dnsutils) et wget"
fi
}
@ -618,6 +622,48 @@ fscript_update(){ # 6/12/2017
rm -rf "$dirTemp/"
}
print_local(){ #v 11/12/2017
local text
figet_ip "-4" || f__error "iproute2 doit être installé" "il remplace net-tools qui n'est plus développé"
f_pr titre1 "IPv4"
f_pr l2 "$fg_ip"
f_pr
[ "$(f__wcv "-l" "$fg_gws")" -gt 1 ] && pluriel="s" || unset pluriel
f_pr l2 "passerelle"$pluriel": ${fg_gws//$'\n'/ ⊗ }"
f_pr
figet_ip "-6"
f_pr titre1 "IPv6"
f_pr l2 "$fg_ip"
f_pr
[ "$(f__wcv "-w" "$fg_ifn_prior")" -gt 1 ] && pluriel="s" || unset pluriel
f_pr l2 "interface"$pluriel" sortante"$pluriel": $fg_ifn_prior" '[ "$fg_ifn_prior" ]'
f_pr
[ "$(f__wcv "-l" "$fg_gws")" -gt 1 ] && pluriel="s" || unset pluriel
f_pr l2 "passerelle"$pluriel": ${fg_gws//$'\n'/ ⊗ }"
f_pr
[ "$(f__wcv "-l" "$fg_mac")" -gt 1 ] && pluriel="s" || unset pluriel
f_pr titre1 "adresse"$pluriel" Mac:"
f_pr l2 "$fg_mac"
echo -en "$text\n" #>> "$fileOutput"
}
# $1=-4|-6|-46
print_public(){ #v 11/12/2017
local itest option="$1"
option=${option//46/4 6}
for itest in $option; do
itest=-${itest//-}
if figet_ip_pub "$itest" ; then
echo "$fg_ip_pub"
else
echo "pas de connectivité ipv${itest#-}"
fi
done
}
testuser(){ # 4/12/2017
f__user
retourFUser="$?"
@ -686,30 +732,13 @@ fi
for j in "$options"; do
case $j in
-4 | --ip4 )
if figet_ip_pub "4" ; then
echo "$fg_public"
else
echo "pas de connectivité ipv4"
fi
print_public "-4"
;; # affiche ip v4 public
-6 | --ip6 )
if figet_ip_pub "6" ; then
echo "$fg_public"
else
echo "pas de connectivité ipv6"
fi
print_public "-6"
;; # 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
print_public "-46"
;; # ip v4 & v6
"" | --local )
print_local

View File

@ -1,13 +1,10 @@
# changelog getIp
## todo
## getIp 1.19.0 11/12/2017
* IP si ethernet non connecté, détecter et ne pas afficher cat /sys/class/net/enp3s0/carrier 0|1
* revoir figet_ip
* éviter 169.254.10.12/16 ( enp3s0 ) (ethernet) si non branché
---
* révision: figet_ip_pub
* révision: f__wget_test
## getIp 1.18.0 10/12/2017