Vérifier si le port est libre avant de faire une connection SSH

This commit is contained in:
Hippolyte Chauvin 2023-05-05 20:10:39 +02:00
parent 84bb3604e1
commit 974a252058

View file

@ -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