#!/bin/bash # test connexion, return 0|1, 0 si ip valide, si $1=log : log si perte cnx, requiert $histo_cnx (fichier historique) f__cnx(){ # 19/04/2018 #~ if ! ping -c1 -w1 8.8.8.8 &>/dev/null; then if ! nc -z -w1 8.8.8.8 53 2>/dev/null ; then #~ [ "$1" == 'log' ] && f__log "cnx down" "$histo_cnx" return 1 else return 0 fi } # $1=bas-haut (ex: 0-5), nombre aléatoire entre bas & haut inclus, si [$2=seq] liste avec départ aléatoire f__random(){ # 25/04/2018 2 local bas haut max rand start 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 #~ while (( (start=RANDOM) >= max )); do #~ : #~ done 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) ) )) } # [-4]|[-6], par défaut -4, return 0|1, 0 si ip valide f__validate_ip(){ # 21/04/2018 local regex #ip6: https://stackoverflow.com/questions/53497/regular-expression-that-matches-valid-ipv6-addresses ()https://stackoverflow.com/a/17871737/9580455) if [[ "$1" == "-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]))$" else regex="(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])" regex="^$regex\\.$regex\\.$regex\\.$regex$" fi [[ "$1" =~ ^- ]] && shift #~ [[ "$1" =~ $regex ]] && echo -n ok || echo -n " KO" ; echo "-$1-" [[ "$1" =~ $regex ]] && return 0 || return 1 } # $1 [-4]|-6, par défaut -4, affiche public ip, requiert f__validate_ip f_ip_pub_diag(){ # 02/05/2018 local proto larg ip_pub cmd server user_agent="Mozilla/5.0 Firefox" declare -a base cmds if [ "$1" == '-6' ] ; then proto="-6" larg=40 else proto="-4" larg=18 fi f__cnx 'log' || return 1 # no connnection, return with error type -p host &>/dev/null && cmds+=( "host -R0 -W1 -t " ) # deprecated type -p dig &>/dev/null && cmds+=( "dig +short +timeout=1 +retry=1 " ) 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 " ) for cmd in "${cmds[@]}"; do if [[ "$proto" == '-4' && "$cmd" =~ ^(host|dig) ]] ; then base=( "${dns4[@]}" ) elif [[ "$proto" == '-6' && "$cmd" =~ ^(host|dig) ]] ; then base=( "${dns6[@]}" ) elif [[ "$proto" == '-4' && "$cmd" =~ ^(wget|curl) ]] ; then base=( "${raw4[@]}" ) elif [[ "$proto" == '-6' && "$cmd" =~ ^(wget|curl) ]] ; then base=( "${raw6[@]}" ) fi for server in "${base[@]}"; do printf "%-28s: " "${server#*@}" getTime start [[ "$cmd" =~ ^host ]] && server="${server//@}" # host # shellcheck disable=SC2086 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 printf "%-${larg}s " "$( cat -A <<< "${ip_pub:0:$larg}" )" #~ printf "%-${larg}s" "$ip_pub" f__validate_ip "$1" "$ip_pub" || printf "%s " invalid getTime end done echo "(${cmd%% *})" sleep 10 done } # getTime start : comptage départ # getTime end : comptage fin, calcul fin - départ, affichage différence en ms # getTime end:sec : idem sauf affichage différence en s # getTime affichage date en ms, sans unité, pour traitement externe à la fonction # declare -g time getTime(){ # 21/04/2018 local date sec ms time2 declare -g time date=$( date +%s.%N ) # s.ns sec=${date%\.*} # s ms=${date#*\.} # ns ms=${ms::-6} # ms if [[ "$*" =~ "start" ]]; then time="$sec$ms" elif [[ "$*" =~ "end" ]]; then if [[ "$*" =~ "sec" ]]; then time2=$(( $sec$ms-time )) echo "${time2::-3}.${time2: -3} s" else echo "$(( $sec$ms-time )) ms" fi else echo "$sec$ms" # ms sans unité fi } { # serveur ip 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 & aléatoire O4/2018 #~ ifconfig.me/ip # raw ip4 only pas fiable? http://ifconfig.me/ infoS ) 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" ) } f_ip_pub_diag -4 f_ip_pub_diag -6 { # inutilisé servers_list=( #~ https://major.io/icanhazip-com-faq/ check.torproject.org # tor checkip.dyndns.com # 1/2 raw ip4 only Current IP Address: 88.191.35.123 checkip.dyndns.org/plain # 1/2 raw ip4 only Current IP Address: 88.191.35.123 bobborst.com/tools/whatsmyip # canyouseeme.org # checkmyip.com # checkip.dns.he.net # 1/2 raw: Your IP address is : 2a01:e35:8bf2:37b0::d7c5:972f checkip.dynu.com # 1/2 raw: Current IP Address: 88.191.35.123 ipcheckv6.dynu.com displaymyip.com # dslreports.com/whatismyip # findmyip.co # formyip.com # geoiptool.com # getmyipaddress.org # httpbin.org/ip # 1/2 raw ip4 only, json infosniper.net # ip-adress.com # ip-adress.eu # ip.changeip.com # 1/2 raw ip4 only ip_pub=${ip_pub##*IPADDR=} ip_pub=${ip_pub%%-->*} # extraction ip.changeip.com ipchicken.com # ipgoat.com # iplocation.net # ip-lookup.net # ip6 defaut, lawrencegoetz.com/programs/ipinfo # mon-ip.com # locale monippublique.com # myexternalip.com # ip6 curl -I "http://myexternalip.com" my-ip-address.co # myipnumber.com # myipv3.hn.org # portchecktool.com # privateinternetaccess.com/pages/whats-my-ip # sshmyip.com # 1/2 raw ip6 only, json telnetmyip.com # 1/2 raw ip6 only, json tracemyip.org # trackip.net # websiteipaddress.com # whatismyip.akamai.com # whatsmydns.net/whats-my-ip-address.html # whatsmyip.net # ip6 defaut ) divers=( 'ssh sshmyip.com' # 'telnet telnetmyip.com' # 'telnet|nc 6.ifcfg.me 23' # NRP 'telnet|nc 4.ifcfg.me 23' # NRP ) }