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