#!/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 set -euo pipefail # ###### Usage ############################################################## usage () { local exitCode="$1" echo >&2 "Usage: $0 [-R|--remote remote_location] [-C|--collector collector] [-N|--nodeid node_id] [-K|--key ssh_private_key_file] [-q|--quiet] [-w|--verbose] [-h|--help] [-v|--version]" exit "${exitCode}" } # ###### Version ############################################################ version () { echo "HiPerConTracer-RTunnel @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 N:R:C:K:H:qwhv --long nodeid:,remote:,collector:,key:,known-hosts:,quiet,verbose,help,version -a -- "$@")" # shellcheck disable=SC2181 if [[ $? -ne 0 ]]; then usage 1 fi NODE_ID= REMOTE="/var/hipercontracer/data" COLLECTOR= KEY="/var/hipercontracer/ssh/id_ed25519" KNOWN_HOSTS="/var/hipercontracer/ssh/known_hosts" VERBOSE=1 eval set -- "${options}" while [ $# -gt 0 ] ; do case "$1" in -N | --nodeid) NODE_ID="$2" shift ;; -R | --remote) REMOTE="$2" shift ;; -C | --collector) COLLECTOR="$2" shift ;; -K | --key) KEY="$2" shift ;; -H | --known-hosts) KNOWN_HOSTS="$2" shift ;; -q | --quiet) VERBOSE=0 ;; -w | --verbose) VERBOSE=1 ;; -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 if [ "${COLLECTOR}" == "" ] ; then usage 1 fi if [ ! -e "${KEY}" ] ; then echo >&2 "ERROR: SSH private key file ${KEY} does not exist!" exit 1 fi if [[ ! "${NODE_ID}" =~ [0-9]+ ]] ; then echo >&2 "ERROR: No or invalid Node ID specified!" exit 1 fi if [ "${NODE_ID}" -lt 1 ] || [ "${NODE_ID}" -gt 9999 ] ; then echo >&2 "ERROR: Invalid number for Node ID: ${NODE_ID}!" exit 1 fi # ====== Obtain node status ================================================= NAME="Unknown" VERSION="?.?" . /etc/os-release # Getting the version of hipercontracer-common here, since it is a dependency of # hpct-rtunnel. So, it should have been upgraded earlier. if [ -x /usr/bin/dpkg ] ; then nodePackageVersion="$(dpkg -s hipercontracer-common 2>/dev/null | grep '^Version: ' | sed -e "s/^Version: //" || echo "N/A")" elif [ -x /usr/bin/rpm ] ; then nodePackageVersion="$(rpm -q hipercontracer-common | sed -e "s/^hipercontracer-common-//" || echo "N/A")" else nodePackageVersion="" fi nodeStatus="\ lsb_release: ${NAME} ${VERSION} node_package_version: ${nodePackageVersion} node_uptime: $(uptime -s) tunnel_uptime: $(date -u +"%Y-%m-%d %H:%M:%S")" # ====== Establish reverse tunnel =========================================== sshOptions="-i ${KEY} -oUserKnownHostsFile=${KNOWN_HOSTS}" user="node${NODE_ID}" port=$((10000+NODE_ID)) lastSeen="${REMOTE}/${user}/last-seen.txt" if [ ${VERBOSE} -ne 0 ] ; then echo -e "\x1b[34m$(date +"%Y-%m-%d %H:%M:%S") Running SSH (ssh -tt -R ${port}:localhost:22 ${sshOptions} ${user}@${COLLECTOR}) ...\x1b[0m" echo "Private key file: ${KEY}" echo "Known hosts file: ${KNOWN_HOSTS}" fi # ping -c1 ${COLLECTOR} || true # shellcheck disable=SC2086 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" \ /dev/null 2>&1 && status=0 || status=$? if [ "${status}" -ne 0 ] ; then echo -e "\x1b[34m$(date +"%Y-%m-%d %H:%M:%S") SSH failed!\x1b[0m" exit 1 else if [ ${VERBOSE} -ne 0 ] ; then echo -e "\x1b[34m$(date +"%Y-%m-%d %H:%M:%S") Done.\x1b[0m" fi fi