dotfiles/bin/vncsconnect

41 lines
853 B
Text
Raw Normal View History

2023-04-13 00:04:20 +02:00
#!/bin/bash
2023-11-11 18:43:05 +01:00
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"
2023-04-13 00:04:20 +02:00
# Fonction
ssh_bridge_already_exists() {
2023-11-11 18:43:05 +01:00
[ -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
2023-04-13 00:04:20 +02:00
# Initialiser les arguments du viewer
2023-11-11 18:43:05 +01:00
args=(localhost "$local_port")
if [ -n "$separator" ]; then
args=("${args[*]// /${separator}/}")
fi
2023-04-13 00:04:20 +02:00
# Exécution
2023-11-11 18:43:05 +01:00
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
2023-04-13 00:04:20 +02:00
exec "$vncviewer" "${args[@]}"