SSH - Run Shell Script Trough SSH

Preface

In the case that you're writing a shell script where:
- is needed to execute set of commands on remote server
- and after it the script shall continu on local server
- where it's needed to keep simple style of the script without too menay files.

For case like this you can probably think about using SSH, like comunication protocol and to use this example for your script.

Syntax

In this case we'll need to understend several points to use this kind of integration.

Connect to remote server

To connect to remote server wi'll use openSSH. After connecting to remote server we'll start shell interpreter, so that we can exacute remote commands.

Basic syntax

The idea here is to:

  • Open SSH session and to start remote shell interpreter.
  • Use pipe instead of STDIN
  • Use output file (with help of pipe) as STDOU

Simple Example:

#!/bin/sh                                                 # Local Client Shell interpreter in Bash Script

ssh root@<SSH Server> "/bin/sh">> /tmp/test <<EOL         # Connect to "<SSH Server>"
                                                          # Start on remote server shell interpreter "/bin/sh"
                                                          # STDOUT - redirect to local file on Client: "/tmp/test"
                                                          # STDIN - Put each line to remote shell to STDIN untill "EOL" then exit

         <command> ;                                      # Set of commands to execute on remote server
         <command> ;
         <command> ;
         ...
         <command> ;
EOL
                                                          # EOL - end of the commands to be executed on remote server
                                                          # EOL - Terminate SSH session

Variables

In general there is only one thing that we will need to keep in mind. For the rest rules we consider to have standard behavior like we’d write standard shall script.

The point that I have mentioned is to understand where will be the variable interpreted.

  • If it will be on local ssh client
  • Or on remote ssh server

Local shell variable

In the case that you’ll use local variables and you’d like to use them on remote server you can continue to use it like you have been using them at standard shall scripts.

At the time when the variable will be sent to SSH server it will be interpreted to his value.

Example - Script that will use local Client Variable on SSH Server:

#!/bin/sh
var_a="Hello Worl" ;                                            # Local variable on client

ssh root@<SSH Server> "/bin/sh">> /tmp/test <<EOL               # Open SSH connection to SSH server and log STDOUT to "/tmp/test"
   echo $var_a ;                                                # Execute command on SSH server with local Client Variable
EOL ''
                                                                # EOL will close the SSH sesion.

Result - This will be present at output file:

# cat /tmp/test
Hello Worl

Remote shell variable

In the case that you’d like to create variable on remote server and use it on remote server. It’s important to keep in mind that you need to escape the “$” character with “\” before you’d like to use it.

Example - Script that will use Remote Variable on SSH Server:

#!/bin/sh
var_a="This is local variable" ;                                # Local variable on client

ssh root@<SSH Server> "/bin/sh">> /tmp/test <<EOL               # Open SSH connection to SSH server and log STDOUT to "/tmp/test"

   var_a="This is remote Variable" ;                            # Variable created on Remote SSH server

   echo "Local var: " $var_a ;                                  # Execute command on SSH server with Local Client Variable
   echo "Remote var: " \$var_a ;                                # Execute command on SSH server with Remote Variable

EOL ''
                                                                # EOL will close the SSH sesion.

Result - This will be present at output file:

# cat /tmp/test
Local var:  This is local variable
Remote var:  This is remote Variable
Navigation
Print/export
QR Code
QR Code wiki:infrastructure_tools:ssh:ssh-run_shell_script_trough_ssh (generated for current page)