Showing results for 
Search instead for 
Did you mean: 

Use ssh in a script

Go to solution
June Cui
Occasional Advisor

Use ssh in a script

How can I set a ssh key to allow user to use ssh in a script without asking for password?
Thank you.
James A. Donovan
Honored Contributor

Re: Use ssh in a script

You'll need to make sure that RSA authentication is enabled in your sshd_config

1. Login as the user and generate a keypair using ssh-keygen. You can use a null passphrase, but I don't recommend it.
2. Add the contents of the $HOME/.ssh/ file to the $HOME/.ssh/authorized_keys file on all machines where you wish to log in using RSA authentication.
3. If you don't use a null passphrase then to execute your script, write a small wrapper script which uses ssh-agent to launch your script. 'ssh-agent myscript'
4. Your script will contain something like the following:
# myscript --- does stuff
# This script is running in the shell launched with ssh-agent
# Load the passphrase into memory
cat .passphrase |ssh-add -p

Carefully review all the man pages on the ssh commands.
Remember, wherever you go, there you are...