Below is the code I said that I\'d post. As you can see from the previous posts there is more that one way to do it. I use ssh key auth as well for non-interactive scripts (e.g. VPN setup), however it can be difficult to script interactive tty-based applications. For that I use expect.
The script below takes one arg (passwd), sshes to my ISP and forwards ports 25, 110, and 8080. I do this because some WiFi locations have port 80 blocked so I need a proxy and my ISP requires SSL or SSH for mail. Since the Z mail app does not use SSL I just forward 25 and 110 and have the Z mail app point to localhost.
After I connect expect starts elm and then drops in to interact mode. You can do a lot in interact mode and have it watch for characters in both directions and automate many functions for you. Below expect is watching for EOF (ssh exit) or ~. to exit. If the keys \"DS\" are pressed then it sends ^DSPAM:[return] to delete all spam from elm.
This is a common procedure for me since ]90% of my mail is SPAM and downloading mail to the Z app over GPRS is slow enough. I could have just sent the ^DSPAM:[return] to elm, exited elm then started interact at the prompt, but I like to take a quick look at the headers for false positives. Then I hit the DS, q to quit, y to delete, y to save in inbox, then use Z mail to download mail for offline reading.
You can get expect from
http://sense.net/zc/files (they are ipk of the standard debian .deb files). Get the tcl package as well.
The book:
http://www.oreilly.com/catalog/expect/index.html#!/usr/bin/expect
log_user 0
set passwd [lindex $argv 0]
spawn ssh -g -l me -L 25:localhost:25 -L 110:localhost:110 -R 8080:proxy.myisp.com:8080 myisp.com
expect {
"word: " {
send "$passwdr"
expect {
"Permission denied" {
puts "bad password"
exit 1
}
"$" {
send "elmr"
interact {
"DS" {
send "\004SPAM:r"
}
"~." {
exit 0
}
EOF {
exit 0
}
}
}
}
}
timeout {
puts "Timeout"
exit 1
}
"ssh:*" {
puts "$expect_out(0,string)"
exit 1
}
}