对收到的电子邮件运行 Linux 脚本
我编写了一个小脚本,用于在维基百科上查找作品,然后将结果打印到命令行。我希望能够通过电子邮件将该单词发送到我的服务器,然后让我的服务器通过电子邮件将结果发回给我。
到目前为止,我有一个名为“wiki”的新用户,电子邮件将发送到该用户。我收到电子邮件很好。在 /etc/aliases 中,我已将传入的电子邮件重定向到我编写的脚本。
# See man 5 aliases for format
wiki: "|/home/wiki/scripts/wiki"
效果很好。
我的脚本可以从命令行找到,就好像我
$ ./wiki <whatever>
在向 [电子邮件受保护]
我的 wiki 脚本权限是:
-rwxr-xr-x 1 wiki wiki 427 2011-04-18 22:54 wiki
出了什么问题!我需要设置什么权限? 任何帮助表示赞赏。
编辑(2011 年 4 月 18 日晚上 8:20): 这是我的剧本。
#!/bin/bash
read MSG
echo $MSG >> "newfile"
FROM=$(echo "$MSG" | cut -d " " -f2)
DATA=$(echo "$MSG" | cut -d " " -f3)
if [ MSG ]
then
RTN=`nslookup -q=txt $DATA.wp.dg.cx | grep "text =" | cut -d"=" -f2`
echo $RTN | sendmail -s "wikipedia: '$DATA'" $FROM
else
echo wilkipedia nslookup. Please supply a command line argument.
fi
I have written a small little script that looks up a work on wikipedia and then prints the result to the command line. I want to be able to email that word to my server and then have my server email me the results back.
So far I have a new user named 'wiki' where the e-mails are being sent to. I am receiving the e-mails fine. In /etc/aliases I have redirected incoming emails to my script I wrote.
# See man 5 aliases for format
wiki: "|/home/wiki/scripts/wiki"
That works fine.
My script works find from the command line, as if i typed
$ ./wiki <whatever>
I get permission denied errors when I sent an email to [email protected]
My wiki script permissions are:
-rwxr-xr-x 1 wiki wiki 427 2011-04-18 22:54 wiki
What is wrong! What permissions do I need to set?
Any help is appreciated.
EDIT (4/18/11 8:20pm):
This is my script.
#!/bin/bash
read MSG
echo $MSG >> "newfile"
FROM=$(echo "$MSG" | cut -d " " -f2)
DATA=$(echo "$MSG" | cut -d " " -f3)
if [ MSG ]
then
RTN=`nslookup -q=txt $DATA.wp.dg.cx | grep "text =" | cut -d"=" -f2`
echo $RTN | sendmail -s "wikipedia: '$DATA'" $FROM
else
echo wilkipedia nslookup. Please supply a command line argument.
fi
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据您的邮件服务器(例如 postfix),您可能需要将其配置为允许管道邮件。大多数情况下,使用 ~/.procmailrc 最简单。
Depending on you mailserver (e.g. postfix) you may need to configure it to allow piping mail. Most often, using ~/.procmailrc is easiest.
这就是我为解决问题所做的...
我废弃了 bash 脚本,并在 python 中编写了相同的函数。我使用 python 是因为它的电子邮件解析功能。我还通过一些字符串转义预防代码运行用户输入,以便我可以安全地将其传递给 python 的
subprocess.popen()
方法。我试图将标准输入写入本地文件,以便我可以看到在哪里解析文本,但是邮件应用程序(邮件或后缀,不确定)没有写入文件的权限 - 可能是一件好事结束。为了调试,我只是以字符串形式返回原始标准输入(电子邮件标头和所有内容),然后通过电子邮件将其发回给自己,看看发生了什么。
我设置 /etc/aliases 来读取
wiki: "|/home/wiki/scripts/wiki.py"
wiki.py 的权限是
4 -rw-r--r-- 1 wiki 邮件 1902 2011-04-19 21:04 wiki.py
这样邮件程序就可以成功地将其传递给您的脚本。如果您的脚本中有错误并且没有通过电子邮件返回给您,您也可以检查“/var/log/mail.log”的输出。如果邮件到达脚本,但由于语法或错误而出错,您应该会收到来自 MAILER-DAEMON 的电子邮件回复,说明邮件无法送达。
我没有使用
procmail
或有.procmailrc
文件。/etc/aliases
工作得很好。它将您的电子邮件传递到标准输入流,并在 python 中尝试以下操作:extra=""
而1:
行= sys.stdin.readline()
如果没有线:
休息
额外 = 额外 + line.strip(" ")
This is what I did to take care of my problem...
I scrapped my bash script, and wrote the same function in python. I used python because of its email parsing functionality. I also ran the user input through some string escape prevention code so that I could safely hand it off to python's
subprocess.popen()
method.I was trying to write the stdin to a local file so I could see where to parse the text, however the mail application (mail or postfix, not sure) doesn't have permissions to write files - Prob a good thing in the end. To debug, I just returned the raw stdin (email header and all) in a string and emailed it back to myself to see what was going on.
I set /etc/aliases to read
wiki: "|/home/wiki/scripts/wiki.py"
permissions on wiki.py are
4 -rw-r--r-- 1 wiki mail 1902 2011-04-19 21:04 wiki.py
and this way the mail program successfully hands it off to your script. You can also go check "/var/log/mail.log" for output if you have errors in your script and nothing gets emailed back to you. If the mail reached the script, but error-ed out because of syntax or a bug, you should get an email reply from MAILER-DAEMON stating that it was undeliverable.
I didn't use
procmail
or have a.procmailrc
file./etc/aliases
worked just fine. It passes your e-mail to the stdin stream, and in python try this:extra=""
while 1:
line = sys.stdin.readline()
if not line:
break
extra = extra + line.strip(" ")