我正在尝试在我的Centos计算机上运行rate -c 192.168.122.0/24
命令,并使用shell_exec('rate -c 192.168.122.0/24')
命令将该命令的输出写入文本文件; 但是一直没有成功!
我正在尝试在我的Centos计算机上运行rate -c 192.168.122.0/24
命令,并使用shell_exec('rate -c 192.168.122.0/24')
命令将该命令的输出写入文本文件; 但是一直没有成功!
$output = shell_exec('rate -c 192.168.122.0/24');
$fh = fopen('output.txt','w');
fwrite($fh,$output);
fclose($fh);
rate -c 192.168.122.0/24 > file.txt
如果你需要从PHP运行它:
shell_exec('rate -c 192.168.122.0/24 > file.txt');
">"字符将命令的输出重定向到文件。
"2>&1
。我怀疑你是否可以从php运行任意命令。 - Salman A由于您没有提到,您的命令提供了一个无限输出流。要实时读取输出,需要使用popen。
PHP官网上的示例:
$handle = popen('/path/to/executable 2>&1', 'r');
echo "'$handle'; " . gettype($handle) . "\n";
$read = fread($handle, 2096);
echo $read;
pclose($handle);
您可以像读取文件一样读取进程输出。
$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
rate
命令存在吗?它在命令行上能用吗? - mario