Execute shell script

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
swarmspot
Forum Newbie
Posts: 4
Joined: Mon Oct 30, 2006 9:44 am

Execute shell script

Post 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]
Last edited by swarmspot on Mon Oct 30, 2006 9:57 am, edited 1 time in total.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Re: Execute shell script

Post 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");
?>
swarmspot
Forum Newbie
Posts: 4
Joined: Mon Oct 30, 2006 9:44 am

Post 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
swarmspot
Forum Newbie
Posts: 4
Joined: Mon Oct 30, 2006 9:44 am

Post 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
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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);
?>
swarmspot
Forum Newbie
Posts: 4
Joined: Mon Oct 30, 2006 9:44 am

Post 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
Post Reply