Version 3 of remote ssh tcl

Updated 2004-05-27 02:24:27 by CMCc

CMcC: Here are a couple of procs to feed tcl commands to a tclsh on another machine via ssh, and collect the result. Requires a recent tclsh8.4 with extended return and dict commands.

connect [email protected] will connect as user fred on host host. Note that fred should be a user whose login shell is tclsh, and for whom login is not permitted (I know this sounds contradictory, but it works under unix.) The user running these procs has to have keys sufficient to log in without password and without passphrase.

    proc connect {where} {
        global ssh
        set ssh [open "|ssh $where" "r+"] ;# start up the remote shell, should be tclsh
        fconfigure $ssh -buffering line -blocking 1

        # set up a command loop which processes and returns our commands
        puts $ssh {
                fconfigure stdout -buffering line
                while {![eof stdin]} {
                    set cmd [gets stdin]
                    set code [catch $cmd result opt]
                    puts [string map [list \\ \\\\ \n \\n] $opt]
                    puts [string map [list \\ \\\\ \n \\n] $result]
                }
        }

        # return the ssh connection
        return $ssh
    }

remote passes the expression to the remote via ssh, and returns the result.

    proc remote {args} {
        global ssh

        # execute the command in the remote tclsh
        #puts stderr "CMD: $args"
        puts $ssh $args

        # get the error option dict
        set opt [string map [list \\n \n \\\\ \\] [gets $ssh]]
        #puts "OPT: $opt"

        # get the remote execution result
        set result [string map [list \\n \n \\\\ \\] [gets $ssh]]
        #puts "RESULT: $result"

        # return the result or error from the remote
        return -options [eval dict create $opt] $result
    }

This is just a little bit of test code. There's a problem with quoting, if someone can show me how to do it better, I'd be grateful.

    if {[info exists argv0] && ($argv0 == [info script])} {
        connect [email protected]
        puts [remote set mumble 100]
        puts [remote incr mumble]
        puts [remote set mumble]
        puts [remote return "Woo\\nWoo\\n"]
        puts [remote set blerf]        ;# expect an error here
        puts [remote set mumble]
    }