How to insert a delay between pipelining commands in a bash script. E.g. cat file | telnet mail.domain.com 25

gabtzi picture gabtzi · Oct 11, 2013 · Viewed 12.1k times · Source

I have a bash script that creates a file and I would like to send an email at the end via telnet. However sometimes it will execute and sometimes it won't.

The command at the end is

cat tempfile | telnet mail.domain.com 25

At the receiving server I see in mail.log the following error when it fails:

improper command pipelining after EHLO from domain.com ....etc

The same script works perfectly if instead of mail.domain.com I start the telnet session in localhost so I'm pretty sure the file format is OK and the rest of the bash script is working too.

I've also tried using standard redirection instead of a pipe

telnet mail.domain.com 25 < tempfile

But again the result sometimes is okay sometimes is not. I think there needs to be a small delay between the redirection and the telnet session command so that the input will be given after the telnet session has been established and a response has been given but I don't know how to do that. I've tried using sleep command in between pipes and redirection and it won't work probably because then the input is redirected to the sleep command.

e.g. cat tempfile | telnet mail.domain.com 25 & sleep 1

The restriction is that I have to do it in a bash script. Is it possible? Also I don't know if it's of any importance but the script used to work between servers in debian squeeze with postfix/courier setup and now the receiving end is set up with debian wheezy and postfix/dovecot.

Thanks in advance for the help

Answer

Paul Nechifor picture Paul Nechifor · Oct 11, 2013

You could try something like:

(echo -n; sleep 5; cat tempfile) | mail.domain.com 25

to open the connection and write nothing, wait for 5 seconds and write the rest.