ssh-fwd : prendre en compte les redirections en dehors d'un control

master
This commit is contained in:
Hippolyte Chauvin 2023-11-11 17:57:40 +01:00
parent 1f68407d9b
commit 6a905e39cf

View file

@ -1,5 +1,9 @@
#!/bin/bash #!/bin/bash
step() {
printf '\033[1;32m*\033[0m \033[1m%s...\033[0m\n' "$*"
}
error() { error() {
>&2 printf '\033[1;31mERROR:\033[0m \033[1m%s\033[0m\n' "$error" >&2 printf '\033[1;31mERROR:\033[0m \033[1m%s\033[0m\n' "$error"
return "$1" return "$1"
@ -18,22 +22,28 @@ start_forwarding() {
-L "${local_port}:${target}:${distant_port}" \ -L "${local_port}:${target}:${distant_port}" \
"$@" \ "$@" \
"$ssh_host" "$ssh_host"
} }
show_redirection_info() {
lsof -i -P -n | awk '($1 == "ssh" && $10 == "(LISTEN)" && $9 ~ /.*:'"${local_port}"'/) { print }'
}
get_ssh_pid() {
show_redirection_info | awk '{ print $2 }' | sort | uniq
}
# Supprimer la redirection SSH # Supprimer la redirection SSH
stop_forwarding() { stop_forwarding() {
start_forwarding -O cancel start_forwarding -O cancel
if [ "$?" -eq 255 ]; then
echo "Okay then, brutally killing SSH..."
kill "$(get_ssh_pid)"
fi
} }
# Retourner 0 si le port de la redirection est déjà utilisé par SSH # Retourner 0 si le port de la redirection est déjà utilisé par SSH
is_forwarding_active() { is_forwarding_active() {
lsof -i -P -n \ [ -n "$(show_redirection_info)" ]
| awk '($1 == "ssh" && $10 == "(LISTEN)") { print $9 }' \
| sed 's/.*://' | sort | uniq | grep -q "$local_port"
}
step() {
printf '\033[1;32m*\033[0m \033[1m%s...\033[0m\n' "$*"
} }
# Parser les variables # Parser les variables