I am writing a shell script where I want to automate service Restart process on multiple Unix servers. I as a normal user do not have rights to run services restart commands. Only s_user has rights to run them.
Procedure to restart services:
- Login to server with my username and password.
To Run the Restart service I need to switch user to s_user using below command: ( I need this command without password prompt)
sudo su - s_user
The above command prompts for password where I will provide my password to restart services. This procedure I have to do in all the servers.I want to skip the password prompt by supplying the password to su through stdin.
The following commands I have tried so far:
1. echo 'password'|sudo -S su -c " .../bin/services stop" Output: [sudo]password for abc.xyz: Sorry, user abc.xyz is not allowed to execute '/bin/su - s_user' -c /bin/services stop' as root on [Servername]
In the above command I gave my password. I have also tried giving the s_user password but it gives below output:
[sudo]password for abc.xyz: Sorry,try again. [sudo]password for abc.xyz: sudo:pam_authenticate:Conversation error
I replaced ‘su’ with ‘u’ in the below command, but still I am getting the same output error as 1st command.
2. echo 'password'|sudo -S u -c " .../bin/services stop"
I have provided s_user in the below command, but still I am getting the same output error as 1st command.
3. echo 'password'|sudo -S su - s_user -c ".../bin/service stop"
Next I have tried the below command.
4. echo 'my password'|su -c ".../bin/services stop" -s /bin/sh abc.xyz Output: Standard in must be a tty
I have also tried the below command.
5. su -c "command_to_restart_the_services" -s /bin/sh abc.xyz
I am able to run the command successfully.
6. echo password|sudo -S su - user-to-switch sudo -S su - user-to-switch
The above command still ask for the password.
• I cannot use “expect”, it’s not installed in my machines.
• I cannot edit the "sudoer" file. I do not have rights.