Laravel ping IP检查在线或离线状态 [英] Laravel ping IP to check online or offline status

查看:260
本文介绍了Laravel ping IP检查在线或离线状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Laravel v.6.11.我想ping IP:Port以检查其在线还是离线状态.为此,请使用 karlmonson/laravel-ping 软件包.每当我ping它返回错误的状态时,有时服务器就会打开,并且返回false,反之亦然.

I am using Laravel v.6.11. I want to ping an IP:Port to check status if its online or offline. For that am using karlmonson/laravel-ping package. Whenever i ping it returns wrong status sometimes server is ON and it returns false and vice versa.

我的控制器

<?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]);
        }
    }
}

现在,我希望有人帮助我摆脱这个问题.我还使用 laravelcollective/remote 来运行SSH命令.太酷了,它可以运行所有命令.任何可以帮助我解决在线或离线检查IP并返回状态代码响应的问题的人.

Now i want someone to help me to get rid of this problem. I am also using laravelcollective/remote to run SSH commands. Thats cool and it runs all commands. Anyone who can help me to solve out this problem of checking an IP if its online or offline and returns response with status code.

推荐答案

好吧,经过数天的Google搜索,我终于得到了另一种解决方案,但是由于没有人对此做出回应,所以忘记发布我的答案了.您可以遵循 curl ,因为它也是 Laravel 中内置的,如果您使用基于Ubuntu/Linux的服务器,则速度会很快.下面是放置虚拟 ip 端口

Okay finally i got another solution after days of searching over google but forgot to post my answer as no one responded to it. You can follow following curl as its also built-in in Laravel and can be fast if you using Ubuntu/Linux based server. Below is am putting dummy ip and port

    $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;
    }

这篇关于Laravel ping IP检查在线或离线状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆