Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to run a command on a ssh-server, but this command is determined by a script on my local machine. How do I do that?

An example for clarity:
I want to write a script here ( that takes an argument. If I run ./ 0 it should somehow send a shutdown signal to the server machine, but if I run ./ 1 it should send a restart signal.

I know how to manually login via ssh, and I've already set ssh-keys to skip passwords, but I don't know how to automate the procedure with a script.

share|improve this question
up vote 2 down vote accepted

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.

share|improve this answer
Logging remotely with root enabled is not recommended. Instead he should to setup the sudoers file to be able to execute the shutdown command without requiring a password. – João Pinto Dec 3 '10 at 15:46
Not quite true. They just have to be in the sudoers file as a user who can issue a shutdown without a password prompt. Not too hard to implement. – Oli Dec 3 '10 at 15:47
The shutdown was really just an example, you can change "root" to "user", just to make sure less knowledgeable readers don't try to remotely login as root. – Malabarba Dec 3 '10 at 16:44

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.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.