Remotely run a command on a ssh-server with a script

You can pass a command (or list of commands, separated by ;) to a SSH connection like this:

ssh user@server-address "./foo 1"

If you have a local script that outputs 0 or 1, you can simplify things further:

ssh user@server-address "./foo `/path/to/your/script`"

The code in backticks executes before anything else and its output is put into the line dynamically.


As Oli commented, you can tell SSH to send commands. You could modify your script so that if your command line arg is 1 it sends ssh user@server "shutdown -h now". Keep in mind that you'll have to be superuser on the other machine to shut it down.

EDIT: Instead of using root, as is suggested in the comments put user into the sudoers file as being able to shut down the machine without a password.