使用subprocess.call()来传递通过CMD执行命令 [英] Using subprocess.call() to pass commands to execute by cmd
问题描述
我想我的IP地址设置为一个LAN网络上的特定的。
要做到这一点,我已经使用以下尝试:
导入子
命令='netsh接口的IPv4设置地址名称=本地连接* 4源=静态地址= 192.168.173.234掩码255.255.255.0 = =网关192.168.0.1
subprocess.call([cmd.exe的命令])
这导致的唯一事情就是开机即没有做任何事情空的cmd.exe。
此外,为了将壳=真用什么?
当我尝试使用它,蟒蛇返回一个SyntaxError
更新:如果我使用:
命令='netsh接口的IPv4设置地址名称=本地连接* 4源=静态地址= 192.168.173.222掩码255.255.255.0 = =网关192.168.0.1
subprocess.check_call([的Netsh.exe,指挥])
的Python返回错误:
回溯(最后最近一次调用):
文件D:文件路径,8号线,上述<&模块GT;
subprocess.check_call([的Netsh.exe,netsh_cmd])
文件C:\\ Python27 \\ lib目录\\ subprocess.py,540线,在check_call
提高CalledProcessError(RET code,CMD)
CalledProcessError:命令'['的Netsh.exe','netsh接口的IPv4设置地址名称=本地连接* 4源=静态地址= 192.168.173.222掩码255.255.255.0 = =网关192.168.0.1']'返回非零退出状态1
如果有的Netsh.exe
某处的搜索路径(假设你是在Windows上),那么你可以通过命令字符串是:
导入子subprocess.check_call(命令)
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="Local Area Connection* 4" source=static address=192.168.173.234 mask=255.255.255.0 gateway=192.168.0.1'
subprocess.call(["cmd.exe", command])
The only thing this results in is the starting up of an empty cmd.exe that isn't doing anything.
Also, what for would the shell=True be used ? When I try to use it, python returns a SyntaxError
UPDATE: If I use:
command='netsh interface ipv4 set address name="Local Area Connection* 4" source=static address=192.168.173.222 mask=255.255.255.0 gateway=192.168.0.1'
subprocess.check_call(["netsh.exe", command])
Python Returns the error:
Traceback (most recent call last):
File "D:file path", line 8, in <module>
subprocess.check_call(["netsh.exe", netsh_cmd])
File "C:\Python27\lib\subprocess.py", line 540, in check_call
raise CalledProcessError(retcode, cmd)
CalledProcessError: Command '['netsh.exe', 'netsh interface ipv4 set address name="Local Area Connection* 4" source=static address=192.168.173.222 mask=255.255.255.0 gateway=192.168.0.1']' returned non-zero exit status 1
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)
这篇关于使用subprocess.call()来传递通过CMD执行命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!