[SGVLUG] Automating (scripting) telnet

Emerson, Tom Tom.Emerson at wbconsultant.com
Tue Feb 21 17:58:24 PST 2006


> -----Original Message-----
> Behalf Of Ted Arden [and others]
> 
> On Tue, 21 Feb 2006, Emerson, Tom wrote:
> 
> > If I just "feed" this to telnet, i.e., "telnet <cmds", it 
> doesn't work [input gets consumed too quickly]
> 
> hummm.. you may be able to mod this:
> 
> http://modp.com/release/sshpasswords/
> same sort of idea. (more or less)

Thanks all for the suggestion of "expect".  It *should* be able to do the job, and in fact might be able to do it "one better" by searching for and eliminating bad e-mails before fetchmail even tries.

Of course, now I want someone to do a presentation on Expect so I can figure out what to do...  Learning expect "from the man page" is semi-possible, but only scratches the surface [Expect is based on TCL, so unless you have an idea of how TCL works, diving into expect (and using only the man page as a guide as I have) will be a bit difficult -- the man page only describes the enhancements to TCL that makes expect what it is.]  There appear to be plenty of books on the subject, some of which are online [o'reilly safari bookshelf comes to mind...]

The documentation, however, has a few dozen examples which may serve to fill in the gaps [like how to use command-line arguments in the expect script]

In a nutshell, I think I want/need something like this:

   spawn [telnet to POP server]
   expect "+OK server ready"
   send "user <id>"
   expect "+OK hello <id>"
   send "pass <passwd>"
   expect "+OK password"
   for (set i [lindex $argv 1]) ($i>0) () {
      send "dele $i"
      expect "+OK message $i deleted"
      incr i -1
   }
   send quit

What would be nice would be to do this once logged on:

   send stat
   [capture highest number]
   for (set i 1) {$i < $lastemail) () {
      send top $i 0
      expect "^from.*\"\"" send "dele $i"; expect "+OK message $i deleted"
      incr i
   }

which, if I'm getting this right, would look for two consectutive double-quotes in the "from" line, deleting the message if found (and waiting, in that case, for the "deletion" of that message to be confirmed)  It's the "capture highest number" bit that escapes me at the moment...

Am I barking up the right tree?


More information about the SGVLUG mailing list