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

13,798

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.

Share:
13,798

Related videos on Youtube

gabtzi
Author by

gabtzi

Updated on June 09, 2022

Comments

  • gabtzi
    gabtzi about 2 years

    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

  • chepner
    chepner over 10 years
    { echo -n; sleep 5; cat tempfile; } | ... would avoid an unnecessary subshell.
  • gabtzi
    gabtzi over 10 years
    Great thanks for the fast response :) This worked flawlessly. Even without echo -n it will still wait for 5 seconds and then write the rest. I never thought about including it in (). I tried it with {} and it still works so both answers are good. However with {} i have to use echo -n;