astroport/zen/tools/nodename_find.sh

36 lines
1.5 KiB
Bash
Raw Normal View History

2020-05-15 06:03:38 +02:00
#!/bin/bash
########################################################################
# Author: Fred (support@qo-op.com)
# Version: 0.3
# License: AGPL-3.0 (https://choosealicense.com/licenses/agpl-3.0/)
########################################################################
2020-05-16 04:37:10 +02:00
{ # garanty full reading before execution
# 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-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
myip=$(hostname -I | awk '{print $1}')
# Check if IP is from reserved LAN addresses
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 1st route gateway
myRouter=$(sudo route -n | head -n 3 | tail -n 1 | awk '{print $2}')
# Ask to the router its name (BOX DNS or system defined)
[[ $isLAN ]] && nodename=$(sudo nslookup $myip $myRouter | head -n 1 | awk -F ' = ' '{print $2}' | sed 's/\.[^.]*$//') \
|| nodename=$(sudo nslookup $myip | head -n 1 | awk -F ' = ' '{print $2}' | sed 's/\.[^.]*$//')
2020-05-15 06:06:28 +02:00
2020-05-16 04:37:10 +02:00
echo $nodename
2020-05-15 06:03:38 +02:00
}