#!/bin/sh # the next line starts tcl \ exec tclsh "$0" "$@" set VERSION 1.0 proc Usage { {msg ""} } { set msg "$msg\nusage: vnc \[options\] " set msg "$msg\n is the remote host" set msg "$msg\n \[options\] is one of the following:" set msg "$msg\n -v | --vncviewer : path to vncviewer executable" set msg "$msg\n -d | --remote-display : display number on " set msg "$msg\n -h | --help : prints this message and exits" set msg "$msg\n --version : print out the version info and continue" puts stderr "$msg\n" exit 1 } # # simple arg parsing # set PORT "" set VNCVIEWER c:/cygwin/usr/local/bin/vncviewer.exe set USER pieper set REMOTEDISPLAY 0 set strippedargs "" set argc [llength $argv] for {set i 0} {$i < $argc} {incr i} { set a [lindex $argv $i] switch -glob -- $a { "--help" - "-h" { Usage exit 0 } "--vncviewer" - "-vnc" { incr i if { $i == $argc } { Usage "missing argument for $a\n" } else { set VNCVIEWER [lindex $argv $i] } } "-p" - "--port" { incr i if { $i == $argc } { Usage "missing argument for $a\n" } else { set PORT [lindex $argv $i] } } "-d" - "--remote-display" { incr i if { $i == $argc } { Usage "missing argument for $a\n" } else { set REMOTEDISPLAY [lindex $argv $i] } } "-v" - "--version" { puts $VERSION } "-*" { Usage "unknown option $a\n" } default { lappend strippedargs $a } } } set argv $strippedargs set argc [llength $argv] if {$argc > 1 } { Usage exit 1 } else { set HOST [lindex $argv 0] if { [string first @ $HOST] != -1 } { scan $HOST {%[^@]@%s} USER HOST } } proc echo {args} {puts $args} if { $PORT == "" } { # look for an open port for {set p 5900} {$p < 6000} {incr p} { if { ![ catch "set sp \[socket -server echo $p\]" ] } { set PORT $p close $sp break } } } if { $PORT == "" } { puts stderr "couldn't find a free port between 5900 and 6000" exit -1 } else { puts "found port $PORT" } set REMOTEPORT [expr 5900 + $REMOTEDISPLAY] exec rxvt -e ssh -C -L $PORT:localhost:$REMOTEPORT $USER@$HOST & for {set tries 0} {$tries < 50} {incr tries} { if { ![ catch "set sp \[socket localhost $PORT\]" ] } { puts "connected to $PORT" close $sp set display [expr $PORT - 5900] exec /usr/bin/csh -c "$VNCVIEWER localhost:$display" & exit } puts -nonewline "."; flush stdout after 100 } puts "timed out waiting for port $PORT"