jonjo
jonjo

Reputation: 21

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

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 + "\"')

Upvotes: 2

Views: 7086

Answers (3)

Perkins
Perkins

Reputation: 2517

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.

Upvotes: 0

jscurtu
jscurtu

Reputation: 11

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.

Upvotes: 1

John La Rooy
John La Rooy

Reputation: 304137

use the subprocess module

import subprocess
subprocess.call(['sudo', vpnFile2, '--cd', vpnpath, '--config', cfgFile])

Upvotes: 8

Related Questions