bash 查找 sendmail 版本(sSMTP 或 Postfix 或其他)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39668980/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 15:13:16  来源:igfitidea点击:

Find sendmail version (sSMTP or Postfix or other)

linuxbashsendmail

提问by Sibidharan

I have a script which runs on multiple servers. Some servers are using sSMTPand some are using postfix.

我有一个在多台服务器上运行的脚本。有些服务器使用sSMTP,有些使用postfix

I want to find which version of sendmailmy server is running in runtime, because -tis not supported in sSMTPbut is mandatory in Postfix

我想找出我的服务器在运行时运行的sendmail版本,因为sSMTP-t不支持,但Postfix是强制性的

Now the challenge begins.

现在挑战开始了。

sendmail -Von sSMTP variant outputs sSMTP 2.64 (Not sendmail at all)but the postfix variant doesn't have the -Voption for displaying the version.

sendmail -V在 sSMTP 变体输出上,sSMTP 2.64 (Not sendmail at all)但后缀变体没有-V显示版本的选项。

I managed to accomplish it with the following snippet.

我设法使用以下代码段完成了它。

VER=$(sendmail -V 2>/dev/null)
if [[ "sSMTP" == $VER* ]]; then
        echo $BODY | sendmail $EMAIL #sSMTP
else
        echo $BODY | sendmail -t $EMAIL #postfix
fi

Is there a more efficient method to achieve this?

有没有更有效的方法来实现这一目标?

I want to find what variant of sendmail is in my server. Not just postfix or sSMTP.

我想在我的服务器中找到 sendmail 的哪个变体。不仅仅是 postfix 或 sSMTP。

采纳答案by tripleee

How about this?

这个怎么样?

type -p sendmail |
xargs dpkg -S

Some packagesput it in /usr/lib, others in /usr/sbin, etc. This obviously requires them to be on your PATH.

有些包把它放进去/usr/lib,其他的放进去/usr/sbin,等等。这显然需要它们在你的PATH.

dpkg -Stells you the name of the package which installed a file, and type -pis used to find sendmailon your PATHso we can pass that to dpkg -S. This obviously requires a Debian-based distro (Ubuntu, Mint, what have you).

dpkg -S告诉您安装文件的包的名称,并type -p用于sendmail在您的上查找,PATH以便我们可以将其传递给dpkg -S. 这显然需要一个基于 Debian 的发行版(Ubuntu、Mint,你有什么)。

On an ssmptsystem, with /usr/libon the PATH, the output is

在一个ssmpt系统上,在/usr/libPATH,输出是

ssmtp: /usr/lib/sendmail

On a Postfix system with a slightly different PATH,

在稍有不同的 Postfix 系统上PATH

postfix: /usr/sbin/sendmail

I don't think I have an Exim or Qmail system where I can try this, but they should be predictably similar.

我不认为我有可以尝试此操作的 Exim 或 Qmail 系统,但可以预见它们应该是相似的。

As an aside, if you want to pass a multi-line string such as an email message to sendmailby way of a pipe from echo, you will need to quote the multi-line variable in order for it to work.

sendmail顺便说一句,如果您想通过来自 的管道echo将多行字符串(例如电子邮件消息)传递给,则需要引用多行变量才能使其工作。

echo "$body" | sendmail -t

If you want to use sendmail -t, the "$body"should contain the To:header for sendmailto parse -- this is what the -toption does, so I don't think you want a variable with a recipient address in this scenario.

如果您想使用sendmail -t,则"$body"应该包含要解析的To:标头sendmail——这就是该-t选项的作用,所以我认为在这种情况下您不需要带有收件人地址的变量。

Also, don't use uppercase for your private variable names; uppercase variable names are reserved for system use.

另外,不要对私有变量名使用大写;大写变量名保留供系统使用。

Finally, as already commented elsewhere, I believe both SSMTP and Postfix support sendmail -t, as does practically every other Sendmail implementation I have seen. So in the end, I don't think you actually need this code to achieve that goal.

最后,正如其他地方已经评论过的,我相信 SSMTP 和 Postfix 都支持sendmail -t,就像我见过的几乎所有其他 Sendmail 实现一样。所以最后,我认为您实际上不需要此代码来实现该目标。

回答by Adam

Is there a more efficient method to achieve this?

有没有更有效的方法来实现这一目标?

Yes; don't try to find the sendmail version, and use a standard way of sending mail...

是的; 不要试图找到sendmail版本,而是使用标准的方式发送邮件...

You should use the mail(or mailx) command for better compatibility

您应该使用mail(or mailx) 命令以获得更好的兼容性

MAILCMD=$(type -p mail || type -p mailx)
echo $BODY | $MAILCMD $EMAIL

回答by James Brown

How about:

怎么样:

Postfix:

后缀:

$ /usr/sbin/sendmail -C sdfsf
sendmail: fatal: open sdfsf/main.cf: No such file or directory
$ echo $?
75

sSMTP:

sSMTP:

$ /usr/sbin/sendmail -C sdfsf
sendmail: No recipients supplied - mail will not be sent
$ echo $?
0

In your script you execute /usr/sbin/sendmail -C sdfsf >/dev/null 2>&1and test the return value, which is different depending on the MTA.

在您的脚本中,您执行/usr/sbin/sendmail -C sdfsf >/dev/null 2>&1并测试返回值,这取决于 MTA。

/usr/sbin/sendmail -C sdfsf >/dev/null 2>&1
if [ $? -eq 0 ]; then   # return code 75 would mean Postfix
    echo sSMTP
else 
    echo Postfix
fi
sSMTP

In Postfix -Cidentifies the config file and if non-existent causes the sendmailto fail with an return code (75). sSMTP if run with or without the -Cswitch exits with a zero and message sendmail: No recipients supplied - mail will not be sent. Test the return value ($?) for identification.

在 Postfix 中-C标识配置文件,如果不存在会导致sendmail失败并返回代码 (75)。sSMTP 如果在有或没有-C开关的情况下运行,都会以零和消息退出sendmail: No recipients supplied - mail will not be sent。测试返回值 ( $?) 以进行识别。

回答by Michael Lubert

This is a bit hacky, but should work (I'd check that man sendmailon a machine with sSMTP doesn't have the word "postfix")

这有点 hacky,但应该可以工作(我会检查man sendmail在带有 sSMTP 的机器上没有“postfix”这个词)

echo $(man sendmail)| grep "postfix" | wc -l

echo $(man sendmail)| grep "postfix" | wc -l

Basically it parses the manpage for the word "postfix" and returns 1 one if it has it (0 theoretically for sSMTP).

基本上它会解析“postfix”这个词的联机帮助页,如果有的话则返回 1(理论上 sSMTP 为 0)。