Vérifier si le port est libre avant de faire une connection SSH
This commit is contained in:
parent
84bb3604e1
commit
974a252058
1 changed files with 11 additions and 1 deletions
12
bin/ssh-fwd
12
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
|
||||
|
||||
|
|
Loading…
Reference in a new issue