o's!
Maybe you can help me with this. I can't find an answer to my specific questions, because there is an obvious solution which I'm not allowed to use. But first things first, the context:
In my company, which is a service provider, we administrate a bunch of Linux servers. Some of my colleagues has for a long time been running a BASH script from a source server, that then performs some tasks over SSH on a number of remote Linux servers. The tasks it performs has to be executed as root, so what the script does is it authorizes the source server as root on the remote Linux servers via SSH (the remote servers has the source servers public SSH key). Then what happened is a new security policy was enforced and now root login over SSH is denied. So the mentioned method no longer works.
The solution I keep finding, which we are by policy not allowed to do, is to create an entry in the sudoers file allowing sudo to root without password for the specific user. This is the terms and they have to obey that. The only procedure that is allowed is to log on to the target server with your personal user, and then sudo su - to root WITH password. Cocky as I apparently was, I said, "It should be possible to have the script do that automatically", and the management was like "Cool, you do it then!" and now I'm here at Stack Overflow, because I know this is where bright minds are.
So this is exactly what I want to do with a BASH script, and I do not know if it's possible or how it's done, I really hope you can help me out:
Imagine Bob, he's logged into the source server, and he wants to execute the script against a target server. Knowing that root over SSH doesn't work, the authorization part of the script has been upgraded. When Bob runs the script, it prompts him for his password. The password is then stored in a variable (encrypted would be amazing) and the script then logs on the target server as his user (which is allowed) and then automatically elevates him to root on the target server using the password he entered on the source server. Now the script is root and it runs its tasks as usual.
Can it be done with BASH? and how?
UPDATE:
The Script:
## define code to be run on the remote system
remote_script='sudo -S hostname'
## local system
# on the local machine: prompt the user for the password
read -r -p "Enter password for $host: " password
# ...and write the password, followed by a NUL delimiter, to stdin of ssh
ssh -t 10.0.1.40 "$remote_script" < <(printf '%s\0' "$password")
The error:
[worker@source ~]$ sh elevate.sh
Enter password for : abc123
elevate.sh: line 10: syntax error near unexpected token `<'
elevate.sh: line 10: `ssh -t 10.0.1.40 "$remote_script" < <(printf '%s\0' "$password")'