msd_2
msd_2

Reputation: 1187

Set administrator privileges to subprocess.check_call() in Python

I call two executables from a python script one which needs Administrator privileges and one which does not need them. Is there any way by which I can set the Administrator privilege before executing the executable so that it does not ask me for my password.

My script is as follows

import subprocess

subprocess.check_call(['DoesnotNeedAdminPrivilege.exe'])
subprocess.check_call(['NeedsAdminPrivilege.exe'])

I tried to run this script from cmd by starting the cmd to run as an Adminstrator. Is there any way to pass these admin rights to the second executable so that it works without any problem

Upvotes: 0

Views: 22568

Answers (2)

HCLivess
HCLivess

Reputation: 1073

I found a nasty workaround

    f = open('test.cmd', 'w+')
    f.write("execute.exe")
    f.close()

    os.system("runas /savecred /profile /user:Administrator \"test.cmd\"")

or you can use subprocess

Upvotes: 2

Back2Basics
Back2Basics

Reputation: 7806

Try this out for entering an administrative password.

import subprocess as sp

sp.check_call(['DoesnotNeedAdminPrivilege.exe'])
prog = sp.Popen(['runas', '/noprofile', '/user:Administrator', 'NeedsAdminPrivilege.exe'],stdin=sp.PIPE)
prog.stdin.write('password')
prog.communicate()

Here are the docs on:

Popen - http://docs.python.org/2/library/subprocess.html#popen-constructor

runas - http://technet.microsoft.com/en-us/library/cc771525.aspx

if this works will depend on the program NeedsAdminPrivilege.

Upvotes: 1

Related Questions