Created
January 15, 2014 22:52
-
-
Save cshimmin/8446359 to your computer and use it in GitHub Desktop.
Simple script to log in to lxplus5 and initiate a vnc session. The vnc port will be forwarded locally to 5901. On mac, simply run the script and leave the window open. Next, open Finder, press CMD+K, and enter "vnc://localhost:5901" NB: You'll have to change the variable "prompt_match" to something that will match your bash prompt line.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env expect | |
# this expect script will connect to an lxplus node, | |
# start a vncserver session, and then locally | |
# forward the vnc host to port 5901. | |
# the ssh session is left open with a kill command ready to go | |
# for easy cleanup. | |
set timeout 30 | |
# set this to (uniquely) match your terminal prompt. if your | |
# prompt indicates your username, that will work. | |
set prompt_match "cshimmin" | |
set geometry [lindex $argv 0] | |
if { $geometry == "" } { | |
puts "Usage: <screen_geometry>" | |
puts " screen_geometry: specified as e.g. 1200x800" | |
exit 1 | |
} | |
# connect to the lxplus cluster alias to get a random node | |
spawn ssh lxplus5.cern.ch | |
expect "$prompt_match" | |
send "\n" | |
expect "$prompt_match" | |
# get the hostname for this node | |
send "hostname\n" | |
expect "\n" | |
expect "\n" | |
expect "lxplus*ch" | |
set lxp_host $expect_out(0,string) | |
# start a vncserver with the specified geometry, and save the vnc port | |
send "vncserver -geometry $geometry\n" | |
expect "New*desktop" | |
set p [string first ":" $expect_out(0,string)] | |
set vnc_port [string range $expect_out(0,string) [expr $p + 1] [expr $p + 2]] | |
# disconnect from the lxp node | |
puts "started vnc session $lxp_host:$vnc_port" | |
send "exit\n" | |
close $spawn_id | |
# now reconnect to the same node and forward the port to 5901 locally | |
spawn ssh "$lxp_host" "-L5901:localhost:[expr 5900 + $vnc_port]" | |
expect "Are you sure" { send "yes\n" } \ | |
"$prompt_match" { send "" } | |
expect "$prompt_match" | |
puts "connected to vnc session" | |
# leave the kill command for this vnc session so the user can manually | |
# clean up when done. | |
send "\n\n" | |
send "vncserver -kill :$vnc_port" | |
interact |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment