SSH - Shell command


In general it is possible to say that in case when you’ll need to run a remote command on SSH Server it not necessary to connect to terminal / console of SSH Server. With SSH protocol it is possible to send command directly to the SSH Server without connecting to a terminal / console of the SSH Server. This is really handy in the case that you are writing for example a script that will connect to several servers only for running an command to get the data from SSH Server.


It is possible to use this example on SSH Client site

[user@SSH_Client ~]$  ssh user@SSH_Server "uname -a"                                           # Connect to SSH Server and run command "uname -a"
user@ssh_server's password:
Linux SSH_Server 2.6.32-431.el6.x86_64 #1 SMP Fri Nov 22 03:15:09 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux    # Result of command
[user@SSH_Client ~]$

Simple Example

For example you have more SSH Servers and you need to know if some process is running on this servers (I will check crond process).
How to do it:
1.) Connect manually to all servers and check this
2.) Use SSH protocol and SSH Agent (as we have our public key stored on all servers)
3.) Another that are not related to SSH or not secure enough.

On the SSH Client server you can write short script:

#!/bin/bash                                              # Short example of " "

loggin_name="user";                                      # User name used for SSH Server access
list_of_servers="SSH_Server1 SSH_Server2";               # List of SSH Server

for i in $list_of_servers                                # For all SSH Servers
    echo $i":";
    ssh -l $loggin_name $i "ps -C crond" ;               # Connect to SSH Server and run the command
    echo "";

exit 0;

In case like this it is really handy to use SSH Agent and SSH Key's instead Passowrd


[user@SSH_Client ~]$ ./
  PID TTY          TIME CMD
 1202 ?        00:00:01 crond

  PID TTY          TIME CMD
 1211 ?        00:00:01 crond
[user@SSH_Client ~]$
QR Code
QR Code wiki:infrastructure_tools:ssh:ssh-shell-command (generated for current page)