Running Python Script as Another User

Python script to launch a program as a different user

This is how to open programs with Python

 import os
import subprocess
os.system("runas /user:cpandl\cgreen")
subprocess.Popen([r"U:\notme\program.exe"])

For more info on how to run with alternate credentials visit https://social.technet.microsoft.com/wiki/contents/articles/1410.windows-how-to-run-with-alternate-credentials-and-open-elevated-command-prompts.aspx

While running python script as another user (with sudo), input string taken as variable name

input behaves differently in Python 2.x and 3.x. The other account has Python 2.x set as its default, or has a Python 2.x executable first in its PATH. Your usual account, on the other hand, is using a Python 3.x executable. Probably the best way to do this is to use the full path to the Python executable you want to use i the script's "shebang" line. For example:

#!/usr/bin/python3

(This path may need adjusting for your system. Try typing which python in your account to see what it's using.)

How to run python process as another Windows user

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.

Running one python script from another script with command line argument having executable in it

Imagine I have 2 files, the first file is a.py and the other is b.py and I want to call the a.py from b.py.

The content of a.py is:

print('this is the a.py file')

and the content of b.py is:


import os

stream = os.popen('python3 a.py')
output = stream.read()

print(output)

Now when I call b.py from terminal I get the output I expect which is a.py print statment


user@mos ~ % python3 b.py
this is the a.py file

You can do this with subprocess too instead of os module.

Here is a nice blog I found online where I got the code from: https://janakiev.com/blog/python-shell-commands/

What is the best way to call a script from another script?

The usual way to do this is something like the following.

test1.py

def some_func():
print 'in test 1, unproductive'

if __name__ == '__main__':
# test1.py executed as script
# do something
some_func()

service.py

import test1

def service_func():
print 'service func'

if __name__ == '__main__':
# service.py executed as script
# do something
service_func()
test1.some_func()

How to start another program and keep it running after Python script finishes?

The solution is actually easier then it seemed :]

We can just use os.popen to run command in cmd/pipe, this will make those processes not dependent on the python process!
So let's just do it:

import os

os.popen("notepad.exe")
os.popen("notepad.exe")

print("Bye! Now it's your responsibility to close new process(es) :0")

this served as my inspiration, tho this solution works a little differently


Windows-only:

Also if you don't want to run several Popen's (through os.popen) to open one cmd.exe and use it instead:

import subprocess
from time import sleep

path = r"C:\Windows\System32\cmd.exe"
p = subprocess.Popen(
[path],
bufsize=-1,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
stdin=subprocess.PIPE)

def comunicate(process, message):
process.stdin.write(message)
process.stdin.flush()

comunicate(p, b'notepad.exe\n')
comunicate(p, b'notepad.exe\n')

sleep(0.1)
comunicate(p, b'exit\n') # closes cmd

print("Bye! Now it's your responsibility to close new process :0")

How can I make one python file run another?

There are more than a few ways. I'll list them in order of inverted preference (i.e., best first, worst last):

  1. Treat it like a module: import file. This is good because it's secure, fast, and maintainable. Code gets reused as it's supposed to be done. Most Python libraries run using multiple methods stretched over lots of files. Highly recommended. Note that if your file is called file.py, your import should not include the .py extension at the end.
  2. The infamous (and unsafe) exec command: Insecure, hacky, usually the wrong answer. Avoid where possible.

    • execfile('file.py') in Python 2
    • exec(open('file.py').read()) in Python 3
  3. Spawn a shell process: os.system('python file.py'). Use when desperate.


Related Topics



Leave a reply



Submit