Compare commits
6 commits
9bf24ee5d1
...
6a905e39cf
Author | SHA1 | Date | |
---|---|---|---|
|
6a905e39cf | ||
|
1f68407d9b | ||
|
7297666d6c | ||
|
f1395291d7 | ||
|
fcdfc805a2 | ||
|
9ff68dda52 |
1 changed files with 56 additions and 51 deletions
107
bin/ssh-fwd
107
bin/ssh-fwd
|
@ -1,68 +1,73 @@
|
|||
#!/bin/bash
|
||||
|
||||
# Alias si le terminal est kitty
|
||||
if [ "$TERM" = xterm-kitty ]; then
|
||||
ssh=(kitty +kitten ssh)
|
||||
else
|
||||
ssh=(ssh)
|
||||
fi
|
||||
step() {
|
||||
printf '\033[1;32m*\033[0m \033[1m%s...\033[0m\n' "$*"
|
||||
}
|
||||
|
||||
# Variables
|
||||
declare -a args
|
||||
|
||||
# Foncitons
|
||||
error() {
|
||||
>&2 printf '\033[1;31m%s\033[0m \033[1m%s\033[0m\n' ERROR: "$*"
|
||||
>&2 printf '\033[1;31mERROR:\033[0m \033[1m%s\033[0m\n' "$error"
|
||||
return "$1"
|
||||
}
|
||||
|
||||
help() {
|
||||
local name
|
||||
name="$(basename "$0")"
|
||||
cat << EOF
|
||||
${name}: utilisation:
|
||||
${name} SERVEUR PORT_LOCAL PORT_DISTANT [CIBLE] [-- ARGUMENTS_SSH...]
|
||||
EOF
|
||||
error_null_arg() {
|
||||
local error
|
||||
error="Argument \"${arg_name}\" can't be null"
|
||||
error 1
|
||||
}
|
||||
|
||||
error_help() {
|
||||
error "Invalid usage"
|
||||
>&2 help
|
||||
exit 1
|
||||
# Lancer la redirection SSH
|
||||
start_forwarding() {
|
||||
ssh \
|
||||
-f -N \
|
||||
-L "${local_port}:${target}:${distant_port}" \
|
||||
"$@" \
|
||||
"$ssh_host"
|
||||
}
|
||||
|
||||
requested_port_bridge_pid() {
|
||||
lsof -nP -i TCP -s TCP:LISTEN | awk '($1 == "ssh" && $9 ~ /.*:'"${1}"'/) { print $2 }' | uniq | head -n 1
|
||||
show_redirection_info() {
|
||||
lsof -i -P -n | awk '($1 == "ssh" && $10 == "(LISTEN)" && $9 ~ /.*:'"${local_port}"'/) { print }'
|
||||
}
|
||||
|
||||
# Analyser les arguments
|
||||
while [ -n "$1" ]; do
|
||||
case "$1" in
|
||||
--)
|
||||
shift
|
||||
break
|
||||
;;
|
||||
*)
|
||||
args+=("$1")
|
||||
shift
|
||||
;;
|
||||
esac
|
||||
done
|
||||
get_ssh_pid() {
|
||||
show_redirection_info | awk '{ print $2 }' | sort | uniq
|
||||
}
|
||||
|
||||
# Il doit y avoir au moins 3 arguments dans le tableau
|
||||
if [ "${#args[@]}" -lt 3 ]; then
|
||||
error_help
|
||||
# Supprimer la redirection SSH
|
||||
stop_forwarding() {
|
||||
start_forwarding -O cancel
|
||||
if [ "$?" -eq 255 ]; then
|
||||
echo "Okay then, brutally killing SSH..."
|
||||
kill "$(get_ssh_pid)"
|
||||
fi
|
||||
}
|
||||
|
||||
# Retourner 0 si le port de la redirection est déjà utilisé par SSH
|
||||
is_forwarding_active() {
|
||||
[ -n "$(show_redirection_info)" ]
|
||||
}
|
||||
|
||||
# Parser les variables
|
||||
trap error_null_arg EXIT
|
||||
set -e
|
||||
|
||||
arg_name='SSH host' ; ssh_host="$1" ; shift
|
||||
arg_name='local port' ; local_port="$1" ; shift
|
||||
arg_name='distant port'; distant_port="$1"; shift
|
||||
trap - EXIT
|
||||
unset arg_name
|
||||
|
||||
if [ -n "$1" ]
|
||||
then target="$1"
|
||||
else target=localhost
|
||||
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]}" \
|
||||
"$@"
|
||||
set +e
|
||||
|
||||
# Branchement principal
|
||||
if ! is_forwarding_active; then
|
||||
step "Starting the redirection"
|
||||
start_forwarding
|
||||
else
|
||||
echo "Suppression de la redirection..."
|
||||
kill "$pid"
|
||||
step "Stopping the redirection"
|
||||
stop_forwarding
|
||||
fi
|
||||
|
||||
|
|
Loading…
Reference in a new issue