With the subprocess.Popen
we can call any executable under the currently logged-in user credentials like so:
import subprocess
cmd = ['C:\Windows\system32\executable.exe']
proc = subprocess.Popen(cmd, cwd=os.path.dirname(RAR_EXE), stdout=subprocess.PIPE)
output = proc.stdout.read()
Would it be possible to call and execute the same executable.exe
under another user credentials assuming that the user's name and the password are known, such as:
username = 'steve'
password = 'password123`
答案 0 :(得分:0)
This question doesn't really have anything to do with Python. You're just asking how to execute a command as another user from the Windows command line. The runas program will do that for you:
Usage as given in the link:
runas [{/profile | /noprofile}] [/env] [{/netonly | /savecred}] [/smartcard] [/showtrustlevels] [/trustlevel] /user: " "
Where:
- /profile
Loads the user's profile. This is the default. This parameter cannot be used with the /netonly parameter.- /no profile
Specifies that the user's profile is not to be loaded. This allows the application to load more quickly, but it can also cause a malfunction in some applications.- /env
Specifies that the current network environment be used instead of the user's local environment.- /netonly
Indicates that the user information specified is for remote access only. This parameter cannot be used with the /profile parameter.- /savecred
Indicates if the credentials have been previously saved by this user. This parameter is not available and will be ignored on Windows Vista Home or Windows Vista Starter Editions. This parameter cannot be used with the /smartcard parameter.- /smartcard
Indicates whether the credentials are to be supplied from a smartcard. This parameter cannot be used with the /savecred parameter.- /showtrustlevels
Displays the trust levels that can be used as arguments to /trustlevel.- /trustlevel
Specifies the level of authorization at which the application is to run. Use /showtrustlevels to see the trust levels available.- /user: " "
Specifies the name of the user account under which to run the program, the program name, and the path to the program file. The user account name format should be @ or \.- /?
Displays help at the command prompt.
It doesn't look like there's any built-in way to provide the user's password, so you'll have to set up input and output pipes to be able to provide it when prompted. You may find this task easier with Pexpect, which is a third-party module for automating subprocess keyboard interaction.