From 6a905e39cfcd20510d5757e25eb6bb63ab90358a Mon Sep 17 00:00:00 2001 From: Hippolyte Chauvin Date: Sat, 11 Nov 2023 17:57:40 +0100 Subject: [PATCH] ssh-fwd : prendre en compte les redirections en dehors d'un control master --- bin/ssh-fwd | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/bin/ssh-fwd b/bin/ssh-fwd index 7e0f317..a79db75 100755 --- a/bin/ssh-fwd +++ b/bin/ssh-fwd @@ -1,5 +1,9 @@ #!/bin/bash +step() { + printf '\033[1;32m*\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" @@ -18,22 +22,28 @@ start_forwarding() { -L "${local_port}:${target}:${distant_port}" \ "$@" \ "$ssh_host" - } +} + +show_redirection_info() { + lsof -i -P -n | awk '($1 == "ssh" && $10 == "(LISTEN)" && $9 ~ /.*:'"${local_port}"'/) { print }' +} + +get_ssh_pid() { + show_redirection_info | awk '{ print $2 }' | sort | uniq +} # 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() { - lsof -i -P -n \ - | awk '($1 == "ssh" && $10 == "(LISTEN)") { print $9 }' \ - | sed 's/.*://' | sort | uniq | grep -q "$local_port" -} - -step() { - printf '\033[1;32m*\033[0m \033[1m%s...\033[0m\n' "$*" + [ -n "$(show_redirection_info)" ] } # Parser les variables