]> gitweb.fluxo.info Git - utils-cli.git/commitdiff
Fix: vnc: support for multiple users main
authorSilvio Rhatto <rhatto@riseup.net>
Wed, 4 Sep 2024 16:49:47 +0000 (13:49 -0300)
committerSilvio Rhatto <rhatto@riseup.net>
Wed, 4 Sep 2024 16:49:47 +0000 (13:49 -0300)
vnc

diff --git a/vnc b/vnc
index 94ce3d6cb2cfa43e8220fa30ef37daf675e3eec1..a5d9a2803cfa0e00f19d4f7259231d37f07cdd7e 100755 (executable)
--- a/vnc
+++ b/vnc
@@ -6,8 +6,9 @@
 # Parameters
 BASENAME="`basename $0`"
 SERVER="$1"
-LOCALPORT="${2:-5901}"
-REMOTEPORT="${3:-5901}"
+REMOTEUSER="$2"
+LOCALPORT="${3:-5901}"
+REMOTEPORT="${4:-5901}"
 
 # Clipboard options
 # In practice, clipboard limitations are not working as expected
@@ -16,7 +17,7 @@ REMOTEPORT="${3:-5901}"
 VIEWER_OPTS="-AcceptClipboard=off -SendClipboard=off -SetPrimary=off -SendPrimary=off"
 
 function usage {
-  echo "usage: $BASENAME <server> [localport] [remoteport]"
+  echo "usage: $BASENAME <server> [user] [localport] [remoteport]"
   cat <<EOF
 
 setup instructions
@@ -42,9 +43,16 @@ if [ "$1" == "--help" ]; then
   usage
 fi
 
-# Check
+# Set user
+if [ -z "$REMOTEUSER" ]; then
+  REMOTEUSER="`whoami`"
+fi
+
+# Set config
 if [ -z "$SERVER" ]; then
-  if [ -h "$HOME/.vnc/default.passwd" ]; then
+  if [ -h "$HOME/.vnc/$REMOTEUSER@default.passwd" ]; then
+    SERVER="$(basename `readlink $HOME/.vnc/$REMOTEUSER@default.passwd` .passwd)"
+  elif [ -h "$HOME/.vnc/default.passwd" ]; then
     SERVER="$(basename `readlink $HOME/.vnc/default.passwd` .passwd)"
   else
     usage
@@ -52,7 +60,7 @@ if [ -z "$SERVER" ]; then
 fi
 
 # Check if vncserver is running in the remote server and start otherwise
-ssh $SERVER <<EOF
+ssh $REMOTEUSER@$SERVER <<EOF
   HOSTNAME="\$(cat /etc/hostname)"
 
   if [ -e "\$HOME/.vnc/\$HOSTNAME:1.pid" ]; then
@@ -69,12 +77,12 @@ EOF
 # See http://www.g-loaded.eu/2006/11/24/auto-closing-ssh-tunnels/
 # Optional SSH compression
 #ssh -C -c blowfish -f -L 5901:127.0.0.1:5901 $SERVER sleep 10
-ssh -f -L $LOCALPORT:127.0.0.1:$REMOTEPORT $SERVER sleep 10
+ssh -f -L $LOCALPORT:127.0.0.1:$REMOTEPORT $REMOTEUSER@$SERVER sleep 10
 
 # Run VNC client
 # Copy $SERVER:~/.vnc/passwd into $HOME/.vnc/$SERVER.passwd for passwordless login
-if [ -e "$HOME/.vnc/$SERVER.passwd" ]; then
-  vncviewer $VIEWER_OPTS -passwd $HOME/.vnc/$SERVER.passwd localhost:$LOCALPORT
+if [ -e "$HOME/.vnc/$REMOTEUSER@$SERVER.passwd" ]; then
+  vncviewer $VIEWER_OPTS -passwd $HOME/.vnc/$REMOTEUSER@$SERVER.passwd localhost:$LOCALPORT
 else
   vncviewer $VIEWER_OPTS localhost:$LOCALPORT
 fi