Debian Codegraph @

src/hpct-rtunnel

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 set -eu
32
33
34 # ###### Usage ##############################################################
35 usage () {
36 echo >&2 "Usage: $0 [-h|--help] [--verbose|-v] [--quiet|-q] [--remote|-R remote_location] [--collector|-C collector] [--nodeid|-N node_id] [--key|-K ssh_private_key_file]"
37 exit 1
38 }
39
40
41 # ###### Main program #######################################################
42
43 # ====== Handle arguments ===================================================
44 GETOPT="$(PATH=/usr/local/bin:$PATH which getopt)"
45 options="$(${GETOPT} -o hN:R:C:K:H:vq --long help,nodeid:,remote:,collector:,key:,known-hosts:,verbose,quiet -a -- "$@")"
46 # shellcheck disable=SC2181
47 if [[ $? -ne 0 ]]; then
48 usage
49 fi
50
51 NODE_ID=
52 REMOTE="/var/hipercontracer/data"
53 COLLECTOR=
54 KEY="/var/hipercontracer/ssh/id_ed25519"
55 KNOWN_HOSTS="/var/hipercontracer/ssh/known_hosts"
56 VERBOSE=1
57
58 eval set -- "${options}"
59 while [ $# -gt 0 ] ; do
60 case "$1" in
61 -N | --nodeid)
62 NODE_ID="$2"
63 shift 2
64 ;;
65 -R | --remote)
66 REMOTE="$2"
67 shift 2
68 ;;
69 -C | --collector)
70 COLLECTOR="$2"
71 shift 2
72 ;;
73 -K | --key)
74 KEY="$2"
75 shift 2
76 ;;
77 -H | --known-hosts)
78 KNOWN_HOSTS="$2"
79 shift 2
80 ;;
81 -h | --help)
82 usage
83 ;;
84 -v | --verbose)
85 VERBOSE=1
86 shift
87 ;;
88 -q | --quiet)
89 VERBOSE=0
90 shift
91 ;;
92 --)
93 shift
94 break
95 ;;
96 esac
97 done
98
99 if [ "${COLLECTOR}" == "" ] ; then
100 usage
101 fi
102 if [ ! -e "${KEY}" ] ; then
103 echo >&2 "ERROR: SSH private key file ${KEY} does not exist!"
104 exit 1
105 fi
106
107 if [[ ! "${NODE_ID}" =~ [0-9]+ ]] ; then
108 echo >&2 "ERROR: No or invalid Node ID specified!"
109 exit 1
110 fi
111 if [ "${NODE_ID}" -lt 1 ] || [ "${NODE_ID}" -gt 9999 ] ; then
112 echo >&2 "ERROR: Invalid number for Node ID: ${NODE_ID}!"
113 exit 1
114 fi
115
116
117 # ====== Obtain node status =================================================
118 NAME="Unknown"
119 VERSION="?.?"
120 . /etc/os-release
121 # Getting the version of hipercontracer-common here, since it is a dependency of
122 # hpct-rtunnel. So, it should have been upgraded earlier.
123 if [ -x /usr/bin/dpkg ] ; then
124 nodePackageVersion="$(dpkg -s hipercontracer-common 2>/dev/null | grep '^Version: ' | sed -e "s/^Version: //" || echo "N/A")"
125 elif [ -x /usr/bin/rpm ] ; then
126 nodePackageVersion="$(rpm -q hipercontracer-common | sed -e "s/^hipercontracer-common-//" || echo "N/A")"
127 else
128 nodePackageVersion=""
129 fi
130 nodeStatus="\
131 lsb_release: ${NAME} ${VERSION}
132 node_package_version: ${nodePackageVersion}
133 node_uptime: $(uptime -s)
134 tunnel_uptime: $(date -u +"%Y-%m-%d %H:%M:%S")"
135
136
137 # ====== Establish reverse tunnel ===========================================
138 sshOptions="-i ${KEY} -oUserKnownHostsFile=${KNOWN_HOSTS}"
139 user="node${NODE_ID}"
140 port=$((10000+NODE_ID))
141 lastSeen="${REMOTE}/${user}/last-seen.txt"
142
143 if [ ${VERBOSE} -ne 0 ] ; then
144 echo -e "\x1b[34m$(date +"%Y-%m-%d %H:%M:%S") Running SSH (ssh -tt -R ${port}:localhost:22 ${sshOptions} ${user}@${COLLECTOR}) ...\x1b[0m"
145 echo "Private key file: ${KEY}"
146 echo "Known hosts file: ${KNOWN_HOSTS}"
147 fi
148
149 # ping -c1 ${COLLECTOR} || true
150
151 # shellcheck disable=SC2086
152 ssh -tt -R "${port}:localhost:22" ${sshOptions} "${user}"@"${COLLECTOR}" "while [ true ] ; do ( date -u +\"%Y-%m-%d %H:%M:%S\" ; echo \"${nodeStatus}\" ) >${lastSeen}.tmp && mv ${lastSeen}.tmp ${lastSeen} ; sleep 255 ; done" \
153 </dev/null >/dev/null 2>&1 && status=0 || status=$?
154 if [ "${status}" -ne 0 ] ; then
155 echo -e "\x1b[34m$(date +"%Y-%m-%d %H:%M:%S") SSH failed!\x1b[0m"
156 exit 1
157 else
158 if [ ${VERBOSE} -ne 0 ] ; then
159 echo -e "\x1b[34m$(date +"%Y-%m-%d %H:%M:%S") Done.\x1b[0m"
160 fi
161 fi