How to run local (non-shell) scripts on a remote host via SSH?

If you want to push the Perl script through the SSH connection, you'll have to run the Perl interpreter on the remote end. It'll read the script from stdin:

ssh remotehost perl < somescript.pl

In the case of Perl, it should even read the command line switches (except -T) from the hashbang line of the input.

If you want to give command line arguments to the Perl interpreter, you can just add them to the command line after perl. If you want to give arguments to the script, you'll need to explicitly tell the interpreter to read the script from stdin (otherwise it will take the first argument as a file name to look for).

So, here -l goes to the interpreter, and foo and bar to the script:

echo 'print "> $_"  foreach @ARGV' | ssh remotehost perl -l - foo bar 

Note that doing just ssh somehost < script.sh counts on the remote login shell being compatible with the script. (i.e. a Bash script won't work if the remote shell happens to be something else.)


ssh someHost will execute your default shell, which in turn will execute the commands it reads from standard input. Since you are sending the content of someFile to standard input, it logically tries to executes to content of someFile.

You don't tell us what someFile is supposed to contain. Assuming its content is supposed to be fed to your perl script, what you should do is:

cat someFile | ssh someHost your_perl_script

Or better:

ssh someHost your_perl_script < someFile

Tags:

Bash

Ssh

Cat