vncsconnect : ré-écriture

This commit is contained in:
Hippolyte Chauvin 2023-11-11 18:43:05 +01:00
parent e7162095a1
commit 3efcb45263

View file

@ -1,11 +1,19 @@
#!/bin/bash
# Variables
declare -a ssh_args
vnc_host="$1"
if [ -n "$2" ]
then local_port="$2"
else local_port=9900
fi
if [ -n "$3" ]
then distant_port="$3"
else distant_port=5900
fi
[ -n "$4" ] && ssh_target="$4"
# Fonction
ssh_bridge_already_exists() {
[ -n "$(lsof -nP -i TCP -s TCP:LISTEN | awk '($1 == "ssh" && $9 ~ /.*:'"${1}"'/) { print }')" ]
[ -n "$(lsof -nP -i TCP -s TCP:LISTEN | awk '($1 == "ssh" && $9 ~ /.*:'"${local_port}"'/) { print }')" ]
}
# Sélectionner le viewer adapté à la session
@ -16,29 +24,15 @@ else
separator=:
fi
# Arguments
while [ -n "$1" ]; do
case "$1" in
--)
shift
break
;;
*)
ssh_args+=("$1")
shift
;;
esac
done
# Initialiser les arguments du viewer
args=(localhost "${ssh_args[1]}")
args=(localhost "$local_port")
if [ -n "$separator" ]; then
args=("${args[*]// /${separator}/}")
fi
# Exécution
if ! ssh_bridge_already_exists "${ssh_args[1]}"; then
ssh-fwd "${ssh_args[@]}" || exit
if ! ssh_bridge_already_exists; then
ssh-fwd "$vnc_host" "$local_port" "$distant_port" "$ssh_target" || exit
else
echo "Le pont SSH existe déjà !"
fi