Departments & Programs

More

ssh to run remote commands



Copyright Phillip Farrell. Last revision August 3, 2004

Table of Contents:
  1. Getting ssh programs for your computer
  2. ssh for remote login
  3. ssh authentication methods
  4. scp and sftp for file transfers
  5. ssh to run remote commands

In addition to using ssh to get a complete remote login session, you can use it to run a single command on a remote system and get the output back to your local system. You can use this to quickly run a single command on the remote system (e.g., ls to see a list of files). Or you can use ssh remote commands as part of a data pipeline of multiple commands, some of them on the local system, and others on the remote system.

Syntax:
      ssh systemname 'command_and_arguments'

You must put the entire remote command and its arguments in quotes if it contains any special characters that the local shell might otherwise try to interpret (you want them to be interpreted by the remote shell).

The standard input of your local shell (e.g., your terminal) becomes the standard input of the remote command, and the standard output of the remote command is routed back to the standard output of your local shell (e.g., your terminal).

ssh can participate in pipes, in which one program runs on the local system and one on the remote system, and ssh takes care of connecting the standard output of one to the standard input of the other across the network. Unless you have enabled "password-less" authentication, ssh must still prompt for your password on the remote system, but it will prompt directly to the terminal and this will not interfere with the data pipeline.

For example:
      ssh kunabasin 'ls -1 /' | grep '^d'

In this example, the ls program is run on the remote computer kunabasin, and its standard output is routed back via the pipe to the grep program running on the local computer.

Comments or Questions?