3

我正在尝试rate -c 192.168.122.0/24在我的 Centos 计算机上运行命令,并使用命令将该命令的输出写入文本文件shell_exec('rate -c 192.168.122.0/24');还是没有运气!!

4

4 回答 4

3

如果你不需要 PHP,你可以在 shell 中运行它:

rate -c 192.168.122.0/24 > file.txt

如果您必须从 PHP 运行它:

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

">" 字符将命令的输出重定向到文件。

于 2011-04-19T11:39:05.757 回答
3

您也可以通过 PHP 获取输出,然后将其保存到文本文件中

    $output = shell_exec('rate -c 192.168.122.0/24');
    $fh = fopen('output.txt','w');
    fwrite($fh,$output);
    fclose($fh);
于 2011-04-19T11:41:09.737 回答
2

正如您忘记提及的,您的命令提供了一个非结束的输出流。要实时读取输出,您需要使用popen

来自 PHP 网站的示例:

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

您可以像读取文件一样读取进程输出。

于 2011-04-19T12:45:28.383 回答
0
$path_to_file = 'path/to/your/file';
$write_command = 'rate -c 192.168.122.0/24 >> '.$path_to_file;
shell_exec($write_command);

希望这会有所帮助。:D 这将引导你找到一个好方法。https://unix.stackexchange.com/a/127529/41966

于 2014-05-02T09:57:43.060 回答