Switch User in within a Shell Script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Switch User in within a Shell Script
# 1  
Old 10-07-2010
Switch User in within a Shell Script

Hi Experts,

I'm trying to write a shell script to stop few things where i have to use another user to execute a command. Otherwise it will not work.

Your help is really appreciated

Thanks,
This User Gave Thanks to Afi_Linux For This Post:
# 2  
Old 10-07-2010
Try:
Code:
su - user -c "command"

These 2 Users Gave Thanks to bartus11 For This Post:
# 3  
Old 10-07-2010
A search on unix forum gave me these results:

https://www.unix.com/unix-advanced-ex...ll-script.html
This User Gave Thanks to panyam For This Post:
# 4  
Old 10-07-2010
Hi,

The following command is prompting for the password. Is there anyway to specify the password and it does not promt again?

su - user -c "command"
# 5  
Old 10-07-2010
  • You an wrap su in an expect script (exposing the password a bit),
  • or set up passwordless login for you-here to him-here using rsh/remsh/rexec/ssh/ssh2,
  • or hit some custom service like a web site running under that user
  • or have some sort of server/daemon process running or frequent cron under that user looking for some sort of message (file, named pipe, email, tcp/udp IP port or other similar network facility like netbui) to trigger the commands you need,
  • or write an setuid bit friendly C program or restricted shell owned by other id that can be executed by you. Setuid is not shell friendly, as it drops LD_LIBRARY_PATH or the like used for dynamic linking and must be compiled staticly or with -R to give it library path for every necessary lib file.
This User Gave Thanks to DGPickett For This Post:
# 6  
Old 10-07-2010
I have no issues with the exposing the password. What would be the exact command though?

Is this right? >> su - user/password -c "command"
# 7  
Old 10-07-2010
Man Page for su (OpenSolaris Section 1m) - The UNIX and Linux Forums

Expect-5.43.0

#!/usr/local/bin/expect --
# Name: autoexpect - generate an Expect script from watching a session
#
# Description:
#
# Given a program name, autoexpect will run that program. Otherwise
# autoexpect will start a shell. Interact as desired. When done, exit
# the program or shell. Autoexpect will create a script that reproduces
# your interactions. By default, the script is named script.exp.
# See the man page for more info.
#
# Author: Don Libes, NIST
# Date: June 30 1995
# Version: 1.4
This User Gave Thanks to DGPickett For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to switch user in shell script?

HI in a server we can't login with root user directly but i can login with different user and then i can switch to root user by su command Requirement is there anyway where i can write a script without mentioning password in file as mentioning the root password is not the... (3 Replies)
Discussion started by: scriptor
3 Replies

2. Shell Programming and Scripting

How to Switch from Local user to root user from a shell script?

Hi, I need to switch from local user to root user in a shell script. I need to make it automated so that it doesn't prompt for the root password. I heard the su command will do that work but it prompt for the password. and also can someone tell me whether su command spawns a new shell or... (1 Reply)
Discussion started by: Little
1 Replies

3. Shell Programming and Scripting

How to switch user in shell scripting (without root)?

Hi everyone: I need create a script that must switch user and then must execute certain commands, sadly neither my user nor the second user have no privileges for su - , I've tried everything but seems su doesn't accept input redirection, please help me, ... (4 Replies)
Discussion started by: ooilinlove
4 Replies

4. 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

5. UNIX for Dummies Questions & Answers

How to switch the user before executing a shell script from web page??

hi, i want to execute a shell script as a different user. the flow is like this. there is a html web page from which i have to call a shell script. web server is apache. to call the shell script from html page, a perl script is required. so the html page calls the perl script and the perl... (2 Replies)
Discussion started by: Little
2 Replies

6. Shell Programming and Scripting

How to switch user using shell script ?

Hi, script1.sh script2.sh script3.sh From above, script1.sh is the main script which is executed from root user, creates installation directory, changing ownership and execution rights etc..etc.. and finally calls scripot2.sh and script3.sh to create the database as well as for post... (1 Reply)
Discussion started by: milink
1 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

How to switch user in shell scripting (without sudo)?

Hi everyone: I have a big trouble, I need create a script that must switch user and then must execute certain commands, sadly neither my user nor the second user have no privileges for sudo, I've tried everything but seems su doesn't accept input redirection, please help me, it's very... (8 Replies)
Discussion started by: edgarvm
8 Replies

9. Shell Programming and Scripting

switch user inside a script

Hi Is there any way to switch user inside a shell script? (4 Replies)
Discussion started by: ./hari.sh
4 Replies

10. Shell Programming and Scripting

switch user inside a script

Hi, I wrote a unix script that will perform differnt tasks on bahalf of number of users. I use "sudo" to run the script. The problem is when I execute the command: su - user -c "xxx " > output_file, I get the system output header frm the su command. Is there a way to get rid of it instdead of... (2 Replies)
Discussion started by: nimo
2 Replies
Login or Register to Ask a Question