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.

share|improve this question
up vote 10 down vote accepted

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.)

share|improve this answer
    
I've found that cat perlScript | ssh remoteHost perl works great. I'd like to pass a command line argument to perl. Do you know how I might do that? – Sol 13 hours ago
    
@Sol, edited, since there's a slight difference between switches to perl and what goes to the script's @ARGV – ilkkachu 12 hours ago
    
A friend suggested this and it works great! cat perlScript | ssh remoteHost perl /dev/stdin someArgument The /dev/stdin part is most interesting. – Sol 12 hours ago

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
share|improve this answer
    
someFile is the perl script I want to execute remotely. Sorry I wasn't clear in the problem description. – Sol 13 hours ago

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 protected] 'cat >> /usr/local/apache/htdocs/it_works.html'

Open remote file with less or cat: With cat:

ssh [email protected] "cat /usr/local/apache/htdocs/it_works.html"

With less:

ssh [email protected] "less /usr/local/apache/htdocs/it_works.html"

Grep remote files:

With pipe:

ssh [email protected] "cat /usr/local/apache/htdocs/it_works.html" |  grep "works"

Just remote grep:

ssh [email protected] "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 [email protected] "tail -f /etc/httpd/logfiles/access_log"

The source for this answer is here.

I hope it helped you.

share|improve this answer
2  
Running less on the remote like that is a bit bare, since by default SSH (without -t) doesn't allocate a terminal if a command line is given, so less only sees a socket, and most of its functionality is lost. – ilkkachu 13 hours ago
1  
Does any of this answer the question here, anyway? – ilkkachu 13 hours ago
    
I'm trying to remotely execute a perl script. I think this may not be possible without copying the perl script to the remote machine. – Sol 13 hours ago
    
check @ilkkachu answer. – kingofkech 13 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.