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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Henry Hollenberg

    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_prof ile" 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_prof ile" &
    ".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
  • ericbrunson@gmail.com

    #2
    Re: Setting up a new user and environment from within a python script

    On Feb 7, 11:15 am, Henry Hollenberg <h...@rcwm.comw rote:
    Hello,
    >
    I have written a script that uses environment variables set during
    a particular users login in ".bash_prof ile" 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_prof ile" &
    ".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.

    Comment

    • Henry Hollenberg

      #3
      Re: Setting up a new user and environment from within a python script

      On Feb 7, 3:30 pm, ericbrun...@gma il.com wrote:
      On Feb 7, 11:15 am, Henry Hollenberg <h...@rcwm.comw rote:
      >
      >
      >
      Hello,
      >
      I have written a script that uses environment variables set during
      a particular users login in ".bash_prof ile" 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_prof ile" &
      ".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.

      Comment

      Working...