This question already has an answer here:
- Setup and use SSH ControlMaster Session in a Shell Script 1 answer
Imagine I have a bash script that executes commands on a remote machine via ssh:
# Do something here
ssh otheruser@host command1
# Do something else
ssh otheruser@host command2
# Do most local tasks
This script prompts me to enter credentials for otheruser@host multiple times. Is there a safe, easy, and accepted way to cache these credentials for the lifetime of the script but guarantee that they are lost after the script ends (either normally or when an error occurs)? Maybe a solution will use ssh-agent?
I am looking for something like this:
special_credential_saving_command_here # This will prompt for credentials
ssh otheruser@host command1 # This will not prompt now
ssh otheruser@host command2 # This will not prompt either
My motivation here is to avoid entering the credentials multiple times in the same script while not running the risk of those credentials persisting after the script has terminated. Not only is entering the credentials cumbersome, it also requires I wait around for the script to finish so that I can enter the credentials rather than leave it to run on its own (it's a long running script).