(py2.7)long" echo"通过子进程

时间:2016-03-05 03:32:59

标签: python subprocess

我开始编写一些pyhton代码来启动带有代理服务器的新服务器/ VM

由于语法错误

,这还没有奏效
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);

所以我尝试将\ n分成

#!/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
~                     

但是这个语法错误呢?

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

谢谢! 我已经尝试了许多组合的'和"但没有运气。

1 个答案:

答案 0 :(得分:0)

问题是"echo "http_proxy="http://proxy.srx.com:80/""...不是有效的字符串

你应该使用:

subprocess.call("""echo "http_proxy="http://proxy.srx.com:80/"" >> /etc/environment""", shell=True);