Table of Contents
SSH - Shell command
Preface
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.
Syntax
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 " simple_ssh_script.sh " 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 do echo $i":"; ssh -l $loggin_name $i "ps -C crond" ; # Connect to SSH Server and run the command echo ""; done exit 0;
Result
[user@SSH_Client ~]$ ./simple_ssh_script.sh SSH_Server1 PID TTY TIME CMD 1202 ? 00:00:01 crond SSH_Server2 PID TTY TIME CMD 1211 ? 00:00:01 crond [user@SSH_Client ~]$