2023-06-17 19:04:53 +02:00
|
|
|
#!/bin/bash
|
2023-04-13 00:04:20 +02:00
|
|
|
|
2023-06-17 19:04:53 +02:00
|
|
|
# Alias si le terminal est kitty
|
|
|
|
if [ "$TERM" = xterm-kitty ]; then
|
|
|
|
ssh=(kitty +kitten ssh)
|
|
|
|
else
|
|
|
|
ssh=(ssh)
|
|
|
|
fi
|
2023-04-13 00:04:20 +02:00
|
|
|
|
|
|
|
# Variables
|
|
|
|
declare -a args
|
|
|
|
|
2023-06-17 19:04:53 +02:00
|
|
|
# Foncitons
|
|
|
|
error() {
|
|
|
|
>&2 printf '\033[1;31m%s\033[0m \033[1m%s\033[0m\n' ERROR: "$*"
|
|
|
|
}
|
|
|
|
|
|
|
|
help() {
|
|
|
|
local name
|
|
|
|
name="$(basename "$0")"
|
2023-04-13 00:04:20 +02:00
|
|
|
cat << EOF
|
2023-06-17 19:04:53 +02:00
|
|
|
${name}: utilisation:
|
|
|
|
${name} SERVEUR PORT_LOCAL PORT_DISTANT [CIBLE] [-- ARGUMENTS_SSH...]
|
2023-04-13 00:04:20 +02:00
|
|
|
EOF
|
|
|
|
}
|
|
|
|
|
2023-06-17 19:04:53 +02:00
|
|
|
error_help() {
|
|
|
|
error "Invalid usage"
|
|
|
|
>&2 help
|
|
|
|
exit 1
|
2023-04-13 00:04:20 +02:00
|
|
|
}
|
|
|
|
|
2023-06-17 19:04:53 +02:00
|
|
|
requested_port_bridge_pid() {
|
|
|
|
lsof -nP -i TCP -s TCP:LISTEN | awk '($1 == "ssh" && $9 ~ /.*:'"${1}"'/) { print $2 }' | uniq | head -n 1
|
2023-05-05 20:10:39 +02:00
|
|
|
}
|
|
|
|
|
2023-06-17 19:04:53 +02:00
|
|
|
# Analyser les arguments
|
|
|
|
while [ -n "$1" ]; do
|
2023-04-13 00:04:20 +02:00
|
|
|
case "$1" in
|
2023-06-17 19:04:53 +02:00
|
|
|
--)
|
2023-06-17 19:38:33 +02:00
|
|
|
shift
|
2023-06-17 19:04:53 +02:00
|
|
|
break
|
|
|
|
;;
|
2023-04-13 00:04:20 +02:00
|
|
|
*)
|
|
|
|
args+=("$1")
|
|
|
|
shift
|
2023-06-17 19:04:53 +02:00
|
|
|
;;
|
2023-04-13 00:04:20 +02:00
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
2023-06-17 19:04:53 +02:00
|
|
|
# Il doit y avoir au moins 3 arguments dans le tableau
|
|
|
|
if [ "${#args[@]}" -lt 3 ]; then
|
|
|
|
error_help
|
|
|
|
fi
|
2023-04-13 00:04:20 +02:00
|
|
|
|
2023-06-17 19:04:53 +02:00
|
|
|
pid="$(requested_port_bridge_pid "${args[1]}")"
|
|
|
|
if [ -z "$pid" ]; then
|
|
|
|
exec "${ssh[@]}" \
|
|
|
|
-f \
|
|
|
|
-N \
|
|
|
|
-L "${args[1]}:${args[3]:-localhost}:${args[2]}" \
|
2023-06-17 19:38:33 +02:00
|
|
|
"${args[0]}" \
|
|
|
|
"$@"
|
2023-06-17 19:04:53 +02:00
|
|
|
else
|
|
|
|
echo "Suppression de la redirection..."
|
|
|
|
kill "$pid"
|
|
|
|
fi
|
2023-04-13 00:04:20 +02:00
|
|
|
|