- Migrate to Rx3 base libray 1.1.2,

- Add bash completion script.
This commit is contained in:
2026-04-21 18:49:31 +02:00
parent 276ce11f80
commit ef456e5f79
6 changed files with 351 additions and 106 deletions

View File

@@ -1,3 +1,12 @@
------------------------------------------------------------------------------------------------------------------------------------
MyIP V 1.2.1 - A. GIBERT - 2026/04/22
------------------------------------------------------------------------------------------------------------------------------------
- Migrate to Rx3 base libray 1.1.2,
- Add bash completion script.
------------------------------------------------------------------------------------------------------------------------------------ ------------------------------------------------------------------------------------------------------------------------------------
MyIP V 1.2.0 - A. GIBERT - 2025/09/18 MyIP V 1.2.0 - A. GIBERT - 2025/09/18
------------------------------------------------------------------------------------------------------------------------------------ ------------------------------------------------------------------------------------------------------------------------------------

View File

@@ -1,5 +1,30 @@
#-----------------------------------------------------------------------------------------------------------------------------------
#
# MyIP
#
# Copyright (C) 2025-2026 Arnaud G. GIBERT
# mailto:arnaud@rx3.net
#
# This is free software: you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) 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 Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this program; If not, see
# <https://www.gnu.org/licenses/>.
#
#-----------------------------------------------------------------------------------------------------------------------------------
%define name myip %define name myip
%define version 1.2.0 %define version 1.2.1
%define release %mkrel 1rx3 %define release %mkrel 1rx3
%global debug_package %{nil} %global debug_package %{nil}
@@ -14,15 +39,17 @@ Version: %{version}
Release: %{release} Release: %{release}
Summary: Simple script to get your public and private IP addresses Summary: Simple script to get your public and private IP addresses
License: GPL License: GPL
URL: https://git.rx3.org/gitea/rx3/myip URL: https://git.rx3.org/gitea/rx3/%{name}
Source0: %{name}-%{version}.tar.bz2 Source0: https://git.rx3.org/gitea/rx3/%{name}/archive/%{name}-%{version}.tar.gz
Distribution: Rx3 Free Software Distribution: Rx3 Free Software
Vendor: Rx3 Vendor: Rx3
Packager: Arnaud G. GIBERT <arnaud@rx3.net> Packager: Arnaud G. GIBERT <arnaud@rx3.net>
Requires: bind-utils BuildArch: noarch
Requires: network_tools-libs
@@ -31,34 +58,74 @@ Simple script to get your public and private IP.
#-----------------------------------------------------------------------------------------------------------------------------------
# Prep
#-----------------------------------------------------------------------------------------------------------------------------------
%prep %prep
%setup -q -n %{name} %setup -q -n %{name}
#-----------------------------------------------------------------------------------------------------------------------------------
# Build
#-----------------------------------------------------------------------------------------------------------------------------------
%build %build
#-----------------------------------------------------------------------------------------------------------------------------------
# Install
#-----------------------------------------------------------------------------------------------------------------------------------
%install %install
install -D -pm 755 bin/%{name} %{buildroot}%{_bindir}/%{name} install -D -pm 644 etc/bash_completion.d/%{name} %{buildroot}%{_sysconfdir}/bash_completion.d/%{name}
install -D -pm 755 var/www/cgi-bin/%{name}.cgi %{buildroot}%{_webcgi}/%{name}.cgi install -D -pm 755 bin/%{name} %{buildroot}%{_bindir}/%{name}
install -D -pm 755 www/cgi-bin/%{name}.cgi %{buildroot}%{_webcgi}/%{name}.cgi
#-----------------------------------------------------------------------------------------------------------------------------------
# Check
#-----------------------------------------------------------------------------------------------------------------------------------
%check %check
#-----------------------------------------------------------------------------------------------------------------------------------
# Files
#-----------------------------------------------------------------------------------------------------------------------------------
%files %files
%doc ReadMe.txt ReleaseNotes.txt %doc ReadMe.txt ReleaseNotes.txt ToDo.txt
%license GNU_GPL-3.0.txt GNU_LGPL-3.0.txt GNU_FDL-1.3.txt %license GNU_GPL-3.0.txt GNU_LGPL-3.0.txt GNU_FDL-1.3.txt
%{_sysconfdir}/bash_completion.d/%{name}
%{_bindir}/%{name} %{_bindir}/%{name}
%{_webcgi}/%{name}.cgi %{_webcgi}/%{name}.cgi
%changelog
#-----------------------------------------------------------------------------------------------------------------------------------
# ChangeLog
#-----------------------------------------------------------------------------------------------------------------------------------
%%changelog
* Tue Apr 21 2026 Arnaud G. GIBERT <arnaud@rx3.net> - 1.2.1-1rx3.mga9
- Update to 1.2.1
- Add bash completion script
* Thu Sep 18 2025 Arnaud G. GIBERT <arnaud@rx3.net> - 1.2.0-1rx3.mga9 * Thu Sep 18 2025 Arnaud G. GIBERT <arnaud@rx3.net> - 1.2.0-1rx3.mga9
- Update to 1.2.0 - Update to 1.2.0

1
ToDo.txt Normal file
View File

@@ -0,0 +1 @@
- ...

296
bin/myip
View File

@@ -1,144 +1,246 @@
#!/bin/bash #!/bin/bash
#----------------------------------------------------------------------------------------------------------------------- #-----------------------------------------------------------------------------------------------------------------------------------
# #
# MyIP # MyIP
# Copyright (C) 2025 Arnaud G. GIBERT #
# Copyright (C) 2025-2026 Arnaud G. GIBERT
# mailto:arnaud@rx3.net # mailto:arnaud@rx3.net
# #
# This program is free software; you can redistribute it and/or # This is free software: you can redistribute it and/or modify it
# modify it under the terms of the GNU General Public License # under the terms of the GNU Lesser General Public License as published
# as published by the Free Software Foundation; either version 2 # by the Free Software Foundation, either version 3 of the License, or
# of the License, or any later version. # (at your option) any later version.
# #
# This program is distributed in the hope that it will be useful, # This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of # but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details. # GNU Lesser General Public License for more details.
# #
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU Lesser General Public
# along with this program; if not, write to the Free Software # License along with this program; If not, see
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. # <https://www.gnu.org/licenses/>.
# #
#----------------------------------------------------------------------------------------------------------------------- #-----------------------------------------------------------------------------------------------------------------------------------
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" #-----------------------------------------------------------------------------------------------------------------------------------
# Includes
#-----------------------------------------------------------------------------------------------------------------------------------
: "${RX3_LIB_DIR:=/usr/lib/rx3}"
. "${RX3_LIB_DIR}/network.bash"
#----------------------------------------------------------------------------------------------------------------------- #-----------------------------------------------------------------------------------------------------------------------------------
# ip_filter # Global Variables
#----------------------------------------------------------------------------------------------------------------------- #-----------------------------------------------------------------------------------------------------------------------------------
ip_filter() declare -g VERSION="1.2.1"
declare -g NAME="MyIP"
declare -g HELP="usage: [-h | --help] | [-V | --version] | [-i | --internal] | [-a | --all] [-T | --test] [-v | --verbose] "
declare -g MODE="EXTERNAL"
declare -g VERBOSE="FALSE"
declare -g DRY_RUN="FALSE"
declare -g RETVAL=0
declare -g WEB_SERVER_NAME="www.rx3"
declare -g WEB_SERVER_IP="10.0.0.65"
declare -g WEB_SERVER_URL="http://www.rx3/cgi-bin/myip.cgi"
declare -g RESOLVER_NAME="resolver1.opendns.com"
declare -g RESOLVER_IP="208.67.222.222"
#-----------------------------------------------------------------------------------------------------------------------------------
# Version Print
#-----------------------------------------------------------------------------------------------------------------------------------
myip_version_print()
{ {
grep -E '^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$' version_print
} }
#----------------------------------------------------------------------------------------------------------------------- #-----------------------------------------------------------------------------------------------------------------------------------
# resolv # Help Print
#----------------------------------------------------------------------------------------------------------------------- #-----------------------------------------------------------------------------------------------------------------------------------
resolv() myip_help_print()
{ {
local host_name="$1" myip_version_print
local host_ip="$2" help_print
echo " default: external IP"
echo " -i: internal IP"
echo " -a: internal + external IP"
}
#-----------------------------------------------------------------------------------------------------------------------------------
# Arg Parse
#-----------------------------------------------------------------------------------------------------------------------------------
myip_args_parse()
{
tmp_args=$(getopt -o ahiTvV --long all,help,internal,test,verbose,version -n "${NAME}" -- "$@")
if [ $? != 0 ]; then echo_error "Terminating..."; exit 1; fi
eval set -- "${tmp_args}"
while true
do
case "$1" in
# Mode switches
-a|--all) MODE="ALL"; shift;;
-h|--help) MODE="EXIT"; myip_help_print; shift;;
-i|--internal) MODE="INTERNAL"; shift;;
-V|--version) MODE="EXIT"; myip_version_print; shift;;
# Global options
-T|--test) DRY_RUN="TRUE"; shift;;
-v|--verbose) VERBOSE="TRUE"; shift;;
# End of options
--) shift; break;;
*) echo "args_parse internal error [$1]!"; exit 1;;
esac
done
}
#-----------------------------------------------------------------------------------------------------------------------------------
# Internal IP Get
#-----------------------------------------------------------------------------------------------------------------------------------
myip_internal_get()
{
local int_ip
local int_label
local web_server_ip
resolv_ip="$( dig +short "${host_name}" | ip_filter)" if [[ "${VERBOSE}" == "TRUE" ]]
if [[ "${resolv_ip}" == "" ]]
then then
RESOLV_STATUS="KO" int_label="Internal IP: "
resolv_ip="${host_ip=}"
else else
RESOLV_STATUS="OK" int_label=""
fi fi
}
dns_lookup "A" "${WEB_SERVER_NAME}" "NOCACHE"
web_server_ip="${dns_value}"
if [[ "${web_server_ip}" == "" ]]
#-----------------------------------------------------------------------------------------------------------------------
# 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 then
int_flag="TRUE" if [[ "${VERBOSE}" == "TRUE" ]]
ext_flag="TRUE"
verbose_flag="TRUE"
else
verbose_flag="FALSE"
if [[ "$1" == "-i" ]]
then then
int_flag="TRUE" echo_error "Warning: resolver failed for [${WEB_SERVER_NAME}], using default address: [${WEB_SERVER_IP}]"
ext_flag="FALSE"
else
int_flag="FALSE"
ext_flag="TRUE"
fi fi
web_server_ip="${WEB_SERVER_IP}"
fi fi
fi
int_ip="$(cmd_exec curl --resolve "${WEB_SERVER_NAME}:80:${web_server_ip}" -s "${WEB_SERVER_URL}" | ip_ip_filter)"
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}" echo "${int_label}${int_ip}"
fi }
#-----------------------------------------------------------------------------------------------------------------------------------
# External IP Get
#-----------------------------------------------------------------------------------------------------------------------------------
myip_external_get()
{
local ext_ip
local ext_label
local resolver_ip
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 [[ "${VERBOSE}" == "TRUE" ]]
if [[ ( "${ext_label}" != "") && ( "${RESOLV_STATUS}" != "OK") ]]
then then
echo "Warning: resolver failed for [${RESOLVER_NAME}], using default address: [${RESOLVER_IP}]" ext_label="External IP: "
else
ext_label=""
fi fi
dns_lookup "A" "${RESOLVER_NAME}" "NOCACHE"
resolver_ip="${dns_value}"
if [[ "${resolver_ip}" == "" ]]
then
if [[ "${VERBOSE}" == "TRUE" ]]
then
echo_error "Warning: resolver failed for [${RESOLVER_NAME}], using default address: [${RESOLVER_IP}]"
fi
resolver_ip="${RESOLVER_IP}"
fi
ext_ip="$(cmd_exec dig +short myip.opendns.com "@${resolver_ip}" | ip_ip_filter)"
echo "${ext_label}${ext_ip}" echo "${ext_label}${ext_ip}"
}
#-----------------------------------------------------------------------------------------------------------------------------------
# Main
#-----------------------------------------------------------------------------------------------------------------------------------
myip_args_parse "$@"
if [[ "${MODE}" == "EXIT" ]]
then
exit 0
fi fi
dns_init
case "${MODE}" in
"INTERNAL")
myip_internal_get
;;
"EXTERNAL")
myip_external_get
;;
"ALL")
myip_internal_get
myip_external_get
;;
*)
myip_help_print
RETVAL=1
;;
esac
dns_deinit
exit ${RETVAL}

View File

@@ -0,0 +1,65 @@
#!/bin/bash
#-----------------------------------------------------------------------------------------------------------------------------------
#
# MyIP Bash Completion
#
# Copyright (C) 2025-2026 Arnaud G. GIBERT
# mailto:arnaud@rx3.net
#
# This is free software: you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) 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 Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this program; If not, see
# <https://www.gnu.org/licenses/>.
#
#-----------------------------------------------------------------------------------------------------------------------------------
#-----------------------------------------------------------------------------------------------------------------------------------
# MyIP Completion
#-----------------------------------------------------------------------------------------------------------------------------------
_myip_completion()
{
local mode_opts="-h --help -V --version -i --internal -a --all"
local glob_opts="-T --test -v --verbose"
local cur="${COMP_WORDS[COMP_CWORD]}"
local mode_set="FALSE"
local i
COMPREPLY=()
# Scan already provided words to detect if a mode option was given
for (( i=1; i<COMP_CWORD; i++ ))
do
case "${COMP_WORDS[i]}" in
-h|--help|-V|--version|-i|--internal|-a|--all)
mode_set="TRUE"
;;
esac
done
# Build candidate list depending on whether a mode is already set
if [[ "${mode_set}" == "TRUE" ]]
then
COMPREPLY=( $(compgen -W "${glob_opts}" -- "${cur}") )
else
COMPREPLY=( $(compgen -W "${mode_opts} ${glob_opts}" -- "${cur}") )
fi
return 0
}
complete -o filenames -F _myip_completion myip

View File

@@ -2,7 +2,8 @@
#----------------------------------------------------------------------------------------------------------------------- #-----------------------------------------------------------------------------------------------------------------------
# #
# MyIP.CGI # MyIP.CGI
# Copyright (C) 2025 Arnaud G. GIBERT #
# Copyright (C) 2025-2026 Arnaud G. GIBERT
# mailto:arnaud@rx3.net # mailto:arnaud@rx3.net
# #
# This program is free software; you can redistribute it and/or # This program is free software; you can redistribute it and/or