FreeBSD 執行系統命令
FreeBSD 執行系統命令
function do_command($commandName, $args)
{
$buffer = "";
if (false === ($command = find_command($commandName))) return false;
if ($fp = @popen("$command $args", 'r'))
{
while (!@feof($fp))
{
$buffer .= @fgets($fp, 4096);
}
return trim($buffer);
}
return false;
}
//確定執行文件位置 FreeBSD
function find_command($commandName)
{
$path = array('/bin', '/sbin', '/usr/bin', '/usr/sbin', '/usr/local/bin', '/usr/local/sbin');
foreach($path as $p)
{
if (@is_executable("$p/$commandName")) return "$p/$commandName";
}
return false;
}
//調用方法
echo do_command('sysctl', "-n hw.model");
?>