Remote input-output with Expect

Expect makes it possible to fake a network file system with just a few lines of scripting. The example below can be wrapped up so that open can access remote files for either line-at-a-time or block I/O.

More to the point, Expect makes it practical to do computations on busted hosts with minimal facilities. If you can see the data, probably through some sort of telnet, then you can bring them inside Expect and calculate conveniently. Here's an illustration:

spawn telnet $host
expect login:
send $user\r
expect Password:
send $pass\r

expect $prompt
send "cat $filename\r"

log_user 0

expect {
    ^$prompt {
        # ...
    }
    -re {^([^\r]*)\r\n} {
        puts "The line is '$expect_out(1,string)'."
        exp_continue
    }
}

Useful tip if rshell is available to the host. No login and you get only the output of the command.

spawn rsh -s $host $commandtorun
expect {
    \n {
        puts $expect_out(buffer)
        exp_continue
    }
    eof {
        puts $expect_out(buffer)
    }
}     ;# RJ