In this tutorial, How to Run a Local Shell Script on a Remote SSH Server. You can pass entire scripts over SSH without having the .sh file on the remote Linux server.
To run a local shell script on a remote SSH server, you can use the
ssh command with the following syntax:
ssh user@remote_host 'bash -s' < local_script.sh
Here’s a breakdown of the command:
user: Replace this with the username for the remote SSH server.
remote_host: Replace this with the IP address or hostname of the remote SSH server.
local_script.sh: Replace this with the path to your local shell script that you want to run on the remote server.
Pass The Script Over to Standard Input
ssh user@remotehost 'bash -s' < myscript_onLocal.sh
- The bash -s command means “execute the following commands in a new bash session.”
- The -s flag makes it read from the standard input
- The < myscript_onLocal.sh bit will read a local script file into standard input.
The output terminal is as follows:
[vagrant@localhost ~]$ cat myscript_onLocal.sh #!/bin/bash echo "Target server" cd /home pwd w [vagrant@localhost ~]$ [vagrant@localhost ~]$ ssh firstname.lastname@example.org 'bash -s' < myscript_onLocal.sh email@example.com's password: Target server /home 14:00:46 up 21 min, 1 user, load average: 0.00, 0.00, 0.00 USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT vagrant pts/0 10.0.2.2 13:40 17:54 0.00s 0.00s -bash [vagrant@localhost ~]$
Running Remote Commands Inside a Script
ssh firstname.lastname@example.org 'bash -s' <<'ENDSSH' # The following commands run on the remote host lsb_release -a ls -l pwd ENDSSH
The <<‘ENDSSH’ directive makes a “here-document” structure
The output as the picture below
You have to Run a Local Shell Script on a Remote SSH Server. I hope will this your helpful. Thank you for reading the DevopsRoles page!