How do you use ssh in a shell script?

Jon Ericson picture Jon Ericson · Aug 27, 2008 · Viewed 34k times · Source

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?

Answer

Mats Fredriksson picture Mats Fredriksson · Aug 27, 2008

Depends on what you want to do, and how you use it. If you just want to execute a command remotely and safely on another machine, just use

ssh user@host command

for example

ssh user@host ls

In order to do this safely you need to either ask the user for the password during runtime, or set up keys on the remote host.