ssh - Execute a PHP script file on a remote server without copying it on the remote server

I have two (linux) servers : A and B
A PHP script is on A, I want to execute it on B but without coping it on B, this script should never exist on B (only in ram), so no "copy on B, exec from ssh command on A, erase on B".
I have ssh keys to access B from A, but not A from B. Maybe something like :
ssh root@B 'php -r ' | echo /myscript/on_serverA
or
<?php
$script=file_get_contents('executable_php_script');
system('ssh root@IP \'php -r "'.$script.'"\'');
Any ideas?
Note: it's a very specific case, so I dont want alternative or security advice.
Answer
Solution:
- You can specify the name of a command to run on the remote machine as an argument to ssh
- PHP will execute a script piped into it through STDIN
- Anything piped to
ssh
will be passed into STDIN of the program being run
Thus:
ssh example.com php < test.php
… will ssh toexample.com
, runphp
there and pipe the contents oftest.php
from the local machine into the remotephp
.
With regards to comments on the question: Note that if someone with root access onexample.com
wanted to steal the script they could replace thephp
executable with a wrapper that logs STDIN to a file before forwarding it to the realphp
. This is far from a bullet-proof security measure.
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: the metadata storage is not up to date, please run the sync-metadata-storage command to fix this issue.
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.