Login into different user on the same server

Hi All,

Please can you advise as to how should I write a shell script so that I can switch user from one user account to the other on the same server.

The requirements are user must be able to provide with a user name and it should then take a password which I have saved in a file and should be able to login into user sapce provided by user.

Cheers,
Shazin

You can use su - username command to switch user and expect command in order to pass the password to su command as follows:

#!/usr/bin/expect --

set password yourpassword

spawn su - username

expect "password"

send $password

1 Like

Hi,

I have written the belwo small script:

Now I am getting the below mentioned error:

Please can somebody assist.

Best Regards,
Shazin

Ensure that you save the file with name script.ksh and when you type the command ./script.ksh you are in the same directory within the file. Also you should make the file executable with "chmod +x script.ksh "

Hi,

These were taken care of.

Best Regards,
Shazin

Do you know "sudo"?

Study on it, it will resolve your problem.

I tried sudo, unfortunately, it didn't work for me.

<user> <server>:NOPASSWD su - <user>

so, if you find a solution, please do post it.

Hi

check you have expect pkg, it require to install separately

ls -l /usr/bin/expect

Regards
Aftab