<?php
# 取得輸入
$input file_get_contents('php://input');
$input json_decode($inputtrue);

# 檢查輸入欄位是否存在
if (!isset($input['target'])) {
    
header('Content-Type: application/json');

    echo 
json_encode([
        
'ok' => false,
        
'msg' => 'No `target` specified.',
    ], 
JSON_PRETTY_PRINT);

    exit();
}

# 排除空白輸入
if ($input['target'] == '') {
    exit(
'<No input>');
}


### 重要:組合 ping 指令 ###
$target $input['target'];
$cmd "ping -c 1 -W 1 $target 2>&1";


# 記錄執行指令
file_put_contents('php://stderr'"Command: “${cmd}”");

# 執行 ping 指令
$content shell_exec($cmd);

# 顯示回傳結果
echo $content;

# 如輸出為空白,讓使用者知道有正常執行
if (strlen($content) == 0) {
    echo 
'<No output>';
}