forked from axiom-team/astroport
55 lines
2.3 KiB
Bash
Executable File
55 lines
2.3 KiB
Bash
Executable File
#!/bin/bash
|
|
########################################################################
|
|
# Author: Fred (support@qo-op.com) + @benoit
|
|
# Version: 0.4
|
|
# License: AGPL-3.0 (https://choosealicense.com/licenses/agpl-3.0/)
|
|
########################################################################
|
|
{ # Full reading before execution !
|
|
# echo '
|
|
#
|
|
# __ ___ _ ___
|
|
# / |/ /_ __ ____ ____ _____ ___ ___ (_)___/__ \
|
|
# / /|_/ / / / / / __ \/ __ `/ __ `__ \/ _ \ / / ___// _/
|
|
# / / / / /_/ / / / / / /_/ / / / / / / __/ / (__ )/_/
|
|
# /_/ /_/\__, / /_/ /_/\__,_/_/ /_/ /_/\___/ /_/____/(_)
|
|
# /____/
|
|
#
|
|
# ' | lolcat
|
|
MY_PATH="`dirname \"$0\"`" # relative
|
|
MY_PATH="`( cd \"$MY_PATH\" && pwd )`" # absolutized and normalized
|
|
ME="${0##*/}"
|
|
|
|
# check if -w is specified:
|
|
# Will look for the public IP of my gateway
|
|
# if my node is a Demilitarized Zone (DMZ)
|
|
WAN=$1
|
|
|
|
# GNU DEBIAN Core install extension
|
|
[[ ! $(which nslookup) ]] && sudo apt-get update && sudo apt-get install dnsutils -y
|
|
|
|
# What is myIP?
|
|
export myIP=$(hostname -I | awk '{print $1}')
|
|
# Check if IP is from reserved LAN addresses
|
|
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])/")
|
|
# Find default route gateway
|
|
export myRouter=$(sudo route -n | head -n 3 | tail -n 1 | awk '{print $2}')
|
|
|
|
# Ask to the router my name (BOX DNS or system defined)
|
|
[[ $isLAN ]] && export NODENAME=$(sudo nslookup $myIP $myRouter | head -n 1 | awk -F ' = ' '{print $2}' | sed 's/\.[^.]*$//') \
|
|
|| export NODENAME=$(sudo nslookup $myIP | head -n 1 | awk -F ' = ' '{print $2}' | sed 's/\.[^.]*$//')
|
|
|
|
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...
|
|
[[ $NODENAME == "" ]] && [[ $isLAN ]] && export NODENAME=$(sudo nslookup $myIP | head -n 1 | awk -F ' = ' '{print $2}' | sed 's/\.[^.]*$//')
|
|
# OK no DNS is answering, let's be /etc/hostname
|
|
[[ $NODENAME == "" ]] && NODENAME=$(cat /etc/hostname)
|
|
|
|
# I can tell my name is (in lowercase)
|
|
echo "$NODENAME" | awk '{print tolower($1)}'
|
|
|
|
} # Full reading before execution !
|