Page 1 of 1

Execute shell script

Posted: Mon Oct 30, 2006 9:54 am
by swarmspot
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hi,

I'm trying to execute a shell script from PHP. 

The script has read/write and run properties granted to anyone. It works when run from the shell, but I can't seem to get PHP to run it. 
The first part of the program creates the script and it all works fine. It's the last line that won't work

Can anybody spot where I'm going wrong?

Code: Select all

<?php
$nas=$_POST['nas'];
$ssid=$_POST['ssid'];
$url=$_POST['url'];
$login=$_POST['login'];
$nvram ="write.sh";
$fp =fopen($nvram, "w");

$script="
cp nvrambak.bin swarmnvram.bin
rpl horn1 $nas swarmnvram.bin
rpl topup.ie topup.ie,$login swarmnvram.bin
rpl swarmspot $ssid swarmnvram.bin
rpl jolly.php $url swarmnvram.bin";


fputs($fp, $script);


system("./sh write.sh");

?>
Any help would be much appreciated.

Regards,

Seab Bracken


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Re: Execute shell script

Posted: Mon Oct 30, 2006 9:56 am
by volka
try

Code: Select all

<?php
$nas=$_POST['nas'];
$ssid=$_POST['ssid'];
$url=$_POST['url'];
$login=$_POST['login'];
$nvram ="./write.sh";
$fp =fopen($nvram, "w");

$script="cp nvrambak.bin swarmnvram.bin
rpl horn1 $nas swarmnvram.bin
rpl topup.ie topup.ie,$login swarmnvram.bin
rpl swarmspot $ssid swarmnvram.bin
rpl jolly.php $url swarmnvram.bin";


fputs($fp, $script);
fclose($fp);

system("sh $nvram");
?>

Posted: Mon Oct 30, 2006 10:09 am
by swarmspot
My God! That was a quick response. It worked a treat. Thanks a million for your help. I've been trying to get it to work for two days.
The script copies a binary file and then updates the copy with some new values. The file is a backup file for DD-WRT that configures a Buffalo or Linksys AP for access to a Radius server via Chillispot.

Once again thanks for your help.

Sean Bracken

Posted: Mon Oct 30, 2006 10:16 am
by swarmspot
I still have a problem. The copy part of the script is working, but the find and replace isn't. Any ideas? rpl is a utility that works fine from the shell.

Regards,

Sean

Posted: Mon Oct 30, 2006 10:27 am
by volka
You could do the whole thing in php

Code: Select all

<?php
$replacements = array(
	'horn' => $_POST['nas'],
	'topup.ie' => "topup.ie,$_POST[login]",
	'swarmspot' => $_POST['ssid'],
	'jolly.php' => $_POST['url']
);

$contents = file_get_contents('nvrambak.bin');
$new_contents = str_replace(array_keys($replacements), array_values($replacements), $contents);

echo "<pre>\n"; print_r($replacements); echo "\n</pre>\n";
echo "<pre>\n", $new_contents, "</pre>\n";

file_put_contents('swarmnvram.bin', $new_contents);
?>

Posted: Mon Oct 30, 2006 10:56 am
by swarmspot
Fantastic!

Eric, if you were a woman I'd kiss you.

I had no idea that I could write to a binary file from PHP.

Thanks a million for your help.

Sean