how do you use ssh in a shell script

Tags: bash shell ssh ksh rsh
Question!

When I try to use an ssh command in a shell script, the command just sits there. Do you have an example of how to use ssh in a shell script?



Answers

You need to put your SSH public key into the ~/.ssh/authorized_keys file on the remote host. Then you'll be able to SSH to that host password-less.

Alternatively you can use ssh-agent. I would recommend against storing the password in the script.

By : Steve M


You can use expect command to populate the username/password info.



First, you need to make sure you've set up password-less (public key login). There are at least two flavors of ssh with slightly different configuration file formats. Check the ssh manpage on your system, consult you local sysadmin or head over to http://stackoverflow.com/questions/7260/how-do-i-setup-public-key-authentication.

To run ssh in batch mode (such as within a shell script), you need to pass a command you want to be run. The syntax is:

ssh host command

If you want to run more than one command at the same time, use quotes and semicolons:

ssh host "command1; command2"

The quotes are needed to protect the semicolons from the shell interpreter. If you left them out, only the first command would be run remotely and all the rest would be run on the local machine.



This video can help you solving your question :)
By: admin