Execute a Remote Script on a Remote Computer via Ssh

I have a bash script on Machine B which I want to run on Machine B. I am using Machine A right now. Is this possible?

So far I've only managed to do it if the script lives on Machine A using the following command:

ssh user@machineb 'bash -s' < /path/machinea/script.sh

I don't want to have to copy this remote script locally. Is there a way to run this remote script on a remote machine through my local machine via SSH?

1

1 Answer

From man ssh:

If command is specified, it is executed on the remote host instead of a login shell.

where command is the last argument of ssh

Therefore, the only thing you have to do is move the single quote:

ssh user@machineb 'bash -s < /path/machinea/script.sh'

Basically the command you provided above redirects the machine A script to ssh, which transmits the script through the network and runs it in machine B. Even less hassle to run scripts/commands locally on machine B.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Sophia Al-Mansoor

Sophia Al-Mansoor

Global Business & E-Commerce Reporter

Sophia analyzes international trade, startup ecosystems, retail transformation, and supply chain logistics for modern digital publications.

Share this article
Twitter Facebook Pinterest