Skip to content

Instantly share code, notes, and snippets.

@cshimmin
Created February 6, 2014 02:00
Show Gist options
  • Save cshimmin/8837125 to your computer and use it in GitHub Desktop.
Save cshimmin/8837125 to your computer and use it in GitHub Desktop.
Simple script to log in to a random lxplus6 node 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 …
#!/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 local_user [lindex [array get env USER] 1]
# 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 lxplus.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*\n"
#set lxp_host $expect_out(0,string)
set lxp_host [string trim $expect_out(0,string)]
append lxp_host ".cern.ch"
# 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