Table of Contents
SSH - Run local Shell Script on remote server
Preface
Sometime it’s needed to run shell script on SSH Server even this script is located on client site.
May be you’re just looking for a way how to centralize your task management, without copping all your scripts to all managed servers.
In cases like this, it’s handy to know that SSH Protocol can help you.
Script – located on SSH Client
In our case we’ll create just simple shell script just to demonstrate the idea.
[root@SSH_Client ~]# cat script.sh uname -a ; hostname ;
Execute local Shell Script on remote SSH Server
To execute local Shell Script on remote SSH Server we’ll need to:
- Establish SSH connection.
- To start “bash” on Remote SSH Server, that will listen at standard input (STDIN)
- To read the script and transfer each command with help of SSH, to bash that is listening on remote SSH server.
- We’ll use “bash –s” for starting bash to listen on STDIN
- Man for “bash –s” :
[root@SSH_Client ~]# man bash ... -s If the -s option is present, or if no arguments remain after option processing, then commands are read from the standard input. This option allows the positional parame‐ ters to be set when invoking an interactive shell. ...
Example
[root@SSH_Client ~]# ssh root@SSH_Server "bash -e" <./script.sh Linux SSH_Server 2.6.32-431.20.3.el6.x86_64 #1 SMP Thu Jun 19 21:14:45 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux SSH_Server