145 lines
3.8 KiB
Bash
Executable File
145 lines
3.8 KiB
Bash
Executable File
#!/bin/bash
|
|
#-----------------------------------------------------------------------------------------------------------------------
|
|
#
|
|
# MyIP
|
|
# Copyright (C) 2025 Arnaud G. GIBERT
|
|
# mailto:arnaud@rx3.net
|
|
#
|
|
# This program is free software; you can redistribute it and/or
|
|
# modify it under the terms of the GNU General Public License
|
|
# as published by the Free Software Foundation; either version 2
|
|
# of the License, or any later version.
|
|
#
|
|
# This program is distributed in the hope that it will be useful,
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
# GNU General Public License for more details.
|
|
#
|
|
# You should have received a copy of the GNU General Public License
|
|
# along with this program; if not, write to the Free Software
|
|
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
#
|
|
#-----------------------------------------------------------------------------------------------------------------------
|
|
|
|
WEB_SERVER_NAME="www.rx3"
|
|
WEB_SERVER_IP="10.0.0.65"
|
|
|
|
RESOLVER_NAME="resolver1.opendns.com"
|
|
RESOLVER_IP="208.67.222.222"
|
|
|
|
declare -g RESOLV_STATUS="OK"
|
|
|
|
|
|
|
|
|
|
|
|
#-----------------------------------------------------------------------------------------------------------------------
|
|
# ip_filter
|
|
#-----------------------------------------------------------------------------------------------------------------------
|
|
|
|
ip_filter()
|
|
{
|
|
grep -E '^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$'
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#-----------------------------------------------------------------------------------------------------------------------
|
|
# resolv
|
|
#-----------------------------------------------------------------------------------------------------------------------
|
|
|
|
resolv()
|
|
{
|
|
local host_name="$1"
|
|
local host_ip="$2"
|
|
|
|
|
|
resolv_ip="$( dig +short "${host_name}" | ip_filter)"
|
|
|
|
if [[ "${resolv_ip}" == "" ]]
|
|
then
|
|
RESOLV_STATUS="KO"
|
|
resolv_ip="${host_ip=}"
|
|
else
|
|
RESOLV_STATUS="OK"
|
|
fi
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#-----------------------------------------------------------------------------------------------------------------------
|
|
# main
|
|
#-----------------------------------------------------------------------------------------------------------------------
|
|
|
|
if [[ "$1" == "-h" ]]
|
|
then
|
|
echo "myip [-i] | [-a] | [-h]"
|
|
echo "default: external IP"
|
|
echo " -i: internal IP"
|
|
echo " -a: internal + external IP"
|
|
else
|
|
if [[ "$1" == "-a" ]]
|
|
then
|
|
int_flag="TRUE"
|
|
ext_flag="TRUE"
|
|
verbose_flag="TRUE"
|
|
else
|
|
verbose_flag="FALSE"
|
|
|
|
if [[ "$1" == "-i" ]]
|
|
then
|
|
int_flag="TRUE"
|
|
ext_flag="FALSE"
|
|
else
|
|
int_flag="FALSE"
|
|
ext_flag="TRUE"
|
|
fi
|
|
fi
|
|
fi
|
|
|
|
|
|
|
|
if [[ ${verbose_flag} == "TRUE" ]]
|
|
then
|
|
int_label="Internal IP: "
|
|
ext_label="External IP: "
|
|
else
|
|
int_label=""
|
|
ext_label=""
|
|
fi
|
|
|
|
|
|
if [[ ${int_flag} == "TRUE" ]]
|
|
then
|
|
resolv "${WEB_SERVER_NAME}" "${WEB_SERVER_IP}"
|
|
web_server_ip="${resolv_ip}"
|
|
|
|
int_ip="$( curl --resolve ${WEB_SERVER_NAME}:80:${web_server_ip} -s http://www.rx3/cgi-bin/myip.cgi | ip_filter)"
|
|
|
|
if [[ ( "${int_label}" != "") && ( "${RESOLV_STATUS}" != "OK") ]]
|
|
then
|
|
echo "Warning: resolver failed for [${WEB_SERVER_NAME}], using default address: [${WEB_SERVER_IP}]"
|
|
fi
|
|
|
|
echo "${int_label}${int_ip}"
|
|
fi
|
|
|
|
if [[ ${ext_flag} == "TRUE" ]]
|
|
then
|
|
resolv "${RESOLVER_NAME}" "${RESOLVER_IP}"
|
|
resolver_ip="${resolv_ip}"
|
|
|
|
ext_ip="$( dig +short myip.opendns.com @${resolver_ip} | ip_filter)"
|
|
|
|
if [[ ( "${ext_label}" != "") && ( "${RESOLV_STATUS}" != "OK") ]]
|
|
then
|
|
echo "Warning: resolver failed for [${RESOLVER_NAME}], using default address: [${RESOLVER_IP}]"
|
|
fi
|
|
|
|
echo "${ext_label}${ext_ip}"
|
|
fi
|