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

时间:2019-03-30 20:56:19

标签: javascript php html css ping

这里是php中服务器端脚本的新手,所以请耐心等待:

问题:

  1. 我将如何实施间歇呼叫?

对于问题1:

我已经按照Online / Offline的要求工作了,但是,我需要实现一个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;
    }
}

// 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>';

带有多个设备的脚本,我想进行间歇性呼叫,而如果设备2掉线(或处于脱机状态),则设备1会将其状态更改为间歇性:

// 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 个答案:

答案 0 :(得分:0)

我觉得这应该对您有用:


 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>';