VNC Tunnel using ssh Expect script

Just a Little script i wrote to log on to a server and set up a ssh tunnel mapping a remote VNC port to your local machine. If the connection is refused the script will keep trying at increased intervals. To make the script work you need to create a credentials file and put the following in it: set username "yoursshusername" set passwd "yoursshpassword" Remember to chmod the credentials file, just in case, because the details are stored in plain text. You will need to edit the script to point at your credentials file and server name.
  1. #! /usr/bin/expect
  2.  
  3. # Set this to the location of your credentials file
  4. set cred "~/scripts/credentials"
  5.  
  6. # Set This to the server name
  7. set server "munkiepus.dnsalias.org"
  8.  
  9.  
  10. #read in credentials file
  11. source $cred
  12.  
  13. set responseok "false"
  14. set passwdok "false"
  15. set sleeptime 2
  16.  
  17. while {$responseok == "false"} {
  18.  
  19.   send "Logging on to $server \n"
  20.   sleep 1
  21.   eval spawn ssh -L 5902:localhost:5902 $username@$server
  22.   match_max 100000
  23.   expect {
  24.     "Connection refused"
  25.     {
  26.        send_user "\n Connection refused...
  27.                  Trying again in $sleeptime Seconds\n\n"
  28.        set responseok false
  29.        sleep $sleeptime
  30.        incr sleeptime
  31.     }
  32.  
  33.     "unknown host"
  34.     {
  35.       send_user "\n Unknown Host...  \n\n"
  36.       set responseok false
  37.       sleep 10
  38.       abort
  39.     }
  40.  
  41.     "assword: "
  42.     {
  43.       set responseok true
  44.     }
  45.  
  46.   }
  47.  
  48. }
  49.  
  50. send "$passwd\r"
  51. expect "$ "
  52. interact

Category: