Version 1 of remote ssh tcl

Updated 2004-05-26 11:42:46 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.

connect fred@host 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+"]
        fconfigure $ssh -buffering line -blocking 1
        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 $ssh
    }

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

    proc remote {args} {
        global ssh
        #puts stderr "CMD: $args"
        puts $ssh $args

        set result ""
        set cnt 0
        set opt [string map [list \\n \n \\\\ \\] [gets $ssh]]
        #puts "OPT: $opt"
        set result [string map [list \\n \n \\\\ \\] [gets $ssh]]
        #puts "RESULT: $result"
        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 snapshot@colin
        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]
    }