Checking exec() runs successfully or not

前端 未结 4 533
小鲜肉
小鲜肉 2020-12-07 20:13

I have been trying to let know know if the exec() command in php executes successfully or not so i can echo certain messages accordingly. I tried the following

4条回答
  •  渐次进展
    2020-12-07 21:11

    A simple sample:

    $ip = "192.168.0.2";
    $exec = exec( "ping -c 3 -s 64 -t 64 ".$ip, $output, $return );
    echo $exec;
    echo "
    ----------------
    "; print_r( $output ); echo "
    ----------------
    "; print_r( $return );

    In case of not ping or ERROR. ( ONE )

    ----------------
    Array ( [0] => PING 192.168.0.2 (192.168.0.2) 64(92) bytes of data. [1] => [2] => --- 192.168.0.2 ping statistics --- [3] => 3 packets transmitted, 0 received, 100% packet loss, time 2016ms [4] => )
    ----------------
    1
    

    In case of success ( ZERO )

    rtt min/avg/max/mdev = 4.727/18.262/35.896/13.050 ms
    ----------------
    Array ( [0] => PING 192.168.0.2 (192.168.0.2) 64(92) bytes of data. [1] => 72 bytes from 192.168.0.2: icmp_req=1 ttl=63 time=14.1 ms [2] => 72 bytes from 192.168.0.2: icmp_req=2 ttl=63 time=35.8 ms [3] => 72 bytes from 192.168.0.2: icmp_req=3 ttl=63 time=4.72 ms [4] => [5] => --- 192.168.0.2 ping statistics --- [6] => 3 packets transmitted, 3 received, 0% packet loss, time 2003ms [7] => rtt min/avg/max/mdev = 4.727/18.262/35.896/13.050 ms )
    ----------------
    0
    

提交回复
热议问题