creating teaming from user input
<p>I'm trying to create a centos 7 a networking teaming (bonding) using a user input in a python script. C</p>

<pre><code>import socket

# IP Address
IPADDR = socket.gethostbyname(socket.gethostname())
print IPADDR
# Netmask
NETMASK = raw_input("Enter Netmask address: ")
PREFIX = sum([bin(int(x)).count("1") for x in NETMASK.split(".")])
print NETMASK
# Gateway
GATEWAY = raw_input("Enter Gateway address: ")
print GATEWAY
# Run command and substitution
os.system("nmcli con add type team con-name team0 ifname team0 config '{"runner":{"name": "activebackup"}}'")
os.system("nmcli con mod team0 ipv4.addresses IPDDR/PREFIX")
os.system("nmcli con mod team0 ipv4.gateway GATEWAY")
os.system("nmcli con mod team0 connection.autoconnect yes")
os.system("nmcli con mod team0 ipv4.method manual")
os.system("nmcli con mod team0 ipv6.method ignore")
</code></pre>

<p>When I run the script I get this errors:</p>

<pre><code> File "team0.py", line 16
    os.system("nmcli con add type team con-name team0 ifname team0 config '{"runner":{"name": "activebackup"}}'")
                                                                                  ^
SyntaxError: invalid syntax
</code></pre>

<p>Can someone help to find what I'm doing wrong.
Thanks</p>

<p>The syntax error is occurring because you are not escaping the quote <code>"</code> characters. The interpreter thinks that the string has ended and then trips.</p>

<p>You can use a backslash <code>\</code> to escape the quote:</p>

<pre><code>os.system("nmcli con add type team con-name team0 ifname team0 config '{\"runner\":{\"name\": \"activebackup\"}}'")
</code></pre>

<p>In addition, please note that your declared variables are not being picked up when they are declared in string literals. You will need to modify these commands with <a href="https://docs.python.org/2/library/string.html#string.Formatter.format" rel="nofollow"><code>format</code></a>, for example:</p>

<pre><code>os.system("nmcli con mod team0 ipv4.addresses {}/{}".format(IPDDR, PREFIX))
os.system("nmcli con mod team0 ipv4.gateway {}".format(GATEWAY)")
</code></pre>

<p>See the above documentation and other SO questions for more info about how to use <code>format</code>.</p>

python