bash 和 telnet 来测试电子邮件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14326350/
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 04:15:14  来源:igfitidea点击:

bash and telnet to test an email

bashshellunixcommandtelnet

提问by bsmoo

I'm trying to find out whether an email address is valid.

我试图找出电子邮件地址是否有效。

I've accomplished this by usign telnet, see below

我已经通过使用 telnet 完成了此操作,请参见下文

$ telnet mail.example.com 25
Trying 0.0.0.0...
Connected to mail.example.com.
Escape character is '^]'.
220 mail.example.com Mon, 14 Jan 2013 19:01:44 +0000
helo email.com
250 mail.example.com Hello email.com [0.0.0.0]
mail from:[email protected]
250 OK
rcpt to:[email protected]
550 Unknown user

with this 550 request i know that the address is not valid on the mail server... if it was valid i would get a response like the below:

通过这个 550 请求,我知道该地址在邮件服务器上无效...如果有效,我会得到如下响应:

250 2.1.5 OK 

How would I automate this in a shell script? so far I have the below

我将如何在 shell 脚本中自动执行此操作?到目前为止,我有以下

#!/bin/bash
host=`dig mx +short  | cut -d ' ' -f2 | head -1`
telnet $host 25 

Thanks!

谢谢!

回答by Gilles Quenot

Try doing this :

尝试这样做:

[[ $@ ]] || {
    printf "Usage\n\t./
./script.sh domain email from_email rcpt_email
domain <email> <from_email> <rcpt_email>\n" exit 1 } { sleep 1 echo "helo " sleep 0.5 echo "mail from:" sleep 0.5 echo "rcpt to:" echo } | telnet 25 | grep -q "Unknown user" && echo "Invalid email" || echo "Valid email"

Usage :

用法 :

cat commands.txt | telnet mail.example.com 25 | grep -i '550 Unknown User'

回答by matt forsythe

You could always enter your commands into a plain text file, line after line, just as if you typed them on the command line. Then you can use something like

您始终可以将命令一行接一行地输入到纯文本文件中,就像在命令行中键入它们一样。然后你可以使用类似的东西

BODY="open realy smtp test"
SMTP-SRV="server_ip"
SMTP-PORT="25"
RCPT="name@domain"
SRC="name@domain"

Since you will probably need to consider this text file as template, (I am assuming you will probably want to parameterize the e-mail address) you may need to insert a call to awk to take the output of 'cat commands.txt' and insert your e-mail address.

由于您可能需要将此文本文件视为模板,(我假设您可能希望参数化电子邮件地址)您可能需要插入对 awk 的调用以获取 'cat commands.txt' 的输出和插入您的电子邮件地址。

回答by matson kepson

variables to change

要改变的变量

/bin/nc ${SMTP-SRV} ${SMTP-PORT} << EOL
ehlo example_domain.com
mail from:${SRC}
RCPT to:${RCPT}
data
From:${SRC}
To:${RCPT}
subject: Telnet test
${BODY}
.
quit
EOL

then run in bash

然后在 bash 中运行

##代码##