How can I make
cat someFile | ssh someHost work when someFile is not a bash script? I want to remotely execute a perl script but I get a bunch of syntax errors from bash when I try the cat | ssh command.
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).
-l goes to the interpreter, 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
ssh someHost your_perl_script < someFile
in ordred to =>
Write local output into remote file: You just installed the apache on your $myhost. Now you want to test it.
You can echo the test string into file, only this time the file is in remote htdocs root:
echo "It works" | ssh email@example.com 'cat >> /usr/local/apache/htdocs/it_works.html'
Open remote file with less or cat: With cat:
ssh firstname.lastname@example.org "cat /usr/local/apache/htdocs/it_works.html"
ssh email@example.com "less /usr/local/apache/htdocs/it_works.html"
Grep remote files:
ssh firstname.lastname@example.org "cat /usr/local/apache/htdocs/it_works.html" | grep "works"
Just remote grep:
ssh email@example.com "grep works /usr/local/apache/htdocs/it_works.html"
Watching remote logs with tail:
This way you can watch the access to apache server in real-time:
ssh firstname.lastname@example.org "tail -f /etc/httpd/logfiles/access_log"
The source for this answer is here.