开发者

using os.system in python to run program with parameters

开发者 https://www.devze.com 2022-12-22 08:59 出处:网络
How do I get python to run sudo openvpn --cd /etc/开发者_StackOverflowopenvpn --config client.ovpn

How do I get python to run sudo openvpn --cd /etc/开发者_StackOverflowopenvpn --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

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 ..

It will only work if you are running this in a GUI environment. You can simply try to call it with 'gksu' or ('kdesu' or 'kdesudo'), it would look like this in a gnome session:

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). It processes the arguments better in many cases.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号