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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to switch user in shell scripting (without root)?
# 1  
Old 01-13-2016
Hammer & Screwdriver 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,



Code:
#!/usr/local/bin/expect

set userpass "mypassword"

send "su - myuser012"
expect "Password:"
send "$userpass\r"
expect eof

after run script this error

Code:
./autos.sh: line 7: send: command not found
./autos.sh: line 8: expect: command not found
./autos.sh: line 9: send: command not found
./autosl.sh: line 10: expect: command not found

My os = SunOS
# 2  
Old 01-13-2016
Hello Smilie

1)
Lets have a look if expect is installed, and if so, where it exactly is.
Code:
which expect

2)
expect is not bash (or ksh,zsh,sh)!

3)
How did you start the script?
If expect is installed, but you started the expect script using bash or sh, then the script is executed as such (bash/sh).

Greets

EDIT:
4)
Either way (expect or (any) shell), the user has to type the password.
Or you should switch to credential files.

hth
# 3  
Old 01-20-2016
The sudo tool is designed to give you the choice and it is probably the best way.

Incidentally, you should not really try to force the password in like this. If you have a process to run that is automated, i.e. you don't fire it off as a real person on the command line, then you should really run it as a non-personal account. You can then give that account the sudo privilege to not require a password.

The problem is that if you script this, then anyone able to read your script will know the account password. Additionally, good practice would have you changing the passwords regularly and that might require editing every script each time.

How far have you got with this now?


Kind regards,
Robin
# 4  
Old 01-20-2016
can you do this using
Code:
 chmod +s script

and then change the owner of the script to the second user.
# 5  
Old 01-20-2016
I am not sure but there should be spawn to call command


Code:
#!/usr/local/bin/expect
spawn su -
expect "Password:" { send "passwd\r" }
expect "# " { send "uname -a\r" }
expect "# " { send "pwd\r" }
expect "# " { send "q" }
exit


Another way is :
Check if you have SSH or telnet to the server available. you can telnet or ssh to server. You could use other user.


Also I use
Quote:
autoexpect
while writing script. Please check man pages before you start.
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

Switch between root and user in the same script.

I am writing a korn shell script where i need to switch to root in between and again exit from root to normal user and continue other commands. Is that possible to switch between these two in the same script? (1 Reply)
Discussion started by: santosh2626
1 Replies

4. UNIX for Dummies Questions & Answers

Not able to switch to other users using su -user from root

Hi all, I have a small problem. When I log in as root and try to switch to any other user using su -user, then it is giving an error saying libncurses.so permission denied. Can you help me? Thank you in advance. Sai. (1 Reply)
Discussion started by: sai2krishna
1 Replies

5. UNIX for Dummies Questions & Answers

Shell Scripting for Router, Switch & FW deviation

Hi, I have written a script for finding deviation for router,switch &fw. It is working fine on linux server. But when I try on sunos 5.10 OS it showing "grep: illegal option -- A". I have used grep -C and grep -A. How it will work on sunos? Help me out please !! (12 Replies)
Discussion started by: GautamSK
12 Replies

6. Solaris

Unable to switch back to root from user

Hi, I am new to Solaris and when i am trying to Switch to root login from user, system throws message saying permissions denied... Steps I Followed: Created a user and logged into that user by SU username tried to come back to root but system throws message "permissions denied" ... (7 Replies)
Discussion started by: VijaySolaris
7 Replies

7. Shell Programming and Scripting

switch user from local user to root in perl

Hi Gurus, I have a script that requires me to switch from local user to root. Anyone who has an idea on this since when i switch user to root it requires me to input root password. It seems that i need to use expect module here, but i don't know how to create the object for this. ... (1 Reply)
Discussion started by: linuxgeek
1 Replies

8. Shell Programming and Scripting

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, (16 Replies)
Discussion started by: Afi_Linux
16 Replies

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

10. Shell Programming and Scripting

Non-interactive user switch to root

Is is possible switch user from a non-root user to root user without entering the password interactively inside a korn shell script which is run by a non-root user? e.g. I have a non-root user called infodba who is in dba group and I want to create a shell script which is executed by infodba... (5 Replies)
Discussion started by: stevefox
5 Replies
Login or Register to Ask a Question