6

I'm trying to write to a monit config file using standard bash scripting inside if python's os.system(), this string is what I'd like to mimic.

echo -e "\t" start program = \""/etc/init.d/snortd00 start\"" >> /etc/monit.d/ips_svcs.monit

Here are my attempts using os.system(). They all produce the same results. None of which are writing the quotes around /etc/init.d/snortd00 start

   os.system('echo -e \"\t\" start program = \""/etc/init.d/snortd00 start\"" >> /etc/monit.d/ips_svcs.monit')

   os.system('echo -e \"\t\" start program = \"\"/etc/init.d/snortd00 start\"\" >> /etc/monit.d/ips_svcs.monit')

   os.system('echo -e \"\t\" start program = "/etc/init.d/snortd00 start" >> /etc/monit.d/ips_svcs.monit')

   os.system('echo -e \"\t\" start program = "\"/etc/init.d/snortd00 start\"" >> /etc/monit.d/ips_svcs.monit')

This is what is being written using all four os.system() statments. start program = /etc/init.d/snortd00 start

I'm looking for this start program = "/etc/init.d/snortd00 start"

insecure-IT
  • 2,068
  • 4
  • 18
  • 26

2 Answers2

8

Just use a raw string to avoid double-escaping (once for python, once for the shell):

cmd = r'echo -e "\t" start program = \""/etc/init.d/snortd00 start\"" >> /etc/monit.d/ips_svcs.monit'
os.system(cmd)

As tripleee points out in the comments, os.system is being replaced by subprocess, so the code above would change to this:

subprocess.call(cmd, shell=True)

Better yet, just use python:

with open("/etc/monit.d/ips_svcs.monit", "a") as file:
    file.write('\t  start program = "/etc/init.d/snortd00 start"\n')
Tom Fenech
  • 72,334
  • 12
  • 107
  • 141
  • 2
    +1 for the proper Python code suggestion... Although, there should probably be a newline in the `write()` call, since it's not added automatically like `print ...` would... – twalberg Mar 26 '15 at 19:29
  • 3
    Perhaps also tangentially note that `os.system()` is [being replaced by `subprocess`](https://docs.python.org/2/library/subprocess.html) although the [recommended replacement code](https://docs.python.org/2/library/subprocess.html#replacing-os-system) is rather unattractive. But maybe that's just to encourage you to use Python's own facilities when that makes sense. – tripleee Mar 26 '15 at 20:37
7

Let's consider why your existing approaches are not working:

In this case, the \ is processed by Python, so the shell gets two consecutive " characters. The shell sees ""/etc..."":

os.system('echo -e \"\t\" start program = \""/etc/init.d/snortd00 start\"" >> /etc/monit.d/ips_svcs.monit')

This is the same as previous: the \ is processed by Python:

os.system('echo -e \"\t\" start program = \"\"/etc/init.d/snortd00 start\"\" >> /etc/monit.d/ips_svcs.monit')

In this case, the shell sees "/etc...":

os.system('echo -e \"\t\" start program = "/etc/init.d/snortd00 start" >> /etc/monit.d/ips_svcs.monit')

In this case also, Python processes \ and the shell sees ""/etc..."":

os.system('echo -e \"\t\" start program = "\"/etc/init.d/snortd00 start\"" >> /etc/monit.d/ips_svcs.monit')

Now, what you want:

os.system('echo -e \"\t\" start program = \\"/etc/init.d/snortd00 start\\" >> /etc/monit.d/ips_svcs.monit')

Here, Python processes \\ into \ and the Shell sees \", which invokes the escaping mechanism of the shell so echo really sees ".

juhist
  • 4,210
  • 16
  • 33