Search code examples
phplinuxremote-accessremote-serverssh2-exec

Execute a command on a remote server with PHP - no callback / stream required


I have been looking HERE to try and figure this out but I don't fully understand all of the parameters so I'll explain what I'm trying to do :

I want to run the following command on a remote Linux (Ubuntu) server via PHP using an existing variable $myfile (which represents something like 'music.mp3') -

wget http://someaddress.com/$myfile /usr/incoming/$myfile lame --decode /usr/incoming/$myfile - | stereo_tool_cmd - - -s /usr/settings/process.sts | lame -b 128 - /usr/processed/$myfile

What this is doing is telling the remote server to download a file (http://someaddress.com/$myfile) then decode it to wav with lame, then process it using Stereo Tool, then re-encode it 128k mp3 and move it to the specified directory.

I don't need the originating server to receive any kind of feedback on the process, it just needs to start it on the remote server. How would I go about setting this up in PHP?

The following is from the PHP.net page that I linked above, and is probably what I need to be using, but I'm not sure how to set it up.

 <?php
 $connection = ssh2_connect('shell.example.com', 22);
 ssh2_auth_password($connection, 'username', 'password');

 $stream = ssh2_exec($connection, '/usr/local/bin/php -i');
 ?>

Solution

  • What about :

     <?php
     $connection = ssh2_connect('shell.example.com', 22);
     ssh2_auth_password($connection, 'username', 'password');
    
     $stream = ssh2_exec($connection,'wget http://someaddress.com/'.$myfile.
        ' /usr/incoming/'.$myfile.' lame --decode /usr/incoming/'.$myfile.
        ' - | stereo_tool_cmd - - -s /usr/settings/process.sts | lame -b 128 - /usr/processed/'.$myfile);
    ?>
    

    That should work just fine.

    edit 1 it is better to put your php variable outside the string.