2

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');
 ?>
Grant
  • 1,297
  • 2
  • 16
  • 39
  • No, it is downloading the music.mp3 file from the originating server and then doing it's thing on it. – Grant Sep 23 '15 at 16:20

1 Answers1

1

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.

Niki van Stein
  • 10,564
  • 3
  • 29
  • 62
  • So that will pass the php variable just fine? And also, I don't understand what the '22' is in the $connection variable? – Grant Sep 23 '15 at 16:10
  • The 22 is the port number, 22 is the default for SSH. To pass the variable for sure you put `'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` instead – Niki van Stein Sep 23 '15 at 16:13
  • Ahh perfect I guessed it was the port number but have never connected to ssh like this before so was unsure. Excellent, will try that now, thanks very much ;) – Grant Sep 23 '15 at 16:15
  • Oh one final thing. $stream sounds like it's a two way thing going on. Is that correct? I don't want the php script on the originating server to wait for a response from the remote one after it's started the task as it has other things to do. – Grant Sep 23 '15 at 16:17
  • It is two ways if you want, the stream is a channel which you do not need to use. It does not wait for response unless you use `ssh2_fetch_stream` afterwards. So no problem for your case. – Niki van Stein Sep 23 '15 at 16:24
  • Perfect, thanks for explaining that Bas. PHP.net is a great resource but those guys talk an entirely different language ;) – Grant Sep 23 '15 at 16:28