#!/usr/bin/env bash # ========================================================================== # _ _ _ ____ ____ _____ # | | | (_) _ \ ___ _ __ / ___|___ _ _|_ _| __ __ _ ___ ___ _ __ # | |_| | | |_) / _ \ '__| | / _ \| '_ \| || '__/ _` |/ __/ _ \ '__| # | _ | | __/ __/ | | |__| (_) | | | | || | | (_| | (_| __/ | # |_| |_|_|_| \___|_| \____\___/|_| |_|_||_| \__,_|\___\___|_| # # --- High-Performance Connectivity Tracer (HiPerConTracer) --- # https://www.nntb.no/~dreibh/hipercontracer/ # ========================================================================== # # High-Performance Connectivity Tracer (HiPerConTracer) # Copyright (C) 2015-2026 by Thomas Dreibholz # # 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 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 General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . # # Contact: dreibh@simula.no # Bash options: set -euo pipefail # ###### Usage ############################################################## usage () { local exitCode="$1" echo >&2 "Usage: $0 [-h|--help] [-v|--version]" exit "${exitCode}" } # ###### Version ############################################################ version () { echo "get-default-ips @BUILD_MAJOR@.@BUILD_MINOR@.@BUILD_PATCH@" exit 0 } # ###### Main program ####################################################### # ====== Handle arguments =================================================== if (( BASH_VERSINFO[0] < 4 )) || (( (BASH_VERSINFO[0] == 4) && (BASH_VERSINFO[1] < 4) )) ; then echo "ERROR: Bash 4.4 or higher is required, but Bash ${BASH_VERSINFO[0]}.${BASH_VERSINFO[1]} is installed!" exit 1 fi UNAME="$(uname)" case "${UNAME}" in FreeBSD) GETOPT="$(PATH="/usr/local/bin:${PATH}" command -v getopt || true)" ;; NetBSD) GETOPT="$(PATH="/usr/pkg/bin:${PATH}" command -v getopt || true)" ;; OpenBSD) GETOPT="$(command -v gnugetopt || true)" ;; Darwin) GETOPT="$(env PATH="/opt/homebrew/opt/gnu-getopt/bin:${PATH}" command -v getopt || true)" ;; *) GETOPT="$(command -v getopt || true)" ;; esac if "${GETOPT}" -T >/dev/null 2>&1 || [[ $? -ne 4 ]]; then echo >&2 "ERROR: Enhanced/GNU getopt is required!" exit 1 fi options="$(${GETOPT} -o hv --long help,version -a -- "$@")" # shellcheck disable=SC2181 if [[ $? -ne 0 ]]; then usage 1 fi eval set -- "${options}" while [ $# -gt 0 ] ; do case "$1" in -h | --help) usage 0 ;; -v | --version) version ;; --) shift break ;; *) # This should not happen: wrong getopt parameters, or missing case? echo >&2 "INTERNAL ERROR: Unhandled option $1!" exit 1 ;; esac shift done if [ $# -ne 0 ] ; then usage 1 fi # ====== Find all interfaces with configured default route ================== OS=$(uname) if [ "${OS}" == "Linux" ] ; then INTERFACES_WITH_DEFAULT_ROUTE_IPv4=$(/sbin/ip -4 route show default | ( while read -r line ; do if [[ "${line}" =~ ^(default)([[:space:]]*)(.*)(dev)([[:space:]]*)([^[:space:]]*)([[:space:]]*) ]] ; then echo "${BASH_REMATCH[6]}" fi done ) | sort -u | xargs) INTERFACES_WITH_DEFAULT_ROUTE_IPv6=$(/sbin/ip -6 route show default | ( while read -r line ; do if [[ "${line}" =~ ^(default)([[:space:]]*)(.*)(dev)([[:space:]]*)([^[:space:]]*)([[:space:]]*) ]] ; then echo "${BASH_REMATCH[6]}" fi done ) | sort -u | xargs) elif [ "${OS}" == "FreeBSD" ] ; then INTERFACES_WITH_DEFAULT_ROUTE_IPv4=$(netstat -rn -f inet | grep ^default | awk '{ print $4 }' | sort -u | xargs) INTERFACES_WITH_DEFAULT_ROUTE_IPv6=$(netstat -rn -f inet6 | grep ^default | awk '{ print $4 }' | sort -u | xargs) else echo >&2 "ERROR: Unsupported operating system ${OS}!" exit 1 fi # echo "I: ${INTERFACES_WITH_DEFAULT_ROUTE_IPv4} ${INTERFACES_WITH_DEFAULT_ROUTE_IPv6}" # ====== Get addresses of these interfaces with global scope ================ if [ "${OS}" == "Linux" ] ; then ADDRESSES_WITH_DEFAULT_ROUTE_IPv4=$( ( for interface in ${INTERFACES_WITH_DEFAULT_ROUTE_IPv4} ; do ip -4 addr show dev "${interface}" scope global | ( while read -r line ; do if [[ "${line}" =~ ^([[:space:]]*)(inet)([[:space:]]*)([^[:space:]]*)/ ]] ; then address="${BASH_REMATCH[4]}" echo "${address}" fi done ) done ) | sort -u | xargs) ADDRESSES_WITH_DEFAULT_ROUTE_IPv6=$( ( for interface in ${INTERFACES_WITH_DEFAULT_ROUTE_IPv6} ; do ip -6 addr show dev "${interface}" scope global | ( while read -r line ; do if [[ "${line}" =~ ^([[:space:]]*)(inet6)([[:space:]]*)([^[:space:]]*)/ ]] ; then address="${BASH_REMATCH[4]}" echo "${address}" fi done ) done ) | sort -u | xargs) elif [ "${OS}" == "FreeBSD" ] ; then ADDRESSES_WITH_DEFAULT_ROUTE_IPv4=$( ( for interface in ${INTERFACES_WITH_DEFAULT_ROUTE_IPv4} ; do ifconfig "${interface}" | ( while read -r line ; do if [[ "${line}" =~ ^([[:space:]]*)(inet)([[:space:]]*)([^[:space:]]*)([[:space:]]*) ]] ; then address="${BASH_REMATCH[4]}" if [[ ! "${line}" =~ scopeid ]] ; then echo "${address}" fi fi done ) done ) | sort -u | xargs) ADDRESSES_WITH_DEFAULT_ROUTE_IPv6=$( ( for interface in ${INTERFACES_WITH_DEFAULT_ROUTE_IPv6} ; do ifconfig "${interface}" | ( while read -r line ; do if [[ "${line}" =~ ^([[:space:]]*)(inet6)([[:space:]]*)([^[:space:]]*)([[:space:]]*) ]] ; then address="${BASH_REMATCH[4]}" if [[ ! "${line}" =~ scopeid ]] ; then echo "${address}" fi fi done ) done ) | sort -u | xargs) fi # echo "A: ${ADDRESSES_WITH_DEFAULT_ROUTE_IPv4} ${ADDRESSES_WITH_DEFAULT_ROUTE_IPv6}" echo "${ADDRESSES_WITH_DEFAULT_ROUTE_IPv4} ${ADDRESSES_WITH_DEFAULT_ROUTE_IPv6}"