When I execute esxcli from the commandline, a command such as
esxcli --server=222.222.222.222 --username=foo@user.local --password='bar' --vihost=111.111.111.111 network vswitch standard portgroup add -p vlan1 -v switch1
works fine. Calling from subprocess however always fails because it cannot recognize the namespace. This is because it puts the namespace and command in quotes, instead of adding it directly. My current code from the subprocess call is:
import settings
import subprocess
subprocess.call(["esxcli",
"--server="+ settings.vserver,
"--username="+ settings.user,
"--password=\'"+ settings.pwd + "\'",
"--vihost="+ settings.host,
"network vswitch standard portgroup add",
"-p "+ settings.newpgname,
"-v "+ settings.newpgswitch])
When I run subprocess.list2cmdline
, I get:
esxcli --server=222.222.222.222 --username=foo@user.local --password='bar' --vihost=111.111.111.111 "network vswitch standard portgroup add" "-p vlan1" "-v switch1"
Note that the namespace and the arguments after it are all in quotations.
Most of the questions I've found about subprocess deal with shell=True, not anything about additional non hyphen-prefixed arguments.
How can I make subprocess run the correct command, without the extra quotes?
With your code, you're saying that fifth argument to the executable should be the string "network vswitch standard portgroup add". In order to pass that on the command line, it has to be quoted. Instead, you should make those be separate arguments, just like they would be on the command line. The same applies for the flag arguments; the executable doesn't expect a single argument "-v switch1", but rather two arguments "-v" and "switch1". Thus you should do:
subprocess.call(["esxcli",
"--server="+settings.vserver,
"--username="+settings.user,
"--password="+settings.pwd,
"--vihost="+settings.host,
"network","vswitch","standard","portgroup","add",
"-p",settings.newpgname,
"-v",settings.newpgswitch])