Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Execute external command as another user

Status
Not open for further replies.

cmcclain0831

Programmer
Aug 4, 2007
3
US
I'm fairly new to Perl, which I'm using to do sysadmin and dba stuff, and I'm consistently running into a need to execute an external command as a user different from the user executing my Perl script, with all of that user's environment variables, privileges, etc. Seems as if that should be easy, but I've done a lot of research and am not finding it so.

Specifically, I'm running an installation script, as root or admin, that copies files to various places, does chmods and chowns, etc. All of that is going fine. However, since a lot the files I'm installing are Oracle-related scripts, one or more steps involve invoking Oracle's sqlplus to run one of these scripts.

Because of Oracle's environmental requirements, I really need to "become" the OS user who is the Oracle instance owner, with all of his environment variables and privileges, for the purpose of invoking sqlplus. Simply sourcing his .bash_profile doesn't do the trick, because of the privilege issue. I also tried:

($<,$>) = (getpwnam("oracle10"),getpwnam("oracle10");

...thinking that would give me oracle10's privileges, but that doesn't seem to work, either. I'm stumped; all I really want to do is say, "For the purpose of running sqlplus right now, I am oracle10, thank you, and make me be admin again when sqlplus finishes".

I understand that there are security issues involved when you assume another user's identity; in this case, I am doing this in a closed lab environment, not production, and want to become a less-privileged user, not more-privileged.

I will appreciate any help anyone can offer; I've spent almost as much time researching and playing with this issue as I have spent on the rest of Perl.
 
You might also try posing the question in forum219 , where the perl gurus hang out. Good luck.

I want to be good, is that not enough?
 
You might find this construct helpful:
su - oracle10 -c "/path/to/command"

or this if you want to run more than one command:
su - oracle10 -c "/path/to/command1; /path/to/command2"

We use this at System Boot (and Shutdown) to startup (or shutdown) Oracle databases from root.

I hope that helps.

Mike
 
Give this a try...

# runuser another-user script-name-or-program

/ip
 
There is sonmething called uid bit. You have to set the uid bit of that executable.
chmod u+s exe_name
 
I second Mike042's solution, that would be the 'normal' way to do what you require in my opinion.

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top