Debian Codegraph @

src/hpct-ssh

raw symbols SCIP
1 #!/usr/bin/envscip-shell filesystem . . `/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 usagescip-shell shell shell-project 0.0.0 usage(). () {
37 local exitCode="$1"
38 echoscip-shell system . . 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 versionscip-shell shell shell-project 0.0.0 version(). () {
45 echoscip-shell system . . 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 echoscip-shell system . . 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="$(unamescip-shell system . . uname.)"
59 case "${UNAME}" in
60 FreeBSD)
61 GETOPT="$(PATH="/usr/local/bin:${PATH}" command -v getopt || truescip-shell system . . true.)"
62 ;;
63 NetBSD)
64 GETOPT="$(PATH="/usr/pkg/bin:${PATH}" command -v getopt || truescip-shell system . . true.)"
65 ;;
66 OpenBSD)
67 GETOPT="$(command -v gnugetopt || truescip-shell system . . true.)"
68 ;;
69 Darwin)
70 GETOPT="$(envscip-shell system . . env. PATH="/opt/homebrew/opt/gnu-getopt/bin:${PATH}" command -v getopt || truescip-shell system . . true.)"
71 ;;
72 *)
73 GETOPT="$(command -v getopt || truescip-shell system . . true.)"
74 ;;
75 esac
76 if "${GETOPT}" -T >/dev/nullscip-shell filesystem . . `/dev/null`. 2>&1 || [[ $? -ne 4 ]]; then
77 echoscip-shell system . . 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 usagescip-shell shell shell-project 0.0.0 usage(). 1
84 fi
85
86 VERBOSE=1
87 COPY_ID=0
88 eval set -- "${options}"
89 while [scip-shell system . . `[`. $# -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 usagescip-shell shell shell-project 0.0.0 usage(). 0
102 ;;
103 -v | --version)
104 versionscip-shell shell shell-project 0.0.0 version().
105 ;;
106 --)
107 shift
108 break
109 ;;
110 *)
111 # This should not happen: wrong getopt parameters, or missing case?
112 echoscip-shell system . . echo. >&2 "INTERNAL ERROR: Unhandled option $1!"
113 exit 1
114 ;;
115 esac
116 shift
117 done
118 if [scip-shell system . . `[`. $# -ne 1 ] ; then
119 usagescip-shell shell shell-project 0.0.0 usage(). 1
120 fi
121
122 # shellcheck disable=SC2001
123 SSH_USER="$(echoscip-shell system . . echo. "$1" | sedscip-shell system . . sed. -e "s/@.*$//")"
124 if [[ ! "${SSH_USER}" =~ ^[a-zA-Z0-9_.-]+$ ]] ; then
125 echoscip-shell system . . echo. >&2 "ERROR: Invalid user ${SSH_USER}!"
126 exit 1
127 fi
128
129 # shellcheck disable=SC2001
130 NODE_ID="$(echoscip-shell system . . echo. "$1" | sedscip-shell system . . sed. -e "s/^.*@//")"
131 if [[ ! "${NODE_ID}" =~ ^[0-9]+$ ]] ; then
132 echoscip-shell system . . echo. >&2 "ERROR: Invalid Node ID ${NODE_ID}!"
133 exit 1
134 fi
135 if [scip-shell system . . `[`. "${NODE_ID}" -lt 1 ] || [scip-shell system . . `[`. "${NODE_ID}" -gt 9999 ] ; then
136 echoscip-shell system . . 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 [scip-shell system . . `[`. ${COPY_ID} -eq 1 ] ; then
145 echoscip-shell system . . echo. "Copying ID to remote machine ..."
146 ssh-copy-idscip-shell system . . ssh-copy-id. -p ${port} "${SSH_USER}"@localhost && status=0 || status=$?
147 if [scip-shell system . . `[`. "${status}" -ne 0 ] ; then
148 echoscip-shell system . . echo. >&2 "ERROR: ssh-copy-id failed!"
149 exit 1
150 fi
151 fi
152
153 if [scip-shell system . . `[`. ${VERBOSE} -ne 0 ] ; then
154 # shellcheck disable=SC2145
155 echoscip-shell system . . echo. -e "\x1b[34m$(datescip-shell system . . date. +"%Y-%m-%d %H:%M:%S") Running SSH: ssh -p ${port} ${SSH_USER}@localhost $@\x1b[0m"
156 fi
157 sshscip-shell system . . ssh. -p ${port} "${SSH_USER}"@localhost "$@" && status=0 || status=$?
158 if [scip-shell system . . `[`. "${status}" -ne 0 ] ; then
159 if [scip-shell system . . `[`. $# -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 echoscip-shell system . . echo. -e "\x1b[34m$(datescip-shell system . . date. +"%Y-%m-%d %H:%M:%S") ${error}\x1b[0m"
166 exit "${status}"
167 else
168 if [scip-shell system . . `[`. ${VERBOSE} -ne 0 ] ; then
169 echoscip-shell system . . echo. -e "\x1b[34m$(datescip-shell system . . date. +"%Y-%m-%d %H:%M:%S") Done.\x1b[0m"
170 fi
171 fi