Sunday, 15 September 2024

How to Run a Local Shell Script on a Remote Machine via SSH

If you want to run a local shell script on a remote machine using SSH, there are several ways to achieve this depending on whether you’re using a Windows or Unix-based system. Here’s a guide for both environments:

For Unix-Based Systems (Linux/macOS)

You can use ssh to execute a local shell script on a remote machine without needing to copy the script over. The command works by sending the script via standard input to be executed on the remote server:

ssh user@MachineB 'bash -s' < local_script.sh

Explanation:

  • ssh: Establishes an SSH connection to the remote machine.
  • 'bash -s': This tells the remote machine to execute commands from the standard input, which is provided by redirecting the local script.
  • < local_script.sh: Sends the content of local_script.sh to the remote machine as the input for bash -s.

For Windows Systems Using Plink (Part of PuTTY)

On a Windows machine, you can use Plink, a command-line utility that comes with PuTTY. It allows you to run commands on a remote machine via SSH.

plink user@MachineB -m local_script.sh

Explanation:

  • plink: The SSH client used in Windows.
  • -m local_script.sh: Executes the local script (local_script.sh) on the remote machine.

Passing Arguments to the Script

If you need to pass arguments to the script, you can modify the SSH command like this:

ssh user@MachineB 'bash -s' arg1 arg2 < local_script.sh

Here, arg1 and arg2 will be passed to the script when it runs on the remote machine.

For Scripts that Require sudo

If your script requires sudo privileges on the remote machine, you can modify the command to include sudo:

ssh user@MachineB 'echo "password" | sudo -Sv && bash -s' < local_script.sh

This example uses sudo with a password prompt, ensuring that the script can run with elevated privileges on the remote machine.

Using Heredoc for Inline Commands

Another powerful approach is using a heredoc to directly run a series of commands on a remote machine without needing a separate script. This method can also handle nested SSH sessions:

ssh user@MachineB <<'ENDSSH'
# Commands to run on remote machine B
ssh user@MachineC <<'ENDSSH2'
    # Commands to run on Machine C
    echo "Nested SSH session"
ENDSSH2
ENDSSH

Passing Environment Variables

You can also pass environment variables from your local machine to the remote one:

ssh user@MachineB VAR1=value1 VAR2=value2 'bash -s' <<'ENDSSH'
  # Commands to run using VAR1 and VAR2
  echo "VAR1 is $VAR1, VAR2 is $VAR2"
ENDSSH

Additional Tips

  • Escaping Variables: If you want to ensure that variables like $HOME are evaluated on the remote machine, escape them in the command:

    ssh user@MachineB "echo \$HOME"
    
  • Handling Interactive Commands: Tools like Expect can be used to automate interactive commands (e.g., FTP or Telnet) when needed.

By using these methods, you can easily execute local scripts on remote machines via SSH without needing to transfer files manually.

Labels:

0 Comments:

Post a Comment

Note: only a member of this blog may post a comment.

<< Home