Reputation: 3136
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!!
Upvotes: 4
Views: 15055
Reputation: 1761
$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
Upvotes: 0
Reputation: 49553
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.
Upvotes: 2
Reputation: 4351
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);
Upvotes: 3
Reputation: 49553
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.
Upvotes: 4