My script needs to execute as root, and for some tasks as someuser, I also need to get someuser $HOME path, and thats where comes the problem.
For that job (find user home) I'm using os.path.expanduser.
As mentioned the script needs to change his user with su and run the code to get user home:
test_home.py
from os.path import expanduser
def user_home():
home = expanduser("~")
return home
print user_home()
The main script calls the test_home.py with su:
# su someuser -c /tmp/test_home.py
/home/someuser
Perfectly fine. But for AIX and Solaris the script doesn't get someuser $HOME, instead, it gets the root $HOME.
AIX: Executing as root
su someuser -c /tmp/test_home.py
/
Solaris: Executing as root
su someuser -c /tmp/test_home.py
shell-init: error retrieving current directory: getcwd: cannot access parent directories: Permission denied
/root
I've tested this code on several distros:
- RedHat
- SLES
- HP-UX
- AIX
- Solaris
- Debian
The problem it's only on AIX and Solaris. Does anyone knows why?
UPDATE
Using login with with - or -l works, but I can't use su - on my script. Any thoughts on how to overcome this?
I was trying to not use a shell approach like the above but I'm getting out of options
cat /etc/passwd | grep someuser | cut -d: -f 6