如何在bash脚本中的管道命令之间插入延迟。例如,cat file | telnet mail.domain.com 25

时间:2022-02-16 15:19:48

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.

我有一个创建文件的bash脚本,我想在最后通过telnet发送电子邮件。然而,有时它会执行,有时不会。

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.

如果我在localhost中启动telnet会话,而不是mail.domain.com,那么同样的脚本可以完美地工作,所以我很确定文件格式是OK的,bash脚本的其余部分也可以工作。

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.

但结果有时是好的,有时不是。我认为重定向和telnet会话命令之间需要有一个小的延迟,以便在telnet会话建立并给出响应之后提供输入,但是我不知道如何做到这一点。我试过在管道和重定向之间使用sleep命令,它可能不会工作,因为输入被重定向到sleep命令。

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.

限制是我必须在bash脚本中这样做。是可能的吗?另外,我不知道它是否重要,但是用于debian挤压服务器之间的脚本使用postfix/courier设置,现在接收端设置为debian wheezy和postfix/dovecot。

Thanks in advance for the help

谢谢你的帮助

1 个解决方案

#1


21  

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.

要打开连接,什么都不写,请等待5秒,然后写剩下的。

#1


21  

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.

要打开连接,什么都不写,请等待5秒,然后写剩下的。