0

I use script in python to fetching mails from Postfix server. This script fetches mails, removes headers and leaves only body of email. Then it uses this body to execute linux command by os.system(). For example I'm sending email with mkdir folder and script creates this.

Problem was when I was sending ls by mail. I've got response : not found ls

Second problem is with mkdir cause it adds ^M to name from email. For example I sent mkdir folder and it created "folder?".

Do you have any ideas ?

BitMask777
  • 2,543
  • 26
  • 36
mac
  • 23
  • 8
  • Could you show some code, as `os.system("ls")` works perfectly for me. – matsjoyce Oct 24 '14 at 20:33
  • 3
    Consider using the `email` and `smtplib` modules for this task. Also hope that no one figures out your email address and sends `rm -rf /` :) – Dane Hillard Oct 24 '14 at 20:37
  • subprocess respones the same. This is part which leaves body and execute os.system. Previous is only fetching emails. for part in msg.walk(): #print only body of mail if part.get_content_maintype() == 'multipart': continue if part.get_content_subtype() != 'plain': continue payload = part.get_payload() print payload os.system(payload) if I use os.system("ls") it works but I need variable – mac Oct 24 '14 at 20:39

1 Answers1

1

You've already identified the problem: the emails have ^M characters in them that you're not expecting. (CR LF is a common line ending convention; Unix usually does not like the CR).

Try removing "\r" from your command: 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.

Community
  • 1
  • 1
nandhp
  • 4,680
  • 2
  • 30
  • 42
  • Thank you very much ! I've added payload = payload.translate(None, "\r") and it resolved both of problems. – mac Oct 24 '14 at 21:10