Substiting a line through PHP in SSH

Posted by Asad Moeen on Server Fault See other posts from Server Fault or by Asad Moeen
Published on 2012-07-09T08:26:58Z Indexed on 2012/07/09 9:17 UTC
Read the original article Hit count: 311

Filed under:
|
|
|

I've already setup SSH usage in PHP and most of the things work.

Now what I want to do is that I'm looking to edit a line in a file and replace it back. It works directly on the server but can't seem to get it working with PHP files. Here is what I'm trying.

$new_line1 = 'Line $I want to add - The $I has to go into the file as it is';
$new_line2 = 'Ending $text of the line - $text again goes into file;
$query = "Addition to line";
$exec1= 'cd /root; perl -pe "s/.*/' ;
$exec2=  '/ if $. == 37" Edit.sh > Edited.sh';
$new="$exec1$new_line1$query$new_line2$exec2";
$edit="cd /root/mp; cp Edited.sh Edit.sh";
echo $ssh->exec($new);
echo $ssh->exec($edit);

Now the thing is that running the perl command directly in SSH works without any errors but when I run this through PHP I get the error: Substitution replacement not terminated at -e line 1. I want to know why would it work this way and not that?

© Server Fault or respective owner

Related posts about unix

Related posts about file