Debian Codegraph @

src/get-default-ips

raw symbols SCIP
1 #!/usr/bin/env bash
2 # ==========================================================================
3 # _ _ _ ____ ____ _____
4 # | | | (_) _ \ ___ _ __ / ___|___ _ _|_ _| __ __ _ ___ ___ _ __
5 # | |_| | | |_) / _ \ '__| | / _ \| '_ \| || '__/ _` |/ __/ _ \ '__|
6 # | _ | | __/ __/ | | |__| (_) | | | | || | | (_| | (_| __/ |
7 # |_| |_|_|_| \___|_| \____\___/|_| |_|_||_| \__,_|\___\___|_|
8 #
9 # --- High-Performance Connectivity Tracer (HiPerConTracer) ---
10 # https://www.nntb.no/~dreibh/hipercontracer/
11 # ==========================================================================
12 #
13 # High-Performance Connectivity Tracer (HiPerConTracer)
14 # Copyright (C) 2015-2026 by Thomas Dreibholz
15 #
16 # This program is free software: you can redistribute it and/or modify
17 # it under the terms of the GNU General Public License as published by
18 # the Free Software Foundation, either version 3 of the License, or
19 # (at your option) any later version.
20 #
21 # This program is distributed in the hope that it will be useful,
22 # but WITHOUT ANY WARRANTY; without even the implied warranty of
23 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 # GNU General Public License for more details.
25 #
26 # You should have received a copy of the GNU General Public License
27 # along with this program. If not, see <http://www.gnu.org/licenses/>.
28 #
29 # Contact: [email protected]
30
31 # Bash options:
32 set -euo pipefail
33
34
35 # ###### Usage ##############################################################
36 usage () {
37 local exitCode="$1"
38 echo >&2 "Usage: $0 [-h|--help] [-v|--version]"
39 exit "${exitCode}"
40 }
41
42
43 # ###### Version ############################################################
44 version () {
45 echo "get-default-ips @BUILD_MAJOR@.@BUILD_MINOR@.@BUILD_PATCH@"
46 exit 0
47 }
48
49
50
51 # ###### Main program #######################################################
52
53 # ====== Handle arguments ===================================================
54 if (( BASH_VERSINFO[0] < 4 )) || (( (BASH_VERSINFO[0] == 4) && (BASH_VERSINFO[1] < 4) )) ; then
55 echo "ERROR: Bash 4.4 or higher is required, but Bash ${BASH_VERSINFO[0]}.${BASH_VERSINFO[1]} is installed!"
56 exit 1
57 fi
58 UNAME="$(uname)"
59 case "${UNAME}" in
60 FreeBSD)
61 GETOPT="$(PATH="/usr/local/bin:${PATH}" command -v getopt || true)"
62 ;;
63 NetBSD)
64 GETOPT="$(PATH="/usr/pkg/bin:${PATH}" command -v getopt || true)"
65 ;;
66 OpenBSD)
67 GETOPT="$(command -v gnugetopt || true)"
68 ;;
69 Darwin)
70 GETOPT="$(env PATH="/opt/homebrew/opt/gnu-getopt/bin:${PATH}" command -v getopt || true)"
71 ;;
72 *)
73 GETOPT="$(command -v getopt || true)"
74 ;;
75 esac
76 if "${GETOPT}" -T >/dev/null 2>&1 || [[ $? -ne 4 ]]; then
77 echo >&2 "ERROR: Enhanced/GNU getopt is required!"
78 exit 1
79 fi
80 options="$(${GETOPT} -o hv --long help,version -a -- "$@")"
81 # shellcheck disable=SC2181
82 if [[ $? -ne 0 ]]; then
83 usage 1
84 fi
85
86 eval set -- "${options}"
87 while [ $# -gt 0 ] ; do
88 case "$1" in
89 -h | --help)
90 usage 0
91 ;;
92 -v | --version)
93 version
94 ;;
95 --)
96 shift
97 break
98 ;;
99 *)
100 # This should not happen: wrong getopt parameters, or missing case?
101 echo >&2 "INTERNAL ERROR: Unhandled option $1!"
102 exit 1
103 ;;
104 esac
105 shift
106 done
107 if [ $# -ne 0 ] ; then
108 usage 1
109 fi
110
111
112 # ====== Find all interfaces with configured default route ==================
113 OS=$(uname)
114 if [ "${OS}" == "Linux" ] ; then
115 INTERFACES_WITH_DEFAULT_ROUTE_IPv4=$(/sbin/ip -4 route show default | (
116 while read -r line ; do
117 if [[ "${line}" =~ ^(default)([[:space:]]*)(.*)(dev)([[:space:]]*)([^[:space:]]*)([[:space:]]*) ]] ; then
118 echo "${BASH_REMATCH[6]}"
119 fi
120 done
121 ) | sort -u | xargs)
122 INTERFACES_WITH_DEFAULT_ROUTE_IPv6=$(/sbin/ip -6 route show default | (
123 while read -r line ; do
124 if [[ "${line}" =~ ^(default)([[:space:]]*)(.*)(dev)([[:space:]]*)([^[:space:]]*)([[:space:]]*) ]] ; then
125 echo "${BASH_REMATCH[6]}"
126 fi
127 done
128 ) | sort -u | xargs)
129 elif [ "${OS}" == "FreeBSD" ] ; then
130 INTERFACES_WITH_DEFAULT_ROUTE_IPv4=$(netstat -rn -f inet | grep ^default | awk '{ print $4 }' | sort -u | xargs)
131 INTERFACES_WITH_DEFAULT_ROUTE_IPv6=$(netstat -rn -f inet6 | grep ^default | awk '{ print $4 }' | sort -u | xargs)
132 else
133 echo >&2 "ERROR: Unsupported operating system ${OS}!"
134 exit 1
135 fi
136
137 # echo "I: ${INTERFACES_WITH_DEFAULT_ROUTE_IPv4} ${INTERFACES_WITH_DEFAULT_ROUTE_IPv6}"
138
139
140 # ====== Get addresses of these interfaces with global scope ================
141 if [ "${OS}" == "Linux" ] ; then
142 ADDRESSES_WITH_DEFAULT_ROUTE_IPv4=$(
143 ( for interface in ${INTERFACES_WITH_DEFAULT_ROUTE_IPv4} ; do
144 ip -4 addr show dev "${interface}" scope global | (
145 while read -r line ; do
146 if [[ "${line}" =~ ^([[:space:]]*)(inet)([[:space:]]*)([^[:space:]]*)/ ]] ; then
147 address="${BASH_REMATCH[4]}"
148 echo "${address}"
149 fi
150 done
151 )
152 done
153 ) | sort -u | xargs)
154 ADDRESSES_WITH_DEFAULT_ROUTE_IPv6=$(
155 ( for interface in ${INTERFACES_WITH_DEFAULT_ROUTE_IPv6} ; do
156 ip -6 addr show dev "${interface}" scope global | (
157 while read -r line ; do
158 if [[ "${line}" =~ ^([[:space:]]*)(inet6)([[:space:]]*)([^[:space:]]*)/ ]] ; then
159 address="${BASH_REMATCH[4]}"
160 echo "${address}"
161 fi
162 done
163 )
164 done
165 ) | sort -u | xargs)
166 elif [ "${OS}" == "FreeBSD" ] ; then
167 ADDRESSES_WITH_DEFAULT_ROUTE_IPv4=$(
168 ( for interface in ${INTERFACES_WITH_DEFAULT_ROUTE_IPv4} ; do
169 ifconfig "${interface}" | (
170 while read -r line ; do
171 if [[ "${line}" =~ ^([[:space:]]*)(inet)([[:space:]]*)([^[:space:]]*)([[:space:]]*) ]] ; then
172 address="${BASH_REMATCH[4]}"
173 if [[ ! "${line}" =~ scopeid ]] ; then
174 echo "${address}"
175 fi
176 fi
177 done
178 )
179 done
180 ) | sort -u | xargs)
181 ADDRESSES_WITH_DEFAULT_ROUTE_IPv6=$(
182 ( for interface in ${INTERFACES_WITH_DEFAULT_ROUTE_IPv6} ; do
183 ifconfig "${interface}" | (
184 while read -r line ; do
185 if [[ "${line}" =~ ^([[:space:]]*)(inet6)([[:space:]]*)([^[:space:]]*)([[:space:]]*) ]] ; then
186 address="${BASH_REMATCH[4]}"
187 if [[ ! "${line}" =~ scopeid ]] ; then
188 echo "${address}"
189 fi
190 fi
191 done
192 )
193 done
194 ) | sort -u | xargs)
195 fi
196
197 # echo "A: ${ADDRESSES_WITH_DEFAULT_ROUTE_IPv4} ${ADDRESSES_WITH_DEFAULT_ROUTE_IPv6}"
198
199 echo "${ADDRESSES_WITH_DEFAULT_ROUTE_IPv4} ${ADDRESSES_WITH_DEFAULT_ROUTE_IPv6}"