1
0
Fork 0
forked from ahurac/dotfiles
ahuarc-dotfiles/bin/ssh-fwd
2023-06-17 19:04:53 +02:00

67 lines
1.2 KiB
Bash
Executable file

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