From 1f68407d9b34bed3535c56c371863a89c76291aa Mon Sep 17 00:00:00 2001 From: Hippolyte Chauvin Date: Sat, 11 Nov 2023 17:32:08 +0100 Subject: [PATCH] =?UTF-8?q?ssh-fwd=20:=20remaniement=20du=20syst=C3=A8me?= =?UTF-8?q?=20d'analyse=20des=20arguments?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bin/ssh-fwd | 37 +++++++++++++++++++++++++------------ 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/bin/ssh-fwd b/bin/ssh-fwd index a9ca57c..7e0f317 100755 --- a/bin/ssh-fwd +++ b/bin/ssh-fwd @@ -7,20 +7,10 @@ error() { error_null_arg() { local error - error="Variable \"${1}\" can't be null" + error="Argument \"${arg_name}\" can't be null" error 1 } -# Parser les variables -trap error_null_arg EXIT -set -e - -ssh_host="$1" -shift - -trap - EXIT -set +e - # Lancer la redirection SSH start_forwarding() { ssh \ @@ -28,7 +18,7 @@ start_forwarding() { -L "${local_port}:${target}:${distant_port}" \ "$@" \ "$ssh_host" -} + } # Supprimer la redirection SSH stop_forwarding() { @@ -42,9 +32,32 @@ is_forwarding_active() { | sed 's/.*://' | sort | uniq | grep -q "$local_port" } +step() { + printf '\033[1;32m*\033[0m \033[1m%s...\033[0m\n' "$*" +} + +# 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 + +set +e + # Branchement principal if ! is_forwarding_active; then + step "Starting the redirection" start_forwarding else + step "Stopping the redirection" stop_forwarding fi