使用 Bash 验证电子邮件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1524288/
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
Validate Emails with Bash
提问by fwaechter
I try to validate an email with a shell script. Is there a easy example to validate a mail? Asked google but just found crap and PHP (also crap..).
我尝试使用 shell 脚本验证电子邮件。是否有一个简单的例子来验证邮件?问谷歌,但刚刚发现废话和 PHP(也废话..)。
Thanks and regards.
感谢致敬。
回答by Leolo
If you explicitly state bash on the #! line, you can uses regexes:
如果您在 #! 行,您可以使用正则表达式:
#!/bin/bash
if [[ $email =~ '(.+)@(.+)' ]] ; then
user=${BASH_REMATCH[1]}
host=${BASH_REMATCH[2]}
fi
If you are serious about programing with bash, read the man page all the way through. Twice. Also read the Bash FAQ.
如果您对使用 bash 编程很认真,请通读一遍手册页。两次。另请阅读Bash 常见问题解答。
回答by eldorado0o
Validate script.
验证脚本。
The meaning of this script, is to be sure from all emails that are valid or not before sending them email.
这个脚本的意思,是在发送电子邮件之前从所有电子邮件中确定是否有效。
this script check a list of emails.
此脚本检查电子邮件列表。
#!/bin/bash
#:
#: e-mail.verify.sh
#:
#: Date: 2011/14/12 13:14 PM IST
#: Author: Louay _at_ [email protected]
#: Discription: Verify (or) Validate the Hotmail Adresses.
#:
#:
#: First we create a Expect script to be sourced for us.
if [ ! $# == 1 ]
then
echo "Invalid Args #!/bin/bash
# check for valid usage
if [ x = 'x' ]
then
echo "Usage: #!/bin/bash
# check for valid usage
if [ x = 'x' ]
then
echo "Usage: read emailId
if echo "${emailId}" | grep '^[a-zA-Z0-9]*@[a-zA-Z0-9]*\.[a-zA-Z0-9]*$' >/dev/null; then
echo Valid
else
echo Not Valid
fi
<email address>"
exit 1
fi
# grabbing fields
user=`echo | cut -f1 -d\@`
host=`echo | cut -f2 -d\@`
mxhost=`host -t mx $host | cut -f7 -d\ `
len=`echo $mxhost | wc -c`
len=`expr $len - 2`
mxhost=`echo $mxhost | cut -b1 -$len`
# compose email commands
echo -ne "helo test.com\r\n" > mailcmd
echo -ne "mail from: test\@test.com\r\n" >> mailcmd
echo -ne "rcpt to: \r\n" >> mailcmd
echo -ne "quit\r\n" >> mailcmd
# check for mail results
mailresult=`cat mailcmd | nc $mxhost 25| grep ^550 | wc -c`
if [ $mailresult -eq 0 ]
then
echo "is valid"
exit 0
else
echo "is not valid"
exit 1
fi
# clean up
rm mailcmd
<email address>"
exit 1
fi
mailcmd=`mktemp`
# grabbing fields
user=`echo | perl -p -e 's/^([^@]+)@([^\@]+)$//g'`
host=`echo | perl -p -e 's/^([^@]+)@([^\@]+)$//g'`
mxhost=`host -t mx $host|perl -p -e 's/.* ([^ ]+)\.$//g'|sort -R|tail -1`
# compose email commands
echo -ne "helo example.com\r\n" > $mailcmd
echo -ne "mail from: <[email protected]>\r\n" >> $mailcmd
echo -ne "rcpt to: <>\r\n" >> $mailcmd
echo -ne "quit\r\n" >> $mailcmd
# check for mail results
mailresult=`cat $mailcmd | nc $mxhost 25| grep ^550 | wc -c`
if [ $mailresult -eq 0 ]
then
echo "is valid"
exit 0
else
echo "is not valid"
exit 1
fi
# clean up
rm $mailcmd
Filename"
exit 0
fi
#: Verifying the Hotmail adressess.
#: First verify the network Connections
C_R="\e[01;31m" ## Colors
C_B="\e[01;30m"
C_G="\e[01;32m"
C_END="\e[00m"
SMTPSERV=`host -t mx hotmail.com |grep 5 | grep mx2.hotmail.com |cut -d " " -f 7| sed 's/\.$//'`
ping -c2 $SMTPSERV >/dev/null
if [ "$?" -eq 0 ]
then
echo -e "Internet Connection" "\t\t\t\t\t\t$C_G[ OK ]$C_END"
echo -e "$SMTPSERV is AVAILABLE."
echo -n "Verifing"
for (( i=0; i<5; i++ ))
do
echo -n ".."
sleep 1
done
echo
else
echo -e "Internet Connection:" "\t\t\t\t\t\t$C_R[ FAIL ]$C_END" ""
echo -e "$SMTPSERV is Unavialable."
echo -e "Check your Network settings."
exit 0
fi
COUNT=0
RM_FILE="validemails.txt"
rm -f $RM_FILE
cat | while read LINE; do
{
MAFR="MAIL FROM: <[email protected]>"
MATO="RCPT TO: <$LINE>"
#: ^variablies declared for not get escaped in the next cat command, where
#: we set the $MAFR in the expect script.
cat << __EOF > e-veri
#!/bin/expect
#:
#: Date: 2011/14/12 01:14 PM
#: Author: Louay Mshelim_at_ [email protected]
#: Discription: Expect Script to Verify/Validate the Hotmail Adresses.
#:
set VMAFR "$MAFR"
set VMATO "$MATO"
spawn nc -C mx4.hotmail.com 25
expect "Sending"
send "HELO mx4.hotmail.com\r"
expect "OK"
send "$VMAFR\r"
expect "OK"
send "$VMATO\r"
expect "250"
send "quit\r"
expect eof
__EOF
#: Running the expect script and extracting the Results.txt
expect e-veri > Results.txt
grep 550 Results.txt >/dev/null
if [ "$?" -eq 0 ]
then
echo -e $LINE >> invalid.txt #invalid E-mails
else
echo -e "$LINE" >> validemails.txt
fi
}
done
echo -e "Valid E-mail have been saved to $C_R[ validemails.txt ]$C_END"
#: END
回答by GeekTantra
Here is an improved and working version of the script by codevour:
这是 codevour 脚本的改进和工作版本:
##代码##回答by Christopher Klewes
You mean something like this?
你的意思是这样的?
##代码##回答by Kernel
My any suggestion for the latter script, check work with multiple servers that accept checks without authentication, using nslookup who knows:
我对后一个脚本的任何建议,检查与接受未经身份验证的检查的多个服务器的工作,使用 nslookup 谁知道:
For example code in: http://www.vivaolinux.com.br/script/Simples-Verificador-de-Email-Gmail
例如代码:http: //www.vivaolinux.com.br/script/Simples-Verificador-de-Email-Gmail
see the code below in the site.
请参阅网站中的以下代码。
I hope I have contributed and I hope to collaborate as well.
我希望我有所贡献,我也希望合作。
Thank you.
谢谢你。
回答by Arumuga Raja
You can use like this,
你可以这样使用,
##代码##
