[英]using os.system in python to run program with parameters
How do I get python to run sudo openvpn --cd /etc/openvpn --config client.ovpn 如何让python运行sudo openvpn --cd / etc / openvpn --config client.ovpn
I'm trying the following at the minute without success 我现在正在尝试以下方面但没有成功
vpnfile2 = '/etc/init.d/openvpn'
cfgFile = 'client.ovpn'
os.system('sudo \"" + vpnFile2 + "\" --cd \"" + vpnpath + "\" --config \"" + cfgFile + "\"')
use the subprocess
module 使用
subprocess
模块
import subprocess
subprocess.call(['sudo', vpnFile2, '--cd', vpnpath, '--config', cfgFile])
This question has been posted awhile ago, but if someone comes across this (like me), there is another way to get privileges using the os.system method .. 这个问题已经发布了一段时间,但如果有人遇到这个(像我一样),还有另一种方法可以使用os.system方法获得权限。
It will only work if you are running this in a GUI environment. 它仅在您在GUI环境中运行时才有效。 You can simply try to call it with 'gksu' or ('kdesu' or 'kdesudo'), it would look like this in a gnome session:
您可以简单地尝试使用'gksu'或('kdesu'或'kdesudo')来调用它,它在gnome会话中看起来像这样:
import os
vpnfile2 = '/etc/init.d/openvpn'
cfgFile = 'client.ovpn'
os.system('gksu \"" + vpnFile2 + "\" --cd \"" + vpnpath + "\" --config \"" + cfgFile + "\"')
The prompt works, but I have not tested it to work with your code. 提示有效,但我没有测试它以使用您的代码。
If there is some reason you want to use os.system, instead of subprocess, I usually wash it through bash, so os.system('''sudo bash -c "command to run"''')
(Or sh or whatever shell you have). 如果有一些原因你想使用os.system而不是subprocess,我通常会通过bash来清洗它,所以
os.system('''sudo bash -c "command to run"''')
(或者sh或者其他什么)你有壳)。 It processes the arguments better in many cases. 在许多情况下,它更好地处理参数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.