cancel
Showing results for 
Search instead for 
Did you mean: 

Use ssh in a script

SOLVED
Go to solution
June Cui
Occasional Advisor

Use ssh in a script

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

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/identity.pub 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:
#!/bin/sh
#
# myscript --- does stuff
# This script is running in the shell launched with ssh-agent
# Load the passphrase into memory
cat .passphrase |ssh-add -p
ssh

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