How to Run a Local Shell Script on a Remote Machine Using SSH
Running a local shell script on a remote machine using SSH is a common task in system administration and software development. This process allows you to execute a script on a remote server without manually copying it over. Here’s how you can do it depending on your local machine’s operating system:
For Unix-based Systems (Linux/macOS):
If your local machine is Unix-based, you can use the ssh
command with input redirection to execute a local script on a remote machine. Here’s the basic syntax:
ssh user@remote_host 'bash -s' < local_script.sh
user@remote_host
: Replace this with your remote machine’s username and hostname/IP address.'bash -s'
: This tells the remote machine to execute a shell script passed via standard input.< local_script.sh
: The local script to be executed on the remote machine.
Example:
ssh root@192.168.1.100 'bash -s' < my_script.sh
This command runs my_script.sh
, located on your local machine, on the remote machine 192.168.1.100
as the root user.
For Windows Systems Using Plink:
If your local machine is running Windows, you can use Plink (a command-line interface to the PuTTY backend) to execute a local script on a remote machine.
plink user@remote_host -m local_script.sh
plink
: The command-line tool from PuTTY.user@remote_host
: Your remote machine’s username and hostname/IP address.-m local_script.sh
: The-m
option tells Plink to execute the local script on the remote machine.
Example:
plink root@192.168.1.100 -m my_script.sh
This command runs my_script.sh
, located on your Windows machine, on the remote machine 192.168.1.100
as the root user.
Handling Script Parameters
If your script requires parameters, you can pass them as follows:
For Unix-based Systems:
ssh user@remote_host 'bash -s -- arg1 arg2' < local_script.sh
arg1 arg2
: Parameters passed to the script.
Example:
ssh root@192.168.1.100 'bash -s -- parameter1 parameter2' < my_script.sh
For Windows Systems Using Plink:
Plink does not natively support parameter passing through the -m
option. However, you can modify the script itself to handle parameters, or use environment variables.
Passing Environment Variables
You can pass environment variables to the remote machine’s shell, which can be used by the script:
For Unix-based Systems:
ssh user@remote_host 'VAR1=value1 VAR2=value2 bash -s' < local_script.sh
Example:
ssh root@192.168.1.100 'MY_VAR="Hello World" bash -s' < my_script.sh
In the remote script, you can access MY_VAR
as a regular environment variable.
Running a local script on a remote machine is straightforward with SSH. Whether you’re on a Unix-based system or using Windows with Plink, these methods allow for seamless remote script execution without the need for manual file transfers. Remember to handle authentication securely, especially when running scripts on remote machines.
Labels: How to Run a Local Shell Script on a Remote Machine Using SSH
0 Comments:
Post a Comment
Note: only a member of this blog may post a comment.
<< Home