Skip to content
Advertisement

(py2.7) long “echo” via subprocess

I’m starting to write some python code to initiate new servers/VM(s) with proxy servers

This hasn’t been working due to syntax error

subprocess.call("echo  'http_proxy="http://proxy.srx.com:80/" nhttps_proxy="http://proxy.srx.com:80/" nno_proxy="127.0.0.1, localhost, *.internaldns.com, 172.27.255.88, 172.27.255.89" ' >> /etc/environment ", shell=True);

so I tried dividing n into

#!/usr/bin/python

import os;
import subprocess;

subprocess.call(["ls", "/etc/"]);

print "=====Next Command=====";

print "echo to environment";

subprocess.call("echo "http_proxy="http://proxy.srx.com:80/"" >> /etc/environment", shell=True);
subprocess.call("echo "https_proxy="http://proxy.srx.com:80/"" >> /etc/environment", shell=True);
subprocess.call("echo "no_proxy="127.0.0.1, localhost, *.internaldns.com, 172.27.255.88, 172.27.255.89"" >> /etc/environment", shell=True);


print "=====Next Command=====";
subprocess.call("ls /root/", shell=True);

#if CentOS yum.conf

#if Ubuntu apt.conf
~                     

But still this syntax error?

~# ./sys.py 
  File "./sys.py", line 12
    subprocess.call("echo "http_proxy="http://proxy.srx.com:80/"" >> /etc/environment", shell=True);
                                    ^
SyntaxError: invalid syntax

Thanks! I’ve tried many combinations of ‘ and ” but no luck.

Advertisement

Answer

The problem is "echo "http_proxy="http://proxy.srx.com:80/""... is not a valid string

you should use:

subprocess.call("""echo "http_proxy="http://proxy.srx.com:80/"" >> /etc/environment""", shell=True);
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement