dotfiles/bin/vncsconnect
2023-11-11 18:43:05 +01:00

40 lines
853 B
Bash
Executable file

#!/bin/bash
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 ~ /.*:'"${local_port}"'/) { print }')" ]
}
# Sélectionner le viewer adapté à la session
if [ -n "$WAYLAND_DISPLAY" ]; then
vncviewer=wlvncc
else
vncviewer=vncviewer
separator=:
fi
# Initialiser les arguments du viewer
args=(localhost "$local_port")
if [ -n "$separator" ]; then
args=("${args[*]// /${separator}/}")
fi
# Exécution
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
exec "$vncviewer" "${args[@]}"