2020-05-15 06:03:38 +02:00
|
|
|
#!/bin/bash
|
|
|
|
########################################################################
|
2020-05-20 18:37:59 +02:00
|
|
|
# Author: Fred (support@qo-op.com) + @benoit
|
|
|
|
# Version: 0.4
|
2020-05-15 06:03:38 +02:00
|
|
|
# License: AGPL-3.0 (https://choosealicense.com/licenses/agpl-3.0/)
|
|
|
|
########################################################################
|
2020-05-16 05:29:15 +02:00
|
|
|
{ # Full reading before execution !
|
2020-05-16 04:37:10 +02:00
|
|
|
# echo '
|
|
|
|
#
|
|
|
|
# __ ___ _ ___
|
|
|
|
# / |/ /_ __ ____ ____ _____ ___ ___ (_)___/__ \
|
|
|
|
# / /|_/ / / / / / __ \/ __ `/ __ `__ \/ _ \ / / ___// _/
|
|
|
|
# / / / / /_/ / / / / / /_/ / / / / / / __/ / (__ )/_/
|
|
|
|
# /_/ /_/\__, / /_/ /_/\__,_/_/ /_/ /_/\___/ /_/____/(_)
|
|
|
|
# /____/
|
|
|
|
#
|
|
|
|
# ' | lolcat
|
2020-05-15 06:03:38 +02:00
|
|
|
MY_PATH="`dirname \"$0\"`" # relative
|
|
|
|
MY_PATH="`( cd \"$MY_PATH\" && pwd )`" # absolutized and normalized
|
|
|
|
ME="${0##*/}"
|
|
|
|
|
2020-05-20 18:37:06 +02:00
|
|
|
# check if -w is specified:
|
|
|
|
# Will look for the public IP of my gateway
|
|
|
|
# if my node is a Demilitarized Zone (DMZ)
|
|
|
|
WAN=$1
|
|
|
|
|
2020-05-16 05:29:15 +02:00
|
|
|
# GNU DEBIAN Core install extension
|
2020-05-16 04:38:04 +02:00
|
|
|
[[ ! $(which nslookup) ]] && sudo apt-get update && sudo apt-get install dnsutils -y
|
2020-05-15 06:03:38 +02:00
|
|
|
|
2020-05-20 18:37:06 +02:00
|
|
|
# What is myIP?
|
2020-05-23 18:10:10 +02:00
|
|
|
export myIP=$(hostname -I | awk '{print $1}' | head -n 1)
|
2020-05-15 06:03:38 +02:00
|
|
|
# Check if IP is from reserved LAN addresses
|
2020-05-16 05:29:15 +02:00
|
|
|
export isLAN=$(echo $myIP | grep -E "/(^127\.)|(^192\.168\.)|(^10\.)|(^172\.1[6-9]\.)|(^172\.2[0-9]\.)|(^172\.3[0-1]\.)|(^::1$)|(^[fF][cCdD])/")
|
2020-05-20 18:37:06 +02:00
|
|
|
# Find default route gateway
|
2020-05-22 15:15:03 +02:00
|
|
|
export myRouter=$(sudo route -n | grep ' UG ' | tail -n 1 | awk '{print $2}')
|
2020-05-20 18:37:06 +02:00
|
|
|
|
|
|
|
# Ask to the router my name (BOX DNS or system defined)
|
2020-05-22 15:15:03 +02:00
|
|
|
[[ $isLAN ]] && export NODENAME=$(sudo nslookup $myIP $myRouter | grep 'name =' | awk -F ' = ' '{print $2}' | sed 's/\.$//') \
|
|
|
|
|| export NODENAME=$(sudo nslookup $myIP | grep 'name =' | awk -F ' = ' '{print $2}' | sed 's/\.$//')
|
2020-05-15 06:06:28 +02:00
|
|
|
|
2020-05-20 18:37:06 +02:00
|
|
|
if [[ -z "$NODENAME" && "$WAN" == "-w" ]]; then
|
|
|
|
IP=`wget --quiet http://brouits.free.fr/ip.php -O- | sed 's/\r//'`
|
|
|
|
NODENAME=`nslookup $IP | head -1 | cut -d'=' -f 2 | sed -E 's/^ *(.*)\.$/\1/'`
|
|
|
|
fi
|
|
|
|
|
|
|
|
# We must have that nodename, perhaps BOX DNS doesn't exist...
|
2020-05-22 15:15:03 +02:00
|
|
|
[[ $NODENAME == "" ]] && [[ $isLAN ]] && export NODENAME=$(sudo nslookup $myIP | grep 'name =' | awk -F ' = ' '{print $2}' | sed 's/\.$//')
|
2020-05-20 18:37:06 +02:00
|
|
|
# OK no DNS is answering, let's be /etc/hostname
|
2020-05-20 15:02:00 +02:00
|
|
|
[[ $NODENAME == "" ]] && NODENAME=$(cat /etc/hostname)
|
|
|
|
|
2020-05-20 21:56:04 +02:00
|
|
|
# I can tell my name is (in lowercase)
|
|
|
|
echo "$NODENAME" | awk '{print tolower($1)}'
|
2020-05-15 06:03:38 +02:00
|
|
|
|
2020-05-16 05:29:15 +02:00
|
|
|
} # Full reading before execution !
|