Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you "echo" quotes using python's os.system()?

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"

like image 590
insecure-IT Avatar asked Mar 26 '15 18:03

insecure-IT


1 Answers

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')
like image 98
Tom Fenech Avatar answered Sep 18 '22 23:09

Tom Fenech