| 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 user@node_id [-c|--copy-id] [-q|--quiet] [-w|--verbose] [-h|--help] [-v|--version] -- [ssh_arguments ...]" |
| 39 | exit "${exitCode}" |
| 40 | } |
| 41 | |
| 42 | |
| 43 | # ###### Version ############################################################ |
| 44 | version () { |
| 45 | echo "HiPerConTracer-SSH @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 cqwhv --long copy-id,quiet,verbose,help,version -a -- "$@")" |
| 81 | # shellcheck disable=SC2181 |
| 82 | if [[ $? -ne 0 ]] ; then |
| 83 | usage 1 |
| 84 | fi |
| 85 | |
| 86 | VERBOSE=1 |
| 87 | COPY_ID=0 |
| 88 | eval set -- "${options}" |
| 89 | while [ $# -gt 0 ] ; do |
| 90 | case "$1" in |
| 91 | -c | --copy-id) |
| 92 | COPY_ID=1 |
| 93 | ;; |
| 94 | -q | --quiet) |
| 95 | VERBOSE=0 |
| 96 | ;; |
| 97 | -w | --verbose) |
| 98 | VERBOSE=1 |
| 99 | ;; |
| 100 | -h | --help) |
| 101 | usage 0 |
| 102 | ;; |
| 103 | -v | --version) |
| 104 | version |
| 105 | ;; |
| 106 | --) |
| 107 | shift |
| 108 | break |
| 109 | ;; |
| 110 | *) |
| 111 | # This should not happen: wrong getopt parameters, or missing case? |
| 112 | echo >&2 "INTERNAL ERROR: Unhandled option $1!" |
| 113 | exit 1 |
| 114 | ;; |
| 115 | esac |
| 116 | shift |
| 117 | done |
| 118 | if [ $# -ne 1 ] ; then |
| 119 | usage 1 |
| 120 | fi |
| 121 | |
| 122 | # shellcheck disable=SC2001 |
| 123 | SSH_USER="$(echo "$1" | sed -e "s/@.*$//")" |
| 124 | if [[ ! "${SSH_USER}" =~ ^[a-zA-Z0-9_.-]+$ ]] ; then |
| 125 | echo >&2 "ERROR: Invalid user ${SSH_USER}!" |
| 126 | exit 1 |
| 127 | fi |
| 128 | |
| 129 | # shellcheck disable=SC2001 |
| 130 | NODE_ID="$(echo "$1" | sed -e "s/^.*@//")" |
| 131 | if [[ ! "${NODE_ID}" =~ ^[0-9]+$ ]] ; then |
| 132 | echo >&2 "ERROR: Invalid Node ID ${NODE_ID}!" |
| 133 | exit 1 |
| 134 | fi |
| 135 | if [ "${NODE_ID}" -lt 1 ] || [ "${NODE_ID}" -gt 9999 ] ; then |
| 136 | echo >&2 "ERROR: Invalid number for Node ID: ${NODE_ID}!" |
| 137 | exit 1 |
| 138 | fi |
| 139 | |
| 140 | |
| 141 | # ====== SSH to node via Reverse Tunnel ===================================== |
| 142 | port=$((10000+NODE_ID)) |
| 143 | |
| 144 | if [ ${COPY_ID} -eq 1 ] ; then |
| 145 | echo "Copying ID to remote machine ..." |
| 146 | ssh-copy-id -p ${port} "${SSH_USER}"@localhost && status=0 || status=$? |
| 147 | if [ "${status}" -ne 0 ] ; then |
| 148 | echo >&2 "ERROR: ssh-copy-id failed!" |
| 149 | exit 1 |
| 150 | fi |
| 151 | fi |
| 152 | |
| 153 | if [ ${VERBOSE} -ne 0 ] ; then |
| 154 | # shellcheck disable=SC2145 |
| 155 | echo -e "\x1b[34m$(date +"%Y-%m-%d %H:%M:%S") Running SSH: ssh -p ${port} ${SSH_USER}@localhost $@\x1b[0m" |
| 156 | fi |
| 157 | ssh -p ${port} "${SSH_USER}"@localhost "$@" && status=0 || status=$? |
| 158 | if [ "${status}" -ne 0 ] ; then |
| 159 | if [ $# -eq 0 ] ; then |
| 160 | error="SSH failed with exit code ${status}." |
| 161 | else |
| 162 | # shellcheck disable=SC2124 |
| 163 | error="SSH \"$@\" failed with exit code ${status}." |
| 164 | fi |
| 165 | echo -e "\x1b[34m$(date +"%Y-%m-%d %H:%M:%S") ${error}\x1b[0m" |
| 166 | exit "${status}" |
| 167 | else |
| 168 | if [ ${VERBOSE} -ne 0 ] ; then |
| 169 | echo -e "\x1b[34m$(date +"%Y-%m-%d %H:%M:%S") Done.\x1b[0m" |
| 170 | fi |
| 171 | fi |