From 974a2520586b808e1e1b878b6ae9a1822d3c0f77 Mon Sep 17 00:00:00 2001 From: Hippolyte Chauvin Date: Fri, 5 May 2023 20:10:39 +0200 Subject: [PATCH] =?UTF-8?q?V=C3=A9rifier=20si=20le=20port=20est=20libre=20?= =?UTF-8?q?avant=20de=20faire=20une=20connection=20SSH?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bin/ssh-fwd | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/bin/ssh-fwd b/bin/ssh-fwd index 465a704..9d5c356 100755 --- a/bin/ssh-fwd +++ b/bin/ssh-fwd @@ -14,11 +14,17 @@ Usage: $(basename "$0") SSH_HOST LOCAL_PORT DISTANT_PORT [TARGET] [-- SSH_OPTION EOF } +# Show an error function error { >&2 usage exit "${1:-1}" } +# Check if TCP port number is free +port_is_free() { + ! lsof -i -P -n | grep -q ':'"${1}"' (LISTEN)' +} + # Arguments while [[ -n $* ]]; do case "$1" in @@ -54,5 +60,9 @@ ssh_com=( # Execution set -x -"${ssh_com[@]}" +if port_is_free "${args[1]}"; then + "${ssh_com[@]}" +else + echo "Port is not free, skipping." +fi