Debian Codegraph @

src/get-default-ips

raw
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 -eu
33
34
35 # ###### Main program #######################################################
36
37 # ====== Find all interfaces with configured default route ==================
38 OS=$(uname)
39 if [ "${OS}" == "Linux" ] ; then
40 INTERFACES_WITH_DEFAULT_ROUTE_IPv4=$(/sbin/ip -4 route show default | (
41 while read -r line ; do
42 if [[ "${line}" =~ ^(default)([[:space:]]*)(.*)(dev)([[:space:]]*)([^[:space:]]*)([[:space:]]*) ]] ; then
43 echo "${BASH_REMATCH[6]}"
44 fi
45 done
46 ) | sort -u | xargs)
47 INTERFACES_WITH_DEFAULT_ROUTE_IPv6=$(/sbin/ip -6 route show default | (
48 while read -r line ; do
49 if [[ "${line}" =~ ^(default)([[:space:]]*)(.*)(dev)([[:space:]]*)([^[:space:]]*)([[:space:]]*) ]] ; then
50 echo "${BASH_REMATCH[6]}"
51 fi
52 done
53 ) | sort -u | xargs)
54 elif [ "${OS}" == "FreeBSD" ] ; then
55 INTERFACES_WITH_DEFAULT_ROUTE_IPv4=$(netstat -rn -f inet | grep ^default | awk '{ print $4 }' | sort -u | xargs)
56 INTERFACES_WITH_DEFAULT_ROUTE_IPv6=$(netstat -rn -f inet6 | grep ^default | awk '{ print $4 }' | sort -u | xargs)
57 else
58 echo >&2 "ERROR: Unsupported operating system ${OS}!"
59 exit 1
60 fi
61
62 # echo "I: ${INTERFACES_WITH_DEFAULT_ROUTE_IPv4} ${INTERFACES_WITH_DEFAULT_ROUTE_IPv6}"
63
64
65 # ====== Get addresses of these interfaces with global scope ================
66 if [ "${OS}" == "Linux" ] ; then
67 ADDRESSES_WITH_DEFAULT_ROUTE_IPv4=$(
68 ( for interface in ${INTERFACES_WITH_DEFAULT_ROUTE_IPv4} ; do
69 ip -4 addr show dev "${interface}" scope global | (
70 while read -r line ; do
71 if [[ "${line}" =~ ^([[:space:]]*)(inet)([[:space:]]*)([^[:space:]]*)/ ]] ; then
72 address="${BASH_REMATCH[4]}"
73 echo "${address}"
74 fi
75 done
76 )
77 done
78 ) | sort -u | xargs)
79 ADDRESSES_WITH_DEFAULT_ROUTE_IPv6=$(
80 ( for interface in ${INTERFACES_WITH_DEFAULT_ROUTE_IPv6} ; do
81 ip -6 addr show dev "${interface}" scope global | (
82 while read -r line ; do
83 if [[ "${line}" =~ ^([[:space:]]*)(inet6)([[:space:]]*)([^[:space:]]*)/ ]] ; then
84 address="${BASH_REMATCH[4]}"
85 echo "${address}"
86 fi
87 done
88 )
89 done
90 ) | sort -u | xargs)
91 elif [ "${OS}" == "FreeBSD" ] ; then
92 ADDRESSES_WITH_DEFAULT_ROUTE_IPv4=$(
93 ( for interface in ${INTERFACES_WITH_DEFAULT_ROUTE_IPv4} ; do
94 ifconfig "${interface}" | (
95 while read -r line ; do
96 if [[ "${line}" =~ ^([[:space:]]*)(inet)([[:space:]]*)([^[:space:]]*)([[:space:]]*) ]] ; then
97 address="${BASH_REMATCH[4]}"
98 if [[ ! "${line}" =~ scopeid ]] ; then
99 echo "${address}"
100 fi
101 fi
102 done
103 )
104 done
105 ) | sort -u | xargs)
106 ADDRESSES_WITH_DEFAULT_ROUTE_IPv6=$(
107 ( for interface in ${INTERFACES_WITH_DEFAULT_ROUTE_IPv6} ; do
108 ifconfig "${interface}" | (
109 while read -r line ; do
110 if [[ "${line}" =~ ^([[:space:]]*)(inet6)([[:space:]]*)([^[:space:]]*)([[:space:]]*) ]] ; then
111 address="${BASH_REMATCH[4]}"
112 if [[ ! "${line}" =~ scopeid ]] ; then
113 echo "${address}"
114 fi
115 fi
116 done
117 )
118 done
119 ) | sort -u | xargs)
120 fi
121
122 # echo "A: ${ADDRESSES_WITH_DEFAULT_ROUTE_IPv4} ${ADDRESSES_WITH_DEFAULT_ROUTE_IPv6}"
123
124 echo "${ADDRESSES_WITH_DEFAULT_ROUTE_IPv4} ${ADDRESSES_WITH_DEFAULT_ROUTE_IPv6}"