[SGVLUG] Automating (scripting) telnet

DJ Byrne djbyrne at jpl.nasa.gov
Tue Feb 28 20:38:10 PST 2006


On Tuesday, February 21, 2006, at 02:28  PM, Emerson, Tom wrote:

> I have a small fetchmail problem.  Every now and then, I get a bit of 
> spam with a "from" line that looks like this:
>
>    from: "steven"" <some.email at some.server>
>
> Note the extra "quote" character -- that causes fetchmail to puke.  I 
> believe this is "fixed" in later releases of fetchmail, but I don't 
> have ready access to a binary for it (and the dependancy chain to 
> compile it is more than I want to deal with at the moment)
>
> As it is, I've been "dealing" with this problem by logging in to my 
> e-mail server using telnet and manually finding-and-deleting the 
> message.  Normally, this is the first message in the queue, so there 
> isn't much of a problem.  Unfortunately, after I clear the first one, 
> there *may* be a second one that has arrived in the following 900+ 
> queued messages [and in fact, that is exactly what happened this 
> morning: message 671 was the same bit of spam...]  Normally, when 
> fetchmail runs it deletes messages it has processed, but in the case 
> of this "bad" message, fetchmail seems to drop out entirely (forcibly 
> disconnect?) and as a result, the 670 messages that it had reported as 
> "flushed" aren't actually flushed, meaning that fetchmail will 
> retrieve them again on the next cycle.
>
> when the second (or third, fourth, or fifth...) copy arrives within 
> say the first 50-60 messages, it isn't too difficult to manually type 
> "dele <nn>" for each message, but when there are 600+, well, I know my 
> fingers will get tired...
>
> What I'd like to do is somehow "script" telent so that I can delete 
> those 600+ messages that have already been processed, something like 
> this:
>
>
> [I type]
>   $  killmsgs 670
>
> [it does this]
>
>    telnet <my.providers.pop.server> pop3
>    user <me>
>    pass <mypass>
>    dele 1
>    dele 2
>    ...
>    dele 670
>    quit
>
> If I just "feed" this to telnet, i.e., "telnet <cmds", it doesn't work 
> -- telnet doesn't seem to "wait" for the server to actually be "ready 
> for input" before sending the commands -- it might see "user <me>", 
> for instance, but only get "last-half-of-password>" as the next 
> "command" since my client would have started sending it before the 
> server responded with "OK, hello <you><crlf>".
>
> any thoughts?
>

You've already noted that "expect" is the Right Way,
but did you try a simple sh script "here document?"

#!/bin/sh
PATH=/usr:/usr/bin

telnet <my.providers.pop.server> pop3 < EOF
user <me>
pass <mypass>
dele 1
dele 2
...
dele 670
EOF


Hmm; alternatively, here's an example showing
throttled cmds to telnet (I don't have 670 email
msgs I want to delete in testing)

#!/bin/sh

{
echo helo
sleep 2
echo vrfy
sleep 2
echo quit
sleep 2
} | telnet localhost 25

That construct would work for your problem somewhat like this:
#!/bin/sh

NMSGS=$1
# error-checking on input here  :-)
INDX=1

{
echo user me
echo passwd me
while [ $INDX -le $NMSGS ]
do
         echo dele $INDX
         INDX=`expr $INDX + 1`
         sleep 1
done
} | telnet <my.providers.pop.server> pop3



More information about the SGVLUG mailing list