Jump to content

[SOLVED] Save what my script echos to a file.


lynxus

Recommended Posts

Save what my script echos to a file.

 

Hi guys,

I have a load of PHP scripts that get some basic data from a form and then echo shed loads of config for a cisco device.. Lets say something liek below.. ( This is only a basic example )

 


$ip = $_POST['ip'];
$subnet = $_POST['subnet'];
$description = $_POST['description'];

echo '
conf t <br>
interface x/x <br>
ip address '.$ip.' '.$subnet.' <br>
description '.$description.' <br>
no shutdown <br>
';

 

This is a basic example that would output the config to paste onto a device.

 

My question is HOW can i get this output to save to a file when the script runs?

I need something simple to implement as i have a lot of these and some are MASSIVE.

 

So a easy way to catch the script output that gets sent to teh browser each time it gets run from a post would be GrEAT..

 

ie:

ScriptOutput > file $description_$date.cfg

 

Any ideas?

 

Thanks

L

 

 

 

Link to comment
Share on other sites

ob_start();          // Enable output buffer
echo 'Hello World';
echo '<br /> more stuff here';
echo '<br />farewell cruel world';
$output = ob_get_contents();      //  Get all the output that we've just buffered into a single variable
ob_end_flush();                   //  Send the output buffer to the browser
file_put_contents('outFile.txt',$output);    //  Write the output that we put in $output to a file

Link to comment
Share on other sites

Humm , ive tried:

 


ob_start();

$ip = $_POST['ip'];
$subnet = $_POST['subnet'];
$description = $_POST['description'];

echo '
conf t <br>
interface x/x <br>
ip address '.$ip.' '.$subnet.' <br>
description '.$description.' <br>
no shutdown <br>
';

$LogToFile = ob_get_contents();

$myFile = "test.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = "$LogToFile";
fwrite($fh, $stringData);
fclose($fh);

ob_end_clean();

 

But that doesnt seem to work, ( No errors mind )

 

Maybe im just being stupid. Sorry im not that great with php :(

Link to comment
Share on other sites

You did not use "flush" you used "clean" instead.

ob_start();

$ip = $_POST['ip'];
$subnet = $_POST['subnet'];
$description = $_POST['description'];

echo '
conf t <br>
interface x/x <br>
ip address '.$ip.' '.$subnet.' <br>
description '.$description.' <br>
no shutdown <br>
';

$LogToFile = ob_get_contents();

$myFile = "test.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = "$LogToFile";
fwrite($fh, $stringData);
fclose($fh);

ob_end_flush(); // note the flush not clean.

Link to comment
Share on other sites

Ah sweet, Looks like it gets the data :) YAY

 

However im having permission errors with apache.

On the off chance, I dont suppose anyone knows how to resolve it ?

 

[Tue Mar 03 17:55:29 2009] [error] [client 195.216.xxx.xxx] PHP Warning:  file_put_contents(outFile.txt) [<a href='function.file-put-contents'>function.file-put-contents</a>]: failed to open stream: Permission denied in /var/www/html/101.php on line 318, referer: http://omitted.com/101.html

 

( Its a linux server )

 

If no one knows how to fix then thanks very much for you help on getting the output into a string for me :)

Its been a pest for a while and now im closer than ever to get it working :) yay

Link to comment
Share on other sites

It will be a dynamiccly created file ( with a date and time in it when ive finished . )

 

How do i allow the script to create files in the dir Logs .

 

Thsi is what i have for the Dir at the mo.

 

drw-rw-rw- 2 apache apache    4096 Mar  3 18:00 Logs

 

Ive tried root root , But still nada.

 

Thanks

L

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.