Advertisement
Linux-Fan

UNI VNC Connection

Apr 19th, 2016
369
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.88 KB | None | 0 0
  1. #!/bin/sh
  2. # Ma_Sys.ma STD/UNI VNC Connection Script 1.0.1.2, Copyright (c) 2016 Ma_Sys.ma.
  3. # For further info send an e-mail to Ma_Sys.ma@web.de.
  4. #
  5. # This program is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program.  If not, see <http://www.gnu.org/licenses/>.
  17. #
  18. # Notice: On Debian systems, the license can be found under
  19. #         /usr/share/common-licenses/GPL-3
  20.  
  21. # -- Settings --
  22.  
  23. tuid=AA00AAAA              # Enter your TU-ID here
  24. dnr=63                     # Remote X11 display number, example: 63
  25. pn=2                       # Pool Computer number, example: 2 for clientssh2
  26. dnr2=2                     # Local X11 display number, 2 for :2 is often OK
  27. wm=/usr/bin/startfluxbox   # Window manager. Leave empty for default icewm
  28.  
  29. # Space-separated list of key-files (first existent is used)
  30. id_rsa=/home/linux-fan/local/id_rsa_pool
  31. id_rsa="$id_rsa /data/main/mdb/prod/keys/tud/pool/id_rsa"
  32.  
  33. # -- End Settings --
  34.  
  35. if [ "$1" = "pseudoproc" ]; then
  36.     # Required to keep the server alive while the client is running.
  37.     # Exec is vital because we may only have one PID
  38.     exec sleep 1000000
  39. fi
  40.  
  41. # Display copyright banner
  42. head -n 4 "$0" | tail -n 3 | cut -c 3-
  43.  
  44. # Determine VNC resolution by using the current screen size.
  45. # Add -40 (height) and -4 (width) to account for i3 borders.
  46. # Other window managers' users might want to change this or go for full screen.
  47. rr="$(xrandr | grep -E "^  .*\*" | head -n 1 | sed 's/\s\+/ /g' | \
  48.                             cut -d " " -f 2)"
  49. rh="$(($(echo "$rr" | cut -dx -f 2) - 40))"
  50. rw="$(($(echo "$rr" | cut -dx -f 1) -  4))"
  51.  
  52. an="$(basename "$0")"
  53.  
  54. # Locate first identity file
  55. identity=
  56. for i in $id_rsa; do
  57.     if [ -f "$i" ]; then
  58.         identity="$i"
  59.         break
  60.     fi
  61. done
  62.  
  63. # SSH identity file is mandatory, remove this to use password authentication.
  64. if [ -z "$identity" ]; then
  65.     echo ERROR: Could not locate identity file. 1>&2
  66.     exit 1
  67. else
  68.     echo "[$an     ] Selected identity file $identity"
  69. fi
  70.  
  71. # Generate random password for this session. Unfortunately we are limited to
  72. # 8 characters. If you believe md5sum is not good, you might want to use base64
  73. # (more characters but probably higher predicatibility)
  74. passw="$(head -c 64 /dev/random | md5sum | cut -c -8)"
  75.  
  76. echo "[$an     ] Using password ${passw}."
  77.  
  78. # Host to connect to
  79. daddr="${tuid}@clientssh${pn}.rbg.informatik.tu-darmstadt.de"
  80.  
  81. # Create window manager configuration if wanted
  82. wmline=
  83. if [ -n "$wm" ]; then
  84.     wmline="echo '\$vncStartup = \"exec $wm\"' > \$HOME/.vncrc;"
  85. fi
  86.  
  87. # ():
  88. # Spawn a pseudoproc (a process which just waits forever) and send its PID and
  89. # the current password to the server, then just wait forever.
  90. #
  91. # ssh -i ...:
  92. # Connect local port 5900 + dnr2 to server's 5900 + dnr where dnr are the
  93. # display numbers (need to be added to determine the real port).
  94. # On the server, read PID and password, create a passwordifle and start the
  95. # server. Then send a success synchronization message and wait for the input
  96. # stream to be closed (happens when pseudoproc terminates).
  97. #
  98. # while read -r line:
  99. # Wait for synchronization message and extract PID (only valid on the local
  100. # machine anyway). Connect using the VNC client and terminate pseudoproc once
  101. # the client terminates thereby closing the remote session.
  102. #
  103. # The idea of this ``Local PID -> Server (TEXT) -> Local (PID)'' is to avoid
  104. # an unecessary temporary file.
  105.  
  106. ( "$0" pseudoproc & pid2=$!; echo "$pid2 $passw"; wait $pid2 ) | \
  107.     ssh -i "$identity" \
  108.         -L $((5900 + $dnr2)):localhost:$((5900 + $dnr)) "$daddr" \
  109.         "read -r pid2 passw; \
  110.         [ -d \"\$HOME/.vnc\" ] || mkdir \"\$HOME/.vnc\"; \
  111.         echo \"\$passw\" | vncpasswd -f > \"\$HOME/.vnc/passwd\"; \
  112.         chmod 0600 \"\$HOME/.vnc/passwd\"; \
  113.         $wmline
  114.         echo \"@\$(uname -n) Starting server... \" \
  115.                     \"(pid=\$pid2, passw=\$passw)\"; \
  116.         vncserver -geometry \"${rw}x$rh\" \":$dnr\" 2>&1; \
  117.         echo MXCHSUCCESS\$pid2; \
  118.         read nothing; \
  119.         vncserver -kill :$dnr 2>&1" | \
  120.     while read -r line; do
  121.         msg="$(echo "$line" | cut -c -11)"
  122.         if [ "$msg" = "MXCHSUCCESS" ]; then
  123.             pid="$(echo "$line" | cut -c 12-)"
  124.             echo "[$an     ] Starting viewer " \
  125.                         "(Pseudoproc running @$pid)"
  126.             echo "$passw" | xtightvncviewer \
  127.                 -compresslevel 9 -autopass \
  128.                 -encodings Tight localhost:$dnr2 2>&1 | \
  129.                 sed 's/^/\['"$an"'\/VIEW\] /g'
  130.             echo "[$an     ] Killing Pseudoproc $pid"
  131.             kill "$pid"
  132.         else
  133.             echo "[$an/SSH ] $line"
  134.         fi
  135.     done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement