dongmisa4779 2019-03-30 12:56
浏览 66
已采纳

在线,间歇,离线php ping脚本

Fairly new to server-side scripting in php here, so please bear w/ me:

Question:

  1. How would I implement an Intermittent call?

For Question 1:

I've got things to work in terms of Online / Offline, however, I'm needing to implement an Intermittent call where if a 2nd device is down, the page will show an Intermittent status globally for the main device as well as showing that the 2nd device is offline but I'm not exactly sure how to go about doing this...

Here is the working online/offline script but has no Intermittent call below...

// CHECK CONNECTIVITY
class CheckDevice {

  // CHECK END USERS OS (Linux or Windows)
  public function myOS(){
    if (strtoupper(substr(PHP_OS, 0, 3)) === (chr(87).chr(73).chr(78)))
      return true;

      return false;
    }

    public function ping($device_ip){
      if ($this->myOS()){
        if (!exec("ping -n 2 -w 1 ".$device_ip." 2>NUL > NUL && (echo 0) || (echo 1)"))
          return true;
        } else {
          if (!exec("ping -q -c2 ".$device_ip." >/dev/null 2>&1 ; echo $?"))
            return true;
          }

        return false;
    }
}

// IP of Device
$device_ip  = 'xxx.xxx.xxx.xxx';

// DEVICE CHECK
if ((new CheckDevice())->ping($device_ip))
  echo '<div class="status_wrapper">
                        <div class="status_oo_wrapper">
                            <div class="pic_bg"></div>
                        </div>
                        <p><b>DEVICE NAME</b></p>
                        <div class="status_online">Online</div>
                        <p class="span">No issues or outages to report...</p>';
else
  echo '<div class="status_wrapper">
                        <div class="status_oo_wrapper">
                            <div class="pic_bg"></div>
                        </div>
                        <p><b>DEVICE NAME</b></p>
                        <div class="status_offline">Offline</div>
                        <div class="status_offline_extended"></div>
                        <p class="span">Please be patient...</p>';

Script w/ more than 1 device where I'd like to have an Intermittent call whereas if device 2 goes down (or is offline), then device 1 would change it's state to Intermittent:

// CHECK CONNECTIVITY
    class CheckDevice {

      // CHECK END USERS OS (Linux or Windows)
      public function myOS(){
        if (strtoupper(substr(PHP_OS, 0, 3)) === (chr(87).chr(73).chr(78)))
          return true;

          return false;
        }

        public function ping($device_ip){
          if ($this->myOS()){
            if (!exec("ping -n 2 -w 1 ".$device_ip." 2>NUL > NUL && (echo 0) || (echo 1)"))
              return true;
            } else {
              if (!exec("ping -q -c2 ".$device_ip." >/dev/null 2>&1 ; echo $?"))
                return true;
              }

            return false;
        }
    }

    // CHECK CONNECTIVITY
    class CheckDevice2 {

      // CHECK END USERS OS (Linux or Windows)
      public function myOS(){
        if (strtoupper(substr(PHP_OS, 0, 3)) === (chr(87).chr(73).chr(78)))
          return true;

          return false;
        }

        public function ping($device2_ip){
          if ($this->myOS()){
            if (!exec("ping -n 2 -w 1 ".$device2_ip." 2>NUL > NUL && (echo 0) || (echo 1)"))
              return true;
            } else {
              if (!exec("ping -q -c2 ".$device2_ip." >/dev/null 2>&1 ; echo $?"))
                return true;
              }

            return false;
        }
    }

    // IP of Device
    $device_ip  = 'xxx.xxx.xxx.xxx';
    $device2_ip = 'xxx.xxx.xxx.xxx';

    // DEVICE CHECK
    if ((new CheckDevice())->ping($device_ip))
      echo '<div class="status_wrapper">
                            <div class="status_oo_wrapper">
                                <div class="pic_bg"></div>
                            </div>
                            <p><b>DEVICE NAME</b></p>
                            <div class="status_online">Online</div>
                            <p class="span">No issues or outages to report...</p>';
    else
      echo '<div class="status_wrapper">
                            <div class="status_oo_wrapper">
                                <div class="pic_bg"></div>
                            </div>
                            <p><b>DEVICE NAME</b></p>
                            <div class="status_offline">Offline</div>
                            <div class="status_offline_extended"></div>
                            <p class="span">Please be patient...</p>';

// DEVICE2 CHECK
        if ((new CheckDevice2())->ping($device2_ip))
          echo '<div class="status_wrapper">
                                <div class="status_oo_wrapper">
                                    <div class="pic_bg"></div>
                                </div>
                                <p><b>DEVICE2 NAME</b></p>
                                <div class="status_online">Online</div>
                                <p class="span">No issues or outages to report...</p>';
        else
          echo '<div class="status_wrapper">
                                <div class="status_oo_wrapper">
                                    <div class="pic_bg"></div>
                                </div>
                                <p><b>DEVICE2 NAME</b></p>
                                <div class="status_offline">Offline</div>
                                <div class="status_offline_extended"></div>
                                <p class="span">Please be patient...</p>';

展开全部

  • 写回答

1条回答 默认 最新

  • duanlinzhen7235 2019-03-30 14:26
    关注

    I feel like this should work for you:

    
     if ((new CheckDevice2())->ping($device2_ip))
        if ((new CheckDevice())->ping($device_ip))
    
            echo '<div class="status_wrapper">
            <div class="status_oo_wrapper">
                <div class="pic_bg"></div>
            </div>
            <p><b>DEVICE NAME</b></p>
            <div class="status_online">Online</div>
            <p class="span">No issues or outages to report...</p>';
            echo '<div class="status_wrapper">
            <div class="status_oo_wrapper">
                <div class="pic_bg"></div>
            </div>
            <p><b>DEVICE2 NAME</b></p>
            <div class="status_online">Online</div>
            <p class="span">No issues or outages to report...</p>';
    
        else
    
            echo '<div class="status_wrapper">
            <div class="status_oo_wrapper">
                <div class="pic_bg"></div>
            </div>
            <p><b>DEVICE NAME</b></p>
            <div class="status_offline">Offline</div>
            <div class="status_offline_extended"></div>
            <p class="span">Please be patient...</p>';
            echo '<div class="status_wrapper">
            <div class="status_oo_wrapper">
                <div class="pic_bg"></div>
            </div>
            <p><b>DEVICE2 NAME</b></p>
            <div class="status_online">Online</div>
            <p class="span">No issues or outages to report...</p>';
    
    else
        echo "INTERMITENT";
    
        echo '<div class="status_wrapper">
        <div class="status_oo_wrapper">
            <div class="pic_bg"></div>
        </div>
        <p><b>DEVICE2 NAME</b></p>
        <div class="status_offline">Offline</div>
        <div class="status_offline_extended"></div>
        <p class="span">Please be patient...</p>';
    
    

    展开全部

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

悬赏问题

  • ¥66 android运行时native和graphics内存详细信息获取
  • ¥100 求一个c#通过CH341读取数据的Demo,能够读取指定地址值的功能
  • ¥15 rk3566 Android11 USB摄像头 微信
  • ¥15 torch框架下的强化学习DQN训练奖励值浮动过低,希望指导如何调整
  • ¥35 西门子博图v16安装密钥提示CryptAcquireContext MS_DEF_PROV Error of containger opening
  • ¥15 mes系统扫码追溯功能
  • ¥40 selenium访问信用中国
  • ¥20 在搭建fabric网络过程中遇到“无法使用新的生命周期”的报错
  • ¥15 Python中关于代码运行报错的问题
  • ¥500 python 的API,有酬谢
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部