movisman

Connect VNC via SSH

Apr 16th, 2019
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.50 KB | None | 0 0
  1. #!/bin/bash  
  2.  
  3. # this script connects via SSH to the destination host, starts an X11VNC server and connects to it
  4. # it uses randomized IP addresses and ports so that the possibility of clashing with other listening programs is minimized
  5. #
  6. # To install the packages needed (e.g. in Ubuntu):
  7. #  apt-get install x11vnc ssvnc
  8.  
  9. # bail if not enough options
  10. if [ "$1" = "" ]; then
  11.     echo "Usage: $0 hostname [vncviewer-options]"
  12.     exit 1
  13. fi
  14.  
  15.  
  16. # range of usable ports
  17. FLOOR=25900
  18. RANGE=25999
  19. LOCALPORT=0
  20. while [ "$LOCALPORT" -le $FLOOR ]
  21. do
  22.   LOCALPORT=$RANDOM
  23.   let "LOCALPORT %= $RANGE"
  24. done
  25.  
  26. # set the same port for remote and local
  27. REMOTEPORT=$LOCALPORT
  28.  
  29. RANGE=255
  30. IPPART=$RANDOM
  31. let "IPPART %= $RANGE"
  32.  
  33. # use a random localhost IP address so we don't accidentally conflict with anything
  34. LOCALHOSTIP=127.59.$IPPART.1
  35.  
  36. # SSH to the 1st argument, start x11vnc there
  37. ssh $1 -L $LOCALHOSTIP:$LOCALPORT:$LOCALHOSTIP:$REMOTEPORT -vvv -o ControlMaster=no "x11vnc -nopw -ncache -display :0 -listen $LOCALHOSTIP -noipv6 -norc -wireframe -rfbport $REMOTEPORT " &
  38. SSHPID=$!
  39. # ^ save SSH PID to kill the connection later
  40.  
  41. # don't use $1 anymore
  42. shift
  43. # wait for the connection to be established, just to be on the safe side
  44. sleep 10
  45. # launch the viewer
  46. ssvnc -viewer -use64 -nocursorshape -nobell -encodings hextile -sendclipboard -scale 0.9 $* $LOCALHOSTIP::$LOCALPORT &
  47. # alternately, use the
  48. ##vncviewer 127.59.0.1:$LOCALPORT
  49.  
  50. wait $!
  51. # ^ wait for VNC to exit
  52. # close the SSH tunnel
  53. kill $SSHPID
Add Comment
Please, Sign In to add comment