Switching user inside a shell script


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Switching user inside a shell script
# 1  
Old 12-06-2009
Switching user inside a shell script

Dear All,

I want to switch a user inside a shell script.My current user say x and user y to whom i want to switch both have login on the same server,the one on which i want to execute my script on.
"I want to do something like this
su - y
Password should be provided in the script itself.And then i want execute a simple command as user y and then switch back to user x.Please help me as i am stuck in middle of something.

Please note iam not the root user.



Thanks
Navjot
# 2  
Old 12-06-2009
If you want to run something as another user you either have to have SUDO set up to allow this (by root) or the owner of the other "program" should set the SUID bit.

On many (or maybe all?) systems SUID has no effect with shell scripts.

Code:
$ ls -l Test*
-r-sr-xr-x  1 user2  staff  8800 Dec  6 13:46 TestCProg # C Program
-r-sr-xr-x  1 user2  staff    35 Dec  6 13:19 TestScript # Shell script

$ id user2
uid=502(user2) gid=20(staff)

$ ./TestCProg > /dev/null &; ps -ef | grep TestCProg
[1]	97055
  502 97055 96971   0   0:00.00 ttys000    0:00.00 ./TestCProg
  501 97057 96971   0   0:00.00 ttys000    0:00.00 grep TestCProg

$ ./TestScript &; ps -ef | grep TestScript
[2]	97063
  501 97063 96971   0   0:00.00 ttys000    0:00.00 ./TestScript
  501 97065 96971   0   0:00.00 ttys000    0:00.00 grep TestScript

Another option is to use the Expect tool. This is the first time I've used it, so probably not very good, but...

Code:
/usr/bin/expect << !
spawn su - y
expect "Password:"
send "y_password\r"
send "who am i\r"
send "exit\r"
expect "exit"
!

$ who am i
x        ttys000  Dec  6 15:49
$ ./Test
spawn su - y
Password:
y$ who am i
y        ttys002  Dec  6 15:50 
y$ exit

It should also be possible to use SSH to do what you want, but you'd need to set up the keys the first time
# 3  
Old 12-07-2009
Hi scott,

Although the idea is great.But ,its not a very secured one.Is there any way through which i could pass the password through a file and then login and execute the command through other user and finally get back to the original user.
# 4  
Old 12-07-2009
Hi.

With su? No, I don't think so.

And what you are suggesting is no more secure than what I suggested. Storing passwords in text files in plain text can never be considered "secure"!
# 5  
Old 12-07-2009
Hi scott,
let me explain u it a little more clearly:-

Im working on same server with two ids say x and y:-

IM not the root:-
What i want to do is
1.login through x
2.then login through y'
3.exec `command x through user y > file on user x directory`
4.exit y

I cant change the permissions of the command/script that i will have to exec as user y.
# 6  
Old 12-07-2009
I understood what you were asking.

But you can't use su - y without being prompted for a password. (unless someone else here can show how - I certainly couldn't figure it out).

You can use RSH for this, but user "y" has to set up the .rhosts file. And, and I said, SUDO and SSH are other options.

If you want a password-less login, these are your options. Otherwise you have to enter a password manually.
# 7  
Old 12-07-2009
You could try this expect script:
however your password will be stored in plain text. if you have permission you can chmod it to 500 and only you(and roots) can read it, but your password will be in plain text still.
I haven't tested this code yet either.

Code:
#!/usr/bin/expect -f

log_file /tmp/status.log

set timeout 2

spawn su - y
 log_user 0
   expect "assword: "
   send "password\r"
   expect "*> "
 log_user 1
   send "commandme\r"
   expect "*> "
   send "exit\r"

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Switching from root to normal user takes me to user's home dir

Whenever i switch from root to another user, by doing su - user, it takes me to home directory of user. This is very annoying as i want to be in same dir to run different commands as root sometimes and sometimes as normal user. How to fix this? (1 Reply)
Discussion started by: syncmaster
1 Replies

2. Shell Programming and Scripting

Switching server in UNIX via shell script (SSH )

Requirement: I am writing a shell script which take some file from say Server1 and I have to execute some command on Server2 then I have to manipulate the data and based on that I have to produce report. Problem:I can schedule the shell script on only one server (Server1 or Server2) So, In the... (3 Replies)
Discussion started by: raks2301
3 Replies

3. Shell Programming and Scripting

Switch user without password inside shell

I want to switch to another user without password inside shell. I used the below command and it is not working. sudo su - user1 user1 is not in the sudoers file. This incident will be reported. I'm getting the above message. If I want to add user1 into the sudoers file using... (5 Replies)
Discussion started by: Roozo
5 Replies

4. Shell Programming and Scripting

Switching user inside a shell script doesn't seem to work

Linux version : Oracle Linux 6.4 Shell : Bash The following script will be run as root. During the execution, it should switch to oracle user and execute few commands. After googling and searching within unix.com , I came up with the following syntax ## Enclosing all commands in double... (7 Replies)
Discussion started by: John K
7 Replies

5. Shell Programming and Scripting

How to login as a different user inside a shell script?

hi, i want to login as a different user inside a shell script and then call another shell script from that script. how to do that? original script : script_A.sh so when the script_A.sh is called , i want to login as a different user and then call another shell script(script_B.sh) from... (3 Replies)
Discussion started by: Little
3 Replies

6. Shell Programming and Scripting

Login into another user from user inside script

now i have logged in username : ramesh in unix Now i have to created script file to login into another user and have run a command inside that user and after executing the command i have to exit from that user. Inside script, i have to login into su - ram along with password : haihow and have to... (4 Replies)
Discussion started by: rammm
4 Replies

7. Shell Programming and Scripting

Switch user inside shell script

Hi, I am trying to create one script where I have to login as another user inside the script to exeute some commands How can i achieve this? Many thanks in advance. (4 Replies)
Discussion started by: prarat
4 Replies

8. Shell Programming and Scripting

problem switching to directory using shell script

Hi, I have an issue with switching to other directory through shell script. I have used cd /Music" but that doesn't help me. Then I have also tried using alias proj 'Music' alias then I get the error permission denied but i set the chmod 777 Music. I am using cShell for my... (3 Replies)
Discussion started by: baluk
3 Replies

9. Shell Programming and Scripting

switching to another user in shell script...

Hi, I have a shell script in which I need to switch to another user and execute some commands and then come back to the original user. To make it more clear - I have to log in as user root then 'su' to jag - execute a script called backup.sh and then logout and come back to root again.. ... (1 Reply)
Discussion started by: bjagadeesh
1 Replies

10. Shell Programming and Scripting

switching user from root to ordinary user

Good day Guys!!! I am currently making a script in AIX, the script runs a SAS job, the owner of the script is the root, but the SAS jobs cannot be run by the root, as it should be run by a user 'sasia'. But inside the script, root creates a logfile, so what I need is just to su to sasia for the... (3 Replies)
Discussion started by: sasia
3 Replies
Login or Register to Ask a Question