简体   繁体   English

在python中使用os.system()执行linux命令

[英]Using os.system() in python to execute linux command

I use script in python to fetching mails from Postfix server. 我在python中使用脚本从Postfix服务器获取邮件。 This script fetches mails, removes headers and leaves only body of email. 该脚本提取邮件,删除标题,仅保留电子邮件正文。 Then it uses this body to execute linux command by os.system() . 然后,它使用此主体通过os.system()执行linux命令。 For example I'm sending email with mkdir folder and script creates this. 例如,我要发送带有mkdir folder电子邮件,脚本将创建此mkdir folder

Problem was when I was sending ls by mail. 问题是当我通过邮件发送ls I've got response : not found ls 我得到回应: not found ls

Second problem is with mkdir cause it adds ^M to name from email. 第二个问题是mkdir因为它在电子邮件的名称中添加了^ M。 For example I sent mkdir folder and it created " folder ?". 例如,我发送了mkdir folder ,它创建了“ folder ?”。

Do you have any ideas ? 你有什么想法 ?

You've already identified the problem: the emails have ^M characters in them that you're not expecting. 您已经发现了问题:电子邮件中有您所期望的^ M个字符。 (CR LF is a common line ending convention; Unix usually does not like the CR). (CR LF是常见的行尾约定; Unix通常不喜欢CR)。

Try removing "\\r" from your command: command = command.translate(None, "\\r") . 尝试从命令中删除“ \\ r”: command = command.translate(None, "\\r")

I also urge you to carefully consider the security implications of running whatever commands are delivered by email. 我还敦促您仔细考虑运行通过电子邮件传递的任何命令所带来的安全隐患。 There is likely a much safer way to do what you're trying to accomplish. 可能有更安全的方法来完成您要完成的任务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM