473,377 Members | 1,153 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,377 software developers and data experts.

Running one Python program from another as a different user

Greetings-
This is on Linux... I have a daemon running as root and I want to
execute another Python program as another user (a regular user). I have
the name of the user and can use the 'pwd' and 'grp' modules to get
that user's user and group ids. What I don't understand is how to then
go about launching that new program. I had considered having the
launched program switch itself back to the target user (somehow), but
the launched program is graphical in nature (PyQt), and I am afraid of
X11 locking out the display to user root (many distros seem to ship
with server access for user root turned off). That might prevent the
launched program from even starting?

Any ideas? My Google searching was not successful in figuring this
out...

Thanks,

Don

Aug 12 '05 #1
6 7098
dw******@gmail.com wrote:
Greetings-
This is on Linux... I have a daemon running as root and I want to
execute another Python program as another user (a regular user). I have
the name of the user and can use the 'pwd' and 'grp' modules to get
that user's user and group ids. What I don't understand is how to then
go about launching that new program. I had considered having the
launched program switch itself back to the target user (somehow), but
the launched program is graphical in nature (PyQt), and I am afraid of
X11 locking out the display to user root (many distros seem to ship
with server access for user root turned off). That might prevent the
launched program from even starting?

Any ideas? My Google searching was not successful in figuring this
out...


from subprocess import *

def run_as(username):
pipe = Popen(["su", username], stdin=PIPE, stdout=PIPE)
(out, err) = pipe.communicate("whoami")
print out,
if __name__ == "__main__":
import sys
for user in sys.argv[1:]:
run_as(user)
Aug 12 '05 #2
Thanks, that looks very promising...
Is there a solution for pre-Python v2.4? I have to have code that works
on 2.x, 0<=x<=4. Do I just use the os.popen instead?

-Don

Aug 12 '05 #3
"dw******@gmail.com" <dw******@gmail.com> writes:
This is on Linux... I have a daemon running as root and I want to
execute another Python program as another user (a regular user). I have
the name of the user and can use the 'pwd' and 'grp' modules to get
that user's user and group ids. What I don't understand is how to then
go about launching that new program.


Use os.fork to start a new process and have the new process do
os.setuid to the new user. Then os.execl (or whatever) to run the
target program.
Aug 13 '05 #4
"dw******@gmail.com" <dw******@gmail.com> writes:
Greetings-
This is on Linux... I have a daemon running as root and I want to
execute another Python program as another user (a regular user). I have
the name of the user and can use the 'pwd' and 'grp' modules to get
that user's user and group ids. What I don't understand is how to then
go about launching that new program. I had considered having the
launched program switch itself back to the target user (somehow), but
the launched program is graphical in nature (PyQt), and I am afraid of
X11 locking out the display to user root (many distros seem to ship
with server access for user root turned off). That might prevent the
launched program from even starting?

Any ideas? My Google searching was not successful in figuring this
out...


Well, Jeff already pointed out running su. You might also check the
os.setuid docs, and the setuid man page.

<mike
--
Mike Meyer <mw*@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Aug 13 '05 #5
dw******@gmail.com wrote:
Thanks, that looks very promising...
Is there a solution for pre-Python v2.4? I have to have code that works
on 2.x, 0<=x<=4. Do I just use the os.popen instead?


import os

def run_as(username):
pipe = os.popen("su %s" % username, 'w')
pipe.write("whoami")
if __name__ == "__main__":
import sys
for user in sys.argv[1:]:
run_as(user)
Aug 13 '05 #6
Jeff Schwab wrote:
dw******@gmail.com wrote:
Thanks, that looks very promising...
Is there a solution for pre-Python v2.4? I have to have code that works
on 2.x, 0<=x<=4. Do I just use the os.popen instead?


import os

def run_as(username):
pipe = os.popen("su %s" % username, 'w')
pipe.write("whoami")
if __name__ == "__main__":
import sys
for user in sys.argv[1:]:
run_as(user)


Jeff, may I suggest you to write down a new recipe for the ASPN Python
Cookbook based on these code snippets? This is exactly the kind of thing
the people is happy to find at ASPN.

CU
-----------------------------------
Alessandro Bottoni
Aug 13 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

220
by: Brandon J. Van Every | last post by:
What's better about Ruby than Python? I'm sure there's something. What is it? This is not a troll. I'm language shopping and I want people's answers. I don't know beans about Ruby or have...
226
by: Stephen C. Waterbury | last post by:
This seems like it ought to work, according to the description of reduce(), but it doesn't. Is this a bug, or am I missing something? Python 2.3.2 (#1, Oct 20 2003, 01:04:35) on linux2 Type...
5
by: Shalen chhabra | last post by:
Hey, Can anyone give me a snippet for running a python program over all the files in the directory. For ex: I have ten files in a directory and I want to run a python program against all of...
17
by: los | last post by:
Hi, I'm trying to create a program similar to that of Google's desktop that will crawl through the hard drive and index files. I have written the program and as of now I just put the thread to...
8
by: Paul Cochrane | last post by:
Hi all, I've got an application that I'm writing that autogenerates python code which I then execute with exec(). I know that this is not the best way to run things, and I'm not 100% sure as to...
8
by: Bernard Lebel | last post by:
Hello, I would like to know if there is a way to run a Python file under a different user account than the one logged in. Allow me to explain. There are a bunch of people here, they are "basic...
5
by: awalter1 | last post by:
Hi, I develop a graphical user interface (with pyGTK) where a click on a button shall launch a program P in background. I want to get the end of this program P but I don't want that my HMI be...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.