1
votes

I'm trying to use python Subprocess module to enable/disable Ethernet connection from python code. Below is my code in which the first step is looking for the available "Ethernet Connections" and the next step enables/disables the ethernet connection according to the parameter passed in "%interfaces%".

for /f "skip=2 tokens=3*" %%A in ('netsh interface show interface') do set interface=%%B

netsh interface set interface %interface%  ENABLED

Now when using in python I couldn't pass the variable, not sure if it's even possible though. Passing only command as below works as expected:

import subprocess
subprocess.call('netsh interface set interface Ethernet10 ENABLED')

I want to do something like:

import subprocess
subprocess.call (set x = 'Ethernet0')
subprocess.call('netsh interface set interface x  ENABLED')
2

2 Answers

3
votes

subprocess.call takes a list as an argument:

subprocess.call(['netsh', 'interface', 'set', 'interface', x, 'ENABLED'])

You could instead pass shell=True and your string would work, but it is a security risk, since an user could for example call another command by using $(command_here)

If you still want to use a string, you could use shlex.split.

0
votes

Use string formatting, interpolation, or concatenation:

x = 'Ethernet10'
subprocess.call('netsh interface set interface ' + x + ' ENABLED')