473,405 Members | 2,141 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,405 software developers and data experts.

Setting up a new user and environment from within a python script

Hello,

I have written a script that uses environment variables set during
a particular users login in ".bash_profile" and ".profile".

I have changed to that users uid and gid in my python script using:

import os
os.setegid
os.setgid
os.seteuid
os.setuid

but I still am not picking up the needed environment. When I run:
os.environ I can see that I still have the environment of the user
that owns the python script.

I would like to maintain the original script owner but somehow pick up
the
correct environment for the targeted user. Several options looked to
create
an environment in a sub-process which I don't think is the correct
solution.

I could of course cut and paste the values from ".bash_profile" &
".profile"
but figured there is probably a better, cleaner way to do the same.

Searched this forum with no luck and checked several python
references.

What is the best practice to achieve this goal?

Thanks, Henry Hollenberg
Feb 7 '08 #1
2 2135
On Feb 7, 11:15 am, Henry Hollenberg <h...@rcwm.comwrote:
Hello,

I have written a script that uses environment variables set during
a particular users login in ".bash_profile" and ".profile".

I have changed to that users uid and gid in my python script using:

import os
os.setegid
os.setgid
os.seteuid
os.setuid

but I still am not picking up the needed environment. When I run:
os.environ I can see that I still have the environment of the user
that owns the python script.

I would like to maintain the original script owner but somehow pick up
the
correct environment for the targeted user. Several options looked to
create
an environment in a sub-process which I don't think is the correct
solution.

I could of course cut and paste the values from ".bash_profile" &
".profile"
but figured there is probably a better, cleaner way to do the same.

Searched this forum with no luck and checked several python
references.

What is the best practice to achieve this goal?

Thanks, Henry Hollenberg
Since you're running the python script as root (the only we seteuid
would work) you could call the script using "su" and rely on it to set
the user's environment:

su - otherusername /path/to/your/script

Other than that, the alternatives are to parse the user's dot files
and set the appropriate env variables from within your script, but
that approach is fraught with problems.

Hope that helps a little.

e.
Feb 7 '08 #2
On Feb 7, 3:30 pm, ericbrun...@gmail.com wrote:
On Feb 7, 11:15 am, Henry Hollenberg <h...@rcwm.comwrote:
Hello,
I have written a script that uses environment variables set during
a particular users login in ".bash_profile" and ".profile".
I have changed to that users uid and gid in my python script using:
import os
os.setegid
os.setgid
os.seteuid
os.setuid
but I still am not picking up the needed environment. When I run:
os.environ I can see that I still have the environment of the user
that owns the python script.
I would like to maintain the original script owner but somehow pick up
the
correct environment for the targeted user. Several options looked to
create
an environment in a sub-process which I don't think is the correct
solution.
I could of course cut and paste the values from ".bash_profile" &
".profile"
but figured there is probably a better, cleaner way to do the same.
Searched this forum with no luck and checked several python
references.
What is the best practice to achieve this goal?
Thanks, Henry Hollenberg

Since you're running the python script as root (the only we seteuid
would work) you could call the script using "su" and rely on it to set
the user's environment:

su - otherusername /path/to/your/script

Other than that, the alternatives are to parse the user's dot files
and set the appropriate env variables from within your script, but
that approach is fraught with problems.

Hope that helps a little.

e.
Right you are. Running it as a cron job. I'll try that trick in
our cron.daily script. Just figured since you can change to another
user from inside python their would be a way to change to another
user's environment as well....ie something comparable to:
source .profile in bash

Thanks hgh.
Feb 7 '08 #3

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

Similar topics

4
by: Sami Viitanen | last post by:
Hello, Is there any other way to set variables than os.putenv(). Putenv doesn't actually put any values to actual system variables.. I'm trying to set CVSEDITOR variable automatically from...
3
by: Greg Lindstrom | last post by:
Hello- I am running python 2.3. on an HP-9000 box running Unix and have a POSIX script that sets up my production environment. I would like to run the script from inside a python routine and...
0
by: Fran Tirimo | last post by:
I am developing a small website using ASP scripts to format data retrieved from an Access database. It will run on a Windows 2003 server supporting FrontPage extensions 2002 hosted by the company...
3
by: David Durkee | last post by:
Hi, I'm trying to write a script I can run from tcsh in Terminal (on Mac OS X) that will set environment variables that can be accessed by subsequent commands I execute in that session. Not...
4
by: Phil Campaigne | last post by:
Hello, I originally installed postgresql as root user and now I am setting up a development environment with cvs and a java ide and tomcat. I have everything with the exception of postgresql...
5
by: pinkfloydhomer | last post by:
I have a Makefile target that uses a python script, like: %.abc: %.def python myscript.py The problem is that myscript.py and some modules that myscript.py imports are not in the current...
1
by: laredotornado | last post by:
Hi, I'm using PHP 4.4.4 on Apache 2 on Fedora Core 5. PHP was installed using Apache's apxs and the php library was installed to /usr/local/php. However, when I set my "error_reporting"...
0
by: George Trojan | last post by:
Is there a utility to parse a Bourne shell environment file, such as ..bashrc, and set the environmental variables from within a Python script? What I mean is an equivalent to shell dot command. I...
1
by: Torsten Bronger | last post by:
Hallöchen! I start a python script with subprocess from another Python script with python = subprocess.Popen(, stdout=subprocess.PIPE, stderr=subprocess.PIPE) However, this child script...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.