Using Subprocess.call() To Pass Commands To Execute By Cmd
I am trying to set my IP address to a specific one on a LAN Network. To do this i have tried using the following: import subprocess command='netsh interface ipv4 set address name='
Solution 1:
If you use a string of args you need shell=True
:
import subprocess
command="netsh interface ipv4 set address name='Local Area Connection* 4' source=static address=192.168.173.234 mask=255.255.255.0 gateway=192.168.0.1"
subprocess.call(command,shell=True)
You can do it without shell=True
which I would recommend by passing a list of args:
import subprocess
command = ['netsh', 'interface', 'ipv4', 'set', 'address', 'name=Local Area Connection* 4', 'source=static', 'address=192.168.173.234', 'mask=255.255.255.0', 'gateway=192.168.0.1']
subprocess.call( command)
Or let shlex.split split the args for you:
import shlex
subprocess.call(shlex.split(command))
using check_call
in place of call may also be preferable as call
will not raise an error if there is a non-zero exit status where check_call will.
Solution 2:
If there is netsh.exe
somewhere in the search path (assuming you are on Windows) then you could pass the command string as is:
import subprocess
subprocess.check_call(command)
Post a Comment for "Using Subprocess.call() To Pass Commands To Execute By Cmd"