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

Updated 2009-05-07 18:18:13 by nem

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

enter categories here