1
0
Fork 0
forked from ahurac/dotfiles

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 #!/bin/bash
# Variables vnc_host="$1"
declare -a ssh_args 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 # Fonction
ssh_bridge_already_exists() { 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 # Sélectionner le viewer adapté à la session
@ -16,29 +24,15 @@ else
separator=: separator=:
fi fi
# Arguments
while [ -n "$1" ]; do
case "$1" in
--)
shift
break
;;
*)
ssh_args+=("$1")
shift
;;
esac
done
# Initialiser les arguments du viewer # Initialiser les arguments du viewer
args=(localhost "${ssh_args[1]}") args=(localhost "$local_port")
if [ -n "$separator" ]; then if [ -n "$separator" ]; then
args=("${args[*]// /${separator}/}") args=("${args[*]// /${separator}/}")
fi fi
# Exécution # Exécution
if ! ssh_bridge_already_exists "${ssh_args[1]}"; then if ! ssh_bridge_already_exists; then
ssh-fwd "${ssh_args[@]}" || exit ssh-fwd "$vnc_host" "$local_port" "$distant_port" "$ssh_target" || exit
else else
echo "Le pont SSH existe déjà !" echo "Le pont SSH existe déjà !"
fi fi