0

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.

Ismael Padilla
  • 5,246
  • 4
  • 23
  • 35
  • 1
    You're trying to use the same quote that delimits the outer string inside the string without escaping it. Of course, this would be a whole lot easier if you weren't using `str` based commands with `shell=True`; `list` based command make the need for internal quotes much less frequent. Or in this particular case, skipping pointless `subprocess` usage in favor of just `open`ing the file and `print`/`write`ing to it directly. `with open('/etc/environment', 'a') as env:`, then in the block `env.write('http_proxy="http://proxy.srx.com:80/"\n')` – ShadowRanger Mar 05 '16 at 03:45
  • 1
    unrelated: don't name your Python scripts as standard Python modules such as `sys`. Otherwise, unexpected errors may happen, [example1](http://stackoverflow.com/q/25299371/4279), [example2](http://stackoverflow.com/q/27702506/4279), [example3](http://stackoverflow.com/q/28066014/4279), [example4](http://stackoverflow.com/q/27714497/4279), etc. – jfs Mar 05 '16 at 20:41
  • @J.F. Sebastian oh wow, i didn't know. thank you. –  Mar 06 '16 at 00:20

1 Answers1

0

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);
wong2
  • 34,358
  • 48
  • 134
  • 179
  • subprocess.call("""echo "http_proxy="http://proxy.srx.com:80/" \nhttps_proxy="http://proxy.srx.com:80/"" >> /etc/environment""", shell=True); (Now I replaced to that thank you!!) –  Mar 05 '16 at 07:04
  • @SndLt: is there any reason not to use pure Python here: `with open('/etc/environment', 'a') as f: print >>f, 'http_proxy=http://proxy.srx.com:80/'`? – jfs Mar 05 '16 at 20:30
  • Going to be doing a whole of bash shelling duties in python among others. (Just trying to get used to it) Is that what you would recommend for regular CentOS/Ubuntu chores? Thanks again –  Mar 06 '16 at 00:22
  • @SndLt : use any scripting language that you know best. If you use Python then don't run the shell if there are simple pure Python solutions available such as `echo` -> `file.write`, `ls` -> `os.listdir()`. If you have an existing bash script then run it as whole — no need to rewrite it if it is already works. – jfs Mar 06 '16 at 00:34