1

我编写了一个 php 函数来执行 shell 脚本,但如果命令执行正确,我无法获得输出,函数和 shell 脚本如下:

PHP函数:

function execWithTimeout($cmd, $timeout=5) {
    //initiate retrun value
    $output = '';
    $res='';
    $cause='';

    $descriptorspec = array(
    0 => array("pipe", "r"),
    1 => array("pipe", "w"),
    2 => array("pipe", "w"));
    $pipes = array();

    $timeout += time();
    $process = proc_open($cmd, $descriptorspec, $pipes);
    if (is_resource($process)) {
        //loop check
        do {
            $timeleft = $timeout - time();
            $read = array($pipes[1]);
            stream_select($read, $write = NULL, $exeptions = NULL, $timeleft, NULL);
            if (!empty($read)) {
                $output .= fread($pipes[1], 8192);
            }
        } while (!feof($pipes[1]) && $timeleft > 0);

        if ($timeleft <= 0) {
            proc_terminate($process);
            $res=false;
            $cause='timeout';
        } else {
            $res=true;
        }
        fclose($pipes[0]);
        fclose($pipes[1]);
        fclose($pipes[2]);
        proc_close($process);
    }else{
        $res=false;
        $cause='execute command failed';
    }
    $retVal=array('result'=>$res,'output'=>$output,'cause'=>$cause);
    return $retVal;
}

该命令如下:

 $cmd='ssh root@192.168.1.100'.'"disk_sd=\`fdisk -l|grep \'Disk /dev/sd\'|awk \'{print \$2}\'|sed -n \'s/\/dev\///p\'|sed -n \'s/://p\'\`;
         disk_hd=\`fdisk -l|grep \'Disk /dev/hd\'|awk \'{print \$2}\'|sed -n \'s/\/dev\///p\'|sed -n \'s/://p\'\`;
         disk_cciss=\`fdisk -l|grep \'Disk \/dev\/cciss\'|awk \'{print \$2}\'|sed -n \'s/\/dev\///p\'|sed -n \'s/://p\'\`;
         disk_xvda=\`fdisk -l|grep \'Disk \/dev\/xvda\'|awk \'{print \$2}\'|sed -n \'s/\/dev\///p\'|sed -n \'s/://p\'\`;
         disks=\$disk_sd\" \"\$disk_hd\" \"\$disk_cciss\" \"\$disk_xvda;
         diskTotal=0;
         diskFree=0;
         diskUsed=0;         
         for d in \$disks;           
         do
           diskTotal=\`fdisk -l|grep \'Disk /dev/\'\$d\':\'|awk \'{print \$5}\'\`;
           for s in \`df -l|grep \$d|awk \'{print \$4*1024}\'\`;
              do 
              diskFree=\`echo \$diskFree \$s|awk \'{print \$1+\$2}\'\`;
           done;              
           for s in \`df -l|grep \$d|awk \'{print \$3*1024}\'\`;
              do 
              diskUsed=\`echo \$diskUsed \$s|awk \'{print \$1+\$2}\'\`;
           done;         
         done;
         diskUsage=\`echo \$diskUsed \$diskTotal|awk -F\' \' \'{print \$1/\$2*100}\'\`;         
         memTotal=\`free |awk \'/Mem:/{print \$2}\'\`;
         memUsed=\`free |awk \'/Mem:/{print \$3}\'\`;
         memFree=\`free |awk \'/Mem:/{print \$4}\'\`;
         memUsage=\`free |awk \'/Mem:/{print \$3/\$2*100}\'\`;
         cpuUsage=\`top -b -n 1|awk /^Cpu/\'{print 100-\$5}\'|cut -f1 -d %\`;
         out=\'{\';
         out=\$out\'\"diskTotal\":\'\$diskTotal;
         out=\$out\',\"diskUsed\":\'\$diskUsed;
         out=\$out\',\"diskFree\":\'\$diskFree;
         out=\$out\',\"diskUsage\":\'\$diskUsage;
         out=\$out\',\"memTotal\":\'\$memTotal;
         out=\$out\',\"memUsed\":\'\$memUsed;
         out=\$out\',\"memFree\":\'\$memFree;
         out=\$out\',\"memUsage\":\'\$memUsage;
         out=\$out\',\"cpuUsage\":\'\$cpuUsage;
         out=\$out\'}\';
         echo \$out;"';
4

0 回答 0