This commit is contained in:
kyodev 2017-08-07 09:39:51 +02:00
parent 993725bf82
commit fd0eb6d20c
2 changed files with 313 additions and 0 deletions

313
scripts/getIp Executable file
View File

@ -0,0 +1,313 @@
#!/bin/bash
#!/bin/bash
version=0.1.0
date="07/08/2017"
projet="simpledeb"
contact="IRC freenode ##sdeb ou https://framagit.org/kyodev/kyopages/issues"
script="getIp"
urlScript="https://framagit.org/kyodev/kyopages/raw/master/scripts/$script"
##### license LPRAB/WTFPL
# auteur: simpledebian
# contributeurs: kyodev
#####
# log spécifique, fichier log limité à 10000octets, $1 message à loguer
f__log(){
if [ -w "$fileLogs" ]; then
if [ "$(stat -c %s $fileLogs)" -ge "10000" ]; then
echo "$(date +%Y%m%d\ %H%M%S) $1" &>/dev/null > "$fileLogs"
else
echo "$(date +%Y%m%d\ %H%M%S) $1" &>/dev/null >> "$fileLogs"
fi
fi
}
# affichage $1 en rouge, $1++ optionnels en bleu, sortie script sur erreur
f__error() {
echo -e "\n$RED Erreur critique: $1 $COLOR"
for (( i=2 ; i<=$# ; i++ )); do echo -e " $BLUE${!i}"; done
echo "$COLOR"
if [ "$opType" == "upgrade" ]; then f__log "$script $version: $1"; fi
exit 1
}
# user ayant initié la session graphique, assigne $user_, testé avec mate, gnome, xfce. manque KDE
# sortie script sur erreur, gestion variable environnement pour personnaliser l'user avec:
# USER_INSTALL=user script
f__user(){
[ "$USER_INSTALL" ] && user_="$USER_INSTALL" # user_ via variable en environnement
if [ -z "$user_" ]; then # priorité à $USER_INSTALL
[ "$SUDO_UID" ] && user_id="$SUDO_UID" # sudo-gnome
echo "$XAUTHORITY" | grep -Eo '[0-9]+' && user_id="$(echo $XAUTHORITY | grep -Eo '[0-9]+')" || \
user_id="$(echo $XDG_RUNTIME_DIR | grep -Eo '[0-9]+')"
[ "$user_id" ] && user_="$(grep $user_id /etc/passwd | cut -d ":" -f 1 )" || \
user_="$(echo $XAUTHORITY | cut -d "/" -f 3)"
fi
[ "$user_" ] && [ -d "/home/$user_" ] && return 0 || echo " répertoire '/home/$user_' INEXISTANT"
f__error "user indéterminé, lancer le script avec:\n USER_INSTALL=user $0" \
"merci contacter $projet $contact pour corriger ça"
}
# install du script dans le système
fscript_install(){
if [ $EUID -ne 0 ]; then f__error "vous devez être ROOT pour installer ce script dans le système"; fi
if ! which wget &>/dev/null; then f__error "wget manquant pour exploiter ce script"; fi
f__user
# install /opt
mkdir -p /opt/bin/
cp -d "$(basename $0)" "/opt/bin/$script"
chmod a+rwx "/opt/bin/$script"
chown "$user_": "/opt/bin/$script"
ln -s "/opt/bin/$script" "/usr/bin/$script" &>/dev/null
# cron/anacron install
fscript_cronAnacron "install"
# création fichier log
touch "$fileLogs"
chmod a+rwx "$fileLogs"
chown "$user_": "$fileLogs"
[ -f "$script"_changelog.md ] || rm "$(basename $0)" &>/dev/null ## on efface pas si changelog (dev)
echo -e "\n$GREEN $script installé dans le système." "maintenant, appel du script par: $script (sans ./)\n$COLOR"
f__log "$script $version installé dans le système"
}
# suppression du script dans le système
fscript_remove(){
if [ $EUID -ne 0 ]; then f__error "vous devez être ROOT pour supprimer ce script du système"; fi
f__user
# suppression de /opt
rm /opt/bin/"$script" &>/dev/null
unlink /usr/bin/"$script" &>/dev/null
unlink /usr/local/bin/"$script" &>/dev/null # ancienne localisation
# cron/anacron remove
fscript_cronAnacron remove
echo -e "\n$GREEN $script supprimé du système.\n$COLOR"
f__log "$script $version supprimé du système"
}
# version script en ligne, assigne $versionScript, $script_aJour=ok|ko
fscript_get_version(){
versionScript="$(wget -q --tries=2 --timeout=15 -O - "$urlScript" | grep '^version=' | cut -d '=' -f 2)"
if [ "$versionScript" ]; then
if [ "$version" != "$versionScript" ] && [ "$versionScript" ]; then
echo -e "\n$GREEN version script en ligne: $versionScript, mise à jour possible\n$GREEN"
script_aJour="ko"
else script_aJour="ok"; fi
fi
}
# mise à jour script si dispo
fscript_dl(){
if [ "$script_aJour" == "ok" ]; then return 0; fi
if ! which wget &>/dev/null; then f__log "wget manquant pour upgrader"; return 1; 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 a+rwx "$dirTemp$script"
chown "$user_": "$dirTemp$script"
cp -d "$dirTemp$script" /opt/bin/"$script"
rm -rf "$dirTemp"
echo -e "\n$GREEN $script mis à jour en version $versionScript\n$COLOR"
f__log "$script mis à jour en version $versionScript"
exit 0
}
# inscription dans tache upgrade en anacron hebdomadaire, via cron horaire
fscript_cronAnacron(){
local dirAnacron="/home/$user_/.config/anatest"
local dirSpool="$dirAnacron/spool"
local fileAnacron="$dirAnacron/$script.anacrontab"
case "$1" in
install )
mkdir -p "$dirAnacron"
# table anacron
echo "1 05 $script nice /opt/bin/$script --upgrade &>/dev/null" > "$fileAnacron"
## test: anacron journalier: echo "1 05 ""$script""test nice /opt/bin/$script --upgrade &>/dev/null" > "$fileAnacron"
chown -R --no-preserve-root "$user_": "$dirAnacron"
chmod -R --no-preserve-root ug+xw "$dirAnacron"
# création spool anacron utilisateur
mkdir -p "$dirSpool"
chown -R --no-preserve-root "$user_": "$dirSpool"
chmod -R --no-preserve-root ug+xw "$dirSpool"
# cron tab pour activation horaire anacron
grep -q ".*$script.anacrontab" /etc/crontab || \
echo "@hourly $user_ /usr/sbin/anacron -t $fileAnacron -S $dirSpool" >> /etc/crontab
;;
remove )
rm "$dirSpool/$script"* &>/dev/null
sed -i "/$script.anacrontab/d" /etc/crontab
rm "$fileAnacron" &>/dev/null
rm "$dirSpool/$script" &>/dev/null
rmdir "$dirSpool" "$dirAnacron" &>/dev/null
;;
esac
}
get_ip() { # $ip_local, $gateway, $gateway_type, $gateway_one, $ip_local, $ip_local_type, $ifname, $ifname_type
ip_local=$(ip route | awk '/proto.*src/ {print $9, "pour",$3}')
gateway=$(ip route | awk '/default.*via/ {print $3, "pour", $5}')
gateway_one=$(head -n 1 <<< $gateway | awk '{print $1}')
ifname=$(ip route | awk '/proto.*src/ {print $3}')
gateway_type=$(sed -E 's/(^.*wl.*)/\1 (wifi)/;s/(^.*en.*|^.*eth.µ)/\1 (ethernet)/' <<< $gateway)
ip_local_type=$(sed -E 's/(^.*wl.*)/\1 (wifi)/;s/(^.*en.*|^.*eth.*)/\1 (ethernet)/' <<< $ip_local)
ifname_type=$(sed -E 's/(^wl.*)/\1 (wifi)/;s/(^en.*|^eth.*)/\1 (ethernet)/' <<< $ifname)
}
get_ip_public(){ # $1=IPv4|IPv6, assigne $ip_public
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
# ip_test+=" https://4.ifcfg.me" #down avec wget!
}
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"
# ip_test+=" http://smart-ip.net/myip" #down avec wget?!
# ip_test+=" https://6.ifcfg.me" #down avec wget?!
}
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"
}
unset ip_public
local dig_test
local ip_test
if [ "$1" == "IPv4" ]; then
list_ip4_dig
list_ip4
ip_telnet=4.ifcfg.me
elif [ "$1" == "IPv6" ]; then
list_ip6_dig
list_ip6
ip_telnet=6.ifcfg.me
else return 1; fi
if which dig &>/dev/null && [ -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
cmd="wget --quiet --tries=1 --timeout=5 -O - "
for ii in $ip_test ; do
ip_public=$($cmd $ii)
[ "$ip_public" ] && break
done
fi
if which curl &>/dev/null && [ -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)
[ "$ip_public" ] && break
done
fi
if which telnet &>/dev/null && [ -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
ip_public=$(nc $ip_telnet 23 2>/dev/null | grep $1 | cut -d ' ' -f 4)
fi
if [ -z "$ip_public" ]; then
echo -e "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"
fi
}
fi_help(){
echo -e "\n$GREEN $script $version$BLUE"
cat << 'EOF'
----------------------------------------------------------------------
./getIo : exécution normale
getIp : exécution normale si script installé dans le système
ce script requiert une des commandes suivantes pour déterminer l'ip publique :
dig (dnsutils) / wget / curl / telnet / nc
options:
--help, -h : affichage aide
--install, -i : installation du script dans le système
--remove, -r : suppression du script dans le système
--upgrade, -u : upgrade script si maj possible
--version, -v : version du script, en ligne et en cours d'exécution
--ip4 : affiche ipv4 public
--ip6 : affiche ipv6 public
--local : affiche ip / passerelle privées
EOF
echo -e "$COLOR"
}
# initialisation
GREEN="$(printf "\033[0;32m")" # ok
BLUE="$(printf "\033[0;34m")" # info
RED="$(printf "\033[0;31m")" # alerte
COLOR="$(printf "\033[0m")" # standard
fileLogs="/var/log/sdeb_$script.log"
for i in $@; do
case $i in
--install | -i ) fscript_install ;; # installation du script dans le système
--remove | -r ) fscript_remove ;; # suppression du script dans le système
--version | -v ) fscript_get_version ;; # version du script, en ligne et exécuté
--upgrade | -u )
f__log="test mise à jour"
fscript_get_version
fscript_dl ;; # upgrade script si maj possible
--ip4 )
get_ip_public "IPv4"
echo "$ip_public" ;; # affiche ip v4 public
--ip6 )
get_ip_public "IPv6"
echo "$ip_public" ;; # affiche ip v6 public
--local )
get_ip
echo "ip $ip_local"
echo "passerelle $gateway";; # affiche ip v6 public
--help | -h | * ) fi_help ;; # affichage help
esac
done
exit
wget -O getIp https://framagit.org/kyodev/kyopages/raw/master/scripts/getIp
chmod +x getIp && ./getIp

View File