| 1 |
#!/usr/bin/env bash |
| 2 |
# ____ ____ ____ _ _ _ |
| 3 |
# / ___| ___ | _ \ / ___|___ | | | ___ ___| |_ ___ _ __ |
| 4 |
# \___ \ / _ \| |_) | | | / _ \| | |/ _ \/ __| __/ _ \| '__| |
| 5 |
# ___) | (_) | __/ | |__| (_) | | | __/ (__| || (_) | | |
| 6 |
# |____/ \___/|_| \____\___/|_|_|\___|\___|\__\___/|_| |
| 7 |
# |
| 8 |
# HiPerConTracer Collector |
| 9 |
# Copyright (C) 2022-2026 by Thomas Dreibholz |
| 10 |
# |
| 11 |
# This program is free software: you can redistribute it and/or modify |
| 12 |
# it under the terms of the GNU General Public License as published by |
| 13 |
# the Free Software Foundation, either version 3 of the License, or |
| 14 |
# (at your option) any later version. |
| 15 |
# |
| 16 |
# This program is distributed in the hope that it will be useful, |
| 17 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 |
# GNU General Public License for more details. |
| 20 |
# |
| 21 |
# You should have received a copy of the GNU General Public License |
| 22 |
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 23 |
# |
| 24 |
# Contact: [email protected] |
| 25 |
|
| 26 |
# Bash options: |
| 27 |
set -eu |
| 28 |
|
| 29 |
|
| 30 |
# ###### Usage ############################################################## |
| 31 |
usage () { |
| 32 |
echo >&2 "Usage: $0 user@node_id [-h|--help] [--verbose|-v] [--quiet|-q] [--copy-id|-c] -- [ssh_arguments ...]" |
| 33 |
exit 1 |
| 34 |
} |
| 35 |
|
| 36 |
|
| 37 |
# ###### Main program ####################################################### |
| 38 |
|
| 39 |
# ====== Handle arguments =================================================== |
| 40 |
if [ $# -lt 1 ] ; then |
| 41 |
usage |
| 42 |
fi |
| 43 |
|
| 44 |
# shellcheck disable=SC2001 |
| 45 |
SSH_USER="$(echo "$1" | sed -e "s/@.*$//")" |
| 46 |
# shellcheck disable=SC2001 |
| 47 |
NODE_ID="$(echo "$1" | sed -e "s/^.*@//")" |
| 48 |
VERBOSE=1 |
| 49 |
COPY_ID=0 |
| 50 |
shift |
| 51 |
|
| 52 |
if [[ ! "${SSH_USER}" =~ ^[a-zA-Z0-9]+$ ]] ; then |
| 53 |
echo >&2 "ERROR: Invalid user ${SSH_USER}!" |
| 54 |
exit 1 |
| 55 |
fi |
| 56 |
if [[ ! "${NODE_ID}" =~ ^[0-9]+$ ]] ; then |
| 57 |
echo >&2 "ERROR: Invalid Node ID ${NODE_ID}!" |
| 58 |
exit 1 |
| 59 |
fi |
| 60 |
if [ "${NODE_ID}" -lt 1 ] || [ "${NODE_ID}" -gt 9999 ] ; then |
| 61 |
echo >&2 "ERROR: Invalid number for Node ID: ${NODE_ID}!" |
| 62 |
exit 1 |
| 63 |
fi |
| 64 |
|
| 65 |
GETOPT="$(PATH=/usr/local/bin:$PATH which getopt)" |
| 66 |
options="$(${GETOPT} -o hqvc --long help,quiet,verbose,copy-id -a -- "$@")" |
| 67 |
# shellcheck disable=SC2181 |
| 68 |
if [[ $? -ne 0 ]] ; then |
| 69 |
usage |
| 70 |
fi |
| 71 |
|
| 72 |
eval set -- "${options}" |
| 73 |
while [ $# -gt 0 ] ; do |
| 74 |
case "$1" in |
| 75 |
-c | --copy-id) |
| 76 |
COPY_ID=1 |
| 77 |
shift 2 |
| 78 |
;; |
| 79 |
-h | --help) |
| 80 |
usage |
| 81 |
;; |
| 82 |
-v | --verbose) |
| 83 |
VERBOSE=1 |
| 84 |
shift |
| 85 |
;; |
| 86 |
-q | --quiet) |
| 87 |
VERBOSE=0 |
| 88 |
shift |
| 89 |
;; |
| 90 |
--) |
| 91 |
shift |
| 92 |
break |
| 93 |
;; |
| 94 |
esac |
| 95 |
done |
| 96 |
|
| 97 |
|
| 98 |
# ====== SSH to node via Reverse Tunnel ===================================== |
| 99 |
port=$((10000+NODE_ID)) |
| 100 |
|
| 101 |
if [ ${COPY_ID} -eq 1 ] ; then |
| 102 |
echo "Copying ID to remote machine ..." |
| 103 |
ssh-copy-id -p ${port} "${SSH_USER}"@localhost && status=0 || status=$? |
| 104 |
if [ "${status}" -ne 0 ] ; then |
| 105 |
echo >&2 "ERROR: ssh-copy-id failed!" |
| 106 |
exit 1 |
| 107 |
fi |
| 108 |
fi |
| 109 |
|
| 110 |
if [ ${VERBOSE} -ne 0 ] ; then |
| 111 |
# shellcheck disable=SC2145 |
| 112 |
echo -e "\x1b[34m$(date +"%Y-%m-%d %H:%M:%S") Running SSH: ssh -p ${port} ${SSH_USER}@localhost $@\x1b[0m" |
| 113 |
fi |
| 114 |
# shellcheck disable=SC2068 |
| 115 |
ssh -p ${port} "${SSH_USER}"@localhost $@ && status=0 || status=$? |
| 116 |
if [ "${status}" -ne 0 ] ; then |
| 117 |
if [ $# -eq 0 ] ; then |
| 118 |
error="SSH failed with exit code ${status}." |
| 119 |
else |
| 120 |
# shellcheck disable=SC2124 |
| 121 |
error="SSH \"$@\" failed with exit code ${status}." |
| 122 |
fi |
| 123 |
echo -e "\x1b[34m$(date +"%Y-%m-%d %H:%M:%S") ${error}\x1b[0m" |
| 124 |
exit "${status}" |
| 125 |
else |
| 126 |
if [ ${VERBOSE} -ne 0 ] ; then |
| 127 |
echo -e "\x1b[34m$(date +"%Y-%m-%d %H:%M:%S") Done.\x1b[0m" |
| 128 |
fi |
| 129 |
fi |