简体   繁体   中英

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.

  • 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 to example.com , run php there and pipe the contents of test.php from the local machine into the remote php .


With regards to comments on the question: Note that if someone with root access on example.com wanted to steal the script they could replace the php executable with a wrapper that logs STDIN to a file before forwarding it to the real php . This is far from a bullet-proof security measure.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM