3

I'm trying to run rate -c 192.168.122.0/24 command on my Centos computer and write down the output of that command to the text file using shell_exec('rate -c 192.168.122.0/24') command; still no luck!!

tshepang
  • 12,111
  • 21
  • 91
  • 136
Roshan Wijesena
  • 3,106
  • 8
  • 38
  • 57

4 Answers4

3

If you don't need PHP, you can just run that in the shell :

rate -c 192.168.122.0/24 > file.txt

If you have to run it from PHP :

shell_exec('rate -c 192.168.122.0/24 > file.txt');

The ">" character redirect the output of the command to a file.

Matthieu Napoli
  • 48,448
  • 45
  • 173
  • 261
  • Well if you run the command yourself do you get an output ? With and without the "> file.txt". This is weird if the text file is empty, that means that the command doesn't print any result. – Matthieu Napoli Apr 19 '11 at 11:45
  • thank you for your reply .yes i can get the out put when run the command in terminal its non ending output stream – Roshan Wijesena Apr 19 '11 at 11:49
  • @Roshan: Did you run into a "permission problem"? Try adding `2>&1` to the above mentioned command line. I doubt if you can run arbitrary commands from php. – Salman A Apr 19 '11 at 11:53
  • @Salman There is no problem in file permission i can get the out out of ls -l to the text file easily.. – Roshan Wijesena Apr 19 '11 at 12:17
  • @Roshan: it could still be a permission problem, ls command could have different permissions compared to rate command. Did you try redirecting standard error to a text file? – Salman A Apr 19 '11 at 12:27
  • Ok if this is a non ending output that is normal. I'll try another answer. – Matthieu Napoli Apr 19 '11 at 12:43
3

You can also get the output via PHP, and then save it to a text file

    $output = shell_exec('rate -c 192.168.122.0/24');
    $fh = fopen('output.txt','w');
    fwrite($fh,$output);
    fclose($fh);
fin1te
  • 4,289
  • 1
  • 19
  • 16
2

As you forgot to mention, your command provides a non ending output stream. To read the output in real time, you need to use popen.

Example from PHP's website :

$handle = popen('/path/to/executable 2>&1', 'r');
echo "'$handle'; " . gettype($handle) . "\n";
$read = fread($handle, 2096);
echo $read;
pclose($handle);

You can read the process output just like a file.

Matthieu Napoli
  • 48,448
  • 45
  • 173
  • 261
0
$path_to_file = 'path/to/your/file';
$write_command = 'rate -c 192.168.122.0/24 >> '.$path_to_file;
shell_exec($write_command);

hopes this helps. :D And this will direct you to a good way. https://unix.stackexchange.com/a/127529/41966

Community
  • 1
  • 1
Thusitha Sumanadasa
  • 1,669
  • 2
  • 22
  • 30