How do I execute 'grep' on a remote machine and receive the result?

There turns out not to be a single right answer. Here are several small essays on the question:


    package require Expect

    log_user 0

    spawn ssh [email protected]
    expect {assword: }
    send $password\r
    expect $prompt
    send "grep $pattern $filename\r"
    expect -re "\[^\n]*\n(.*)\n.*$prompt"
    puts $expect_out(1,string)
    send exit\r
    expect eof

[Show how to put it in a proc.]


[Show an RE-free version.]


[Show a more max_match-resistant version.]


[Introduce exception-handling.]


[Illustrate reliance on "How to access the result of a remote command in Expect"]


NEM offers:

proc remote {user host command args} {
    exec ssh $user@$host $command {*}$args
}
remote $me $myhost grep foo /etc/passwd

Will the original requestor be able to say

set result [remote $me $myhost grep foo /etc/passwd]

and have the result of the command in $result?

I tried the example, but the ssh on my machine is somehow incompatible with the ssh on my $myhost. I know the error I received wasn't in $result though.