title: Python: execute shell command using variable
body: <p>In my Lab I need to change the interface on a server to a random IPv6 address. I have two scripts. The first script makes up a random IPv6 address and works perfectly. I'm trying to take the output of that script and use it as a variable in the second script. Then run an ifconfig command to set that IPv6 address on that interface. That's the general run down of what I'm trying to do.
  Below is the script that generates the random IPv6 address, ipv6_gen.py</p>
  
  <pre><code>from random2 import seed, getrandbits
  from ipaddress import IPv6Network, IPv6Address
  
  subnet = u'1234:5678:ab:cde::/64'
  
  seed()
  network = IPv6Network(subnet)
  address = IPv6Address(network.network_address + getrandbits(network.max_prefixlen - network.prefixlen))
  
  print(address)
  </code></pre>
  
  <p>This one should take the output of the first and execute the ifconfig command.</p>
  
  <pre><code>import ipv6_gen
  import os, commands
  import subprocess
  import shlex
  
  ipv6=ipv6_gen
  shell_cmd = ("sudo ifconfig en0 inet6 add %s" %ipv6)
  
  subprocess_cmd = shlex.split(shell_cmd)
  subprocess.call(subprocess_cmd)
  </code></pre>
  
  <p>I'm getting the following errors:</p>
  
  <pre><code>ubuntu:~$ python change_ipv6.py 
  1234:5678:ab:cde:8087:1bb6:b264:acdd
  &lt;module: Host name lookup failure
  getaddrinfo: ipv6_gen: -3
  ipv6_gen: Host name lookup failure
  ifconfig: `--help' gives usage information.
  </code></pre>
  
  <p>when I run ifconfig the IPv6 address isn't set. Not sure what I'm doing wrong, tried researching this for awhile now and got nothing. Any help would be appreciated.</p>