Switch user without password inside shell


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Switch user without password inside shell
# 1  
Old 07-31-2014
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.

Code:
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 below command, sorry I don't have root permission to edit such files Smilie
Code:
USER_NAME   ALL=(ALL) ALL

Using: ksh, HP-UX. Someone please let know how to switch user without password.
# 2  
Old 08-01-2014
Does your server allow paswordless ssh sessions?

I'm thinking you could setup a public/private key pair for the user1 account and use ssh to execute the commands.
# 3  
Old 08-01-2014
Thanks Chubler_XL,

I just learned about ssh and tried in command prompt and it working as expected.

Code:
ssh -keygen

- To set up public/private key.

Code:
ssh host1

- To login to another host machine (host1)

Code:
ssh user1@host1

- To change user.

Code:
ssh user1@host1 -P password

- Provide password with ssh command (which will avoid prompting for password)



But here the problem is when I used this in shell, for ex:

Code:
#!/bin/ksh 
 
ssh user1@host1 -P password   #--> Trying to login as different user 
 
sleep 1 
 
ping host3 
 
- Recycle components step to come here

After login to user1, the ping and recycle components are not happening in user1.

Last edited by Roozo; 08-01-2014 at 03:52 AM.. Reason: Get rid of extraneous Italic tags inside CODE tags.
# 4  
Old 08-01-2014
Steps to be executed by ssh that come from the script (rather than from the script's standard input) need to be redirected:
Code:
#!/bin/ksh 
ssh user1@host1 -P password <<-EOF
        sleep 1 
        ping host3
        # other commands to be executed through ssh
EOF
# other commands to be executed after ssh session terminates.

Note that using -P password this way makes user1's password on host1 visible to anyone who runs ps while ssh is running. Consider putting the password in the here-document; then that user's password is only public to anyone with permission to read your script. Depending on your security requirements, you may want to explore other ways to enter the password.
# 5  
Old 08-01-2014
Hi Don,

I tried the above,

When executing the ssh command with or without EOF concept, it will ask for my personal user id "roozo" other than user1(machine user)

When executing the above inside shell, the below is happening where I have to enter my personal id say "roozo" and exiting from user1
Code:
Enter you User Id: stty : Not a typewriter 
logout user1 HP-UX

# 6  
Old 08-01-2014
Quote:
Originally Posted by Roozo
Hi Don,

I tried the above
What, exactly, did you try? There's a whole lot of "add your stuff here" in that code where we can't see what you actually added.

Show us exactly what you did, word for word, letter for letter, keystroke for keystroke.
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

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

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

6. Shell Programming and Scripting

switch as another user without password

I want to switch as another user without using password .Is it posiible ? I have one server B and I have logged in as username u1 but I want to login to that same server using username as u2 but I don't want to give the password for u2. (3 Replies)
Discussion started by: maitree
3 Replies

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

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