我使用的是Laravel v.6.11。我想要平一个IP:端口,以检查其状态是在线还是离线。为此,我正在使用卡蒙森/拉拉-平包。每当我ping时,它返回错误的状态,有时服务器是打开的,它返回false,反之亦然。
我的控制器
<?php
namespace App\Http\Controllers\User;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Ping;
class PlexServerController extends Controller
{
public function serverStatus(Request $request)
{
$ip = $request->ip;
$port = $request->port;
$health = Ping::check($ip.':'.$port);
if($health == 200){
return $json = json_encode(['status' => '1','health' => $health]);
} else {
return $json = json_encode(['status' => '2','health' => $health]);
}
}
}现在我想找人帮我解决这个问题。我还使用集体/偏远运行SSH命令。这很酷,它运行所有的命令。任何人谁能帮助我解决这个问题,检查一个IP是否在线或离线,并返回响应的状态代码。
发布于 2020-06-09 16:08:53
好的,在谷歌搜索了几天之后,我终于找到了另一个解决方案,但忘了发布我的答案,因为没有人对此做出回应。您可以遵循curl作为它在Laravel中的内置功能,如果您使用基于Ubuntu/Linux的服务器,则可以运行得很快。下面是虚拟的ip和端口
$ip = '127.0.0.1';
$port = '22';
$url = $ip . ':' . $port;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
$health = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($health) {
$json = json_encode(['health' => $health, 'status' => '1']);
return $json;
} else {
$json = json_encode(['health' => $health, 'status' => '0']);
return $json;
}https://stackoverflow.com/questions/60871657
复制相似问题