監控網路連線狀態

同事有監控網路連線狀態並開關軟體的需求,請我做個小工具。

運作方式:每五秒會 ping 三個 DNS,三個 DNS 都沒回應就判斷網路有問題,會把斷線時間寫入檔案,並關掉指定軟體。三個 DNS 都有回應代表正常。網路有問題又回復連線的話,會等待三十秒再把指定軟體打開。

下面是 Node.js 的 code,有用到 ping 這個 package。

const { execSync, execFile } = require('child_process');
const fs = require('fs');
const ping = require('ping');

const hosts = ['168.95.1.1', '8.8.8.8', '1.1.1.1'];
const appName = 'app.exe';
const appPath = `C:\\Program Files (x86)\\App\\${appName}`;

let status;
let isClosed = false;
let intervalId;
let countdown = 30;

setInterval(() => {
  let onCount = 0;
  let offCount = 0;

  hosts.forEach((host) => {
    ping.sys.probe(host, (isAlive) => {
      if (isAlive) {
        onCount++;
        console.log(`${host} (on)`);

        // 連線正常
        if (onCount >= hosts.length) {
          console.log('--- Status: Online ---');

          // 連線回復
          if (status === 'OFFLINE') {
            status = '';
            console.log('Status: Network Back....');

            // 啟動 app
            launchApp();
          }
        }
      } else {
        offCount++;
        console.log(`${host} (off)`);

        // 連線有問題
        if (offCount >= hosts.length) {
          status = 'OFFLINE';
          console.log(`==== Status: ${status} [${getDateTime()}] ====`);

          // 寫入斷線時間
          logOffline();

          // 關閉 app
          if (!isClosed) {
            closeApp();
          }
        }
      }
    });
  });
}, 5000);

function closeApp() {
  isClosed = true;
  // console.log('closeApp()-isClosed:', isClosed);

  setTimeout(() => {
    console.log('Close App');
    execSync(`taskkill /f /im ${appName} /t`);
  }, 2000);
}

function launchApp() {
  isClosed = false;
  // console.log('launchApp()-isClosed:', isClosed);

  intervalId = setInterval(() => {
    console.log(`Countdown: ${countdown--}`);

    if (countdown === 0) {
      // console.log('clear-intervalId:', intervalId);
      clearInterval(intervalId);

      countdown = 30;

      console.log('Launch App');
      execFile(appPath);
    }
  }, 1000);

  // console.log('set-intervalId:', intervalId);
}

function getDateTime() {
  let today = new Date();

  let year = today.getFullYear();
  let month = ('0' + (today.getMonth() + 1)).slice(-2);
  let date = ('0' + today.getDate()).slice(-2);

  let hours = ('0' + today.getHours()).slice(-2);
  let minutes = ('0' + today.getMinutes()).slice(-2);
  let seconds = ('0' + today.getSeconds()).slice(-2);

  return `${year}-${month}-${date} ${hours}:${minutes}:${seconds}`;
}

function logOffline() {
  const data = `OFFLINE ${getDateTime()}\n`;

  fs.appendFile('offline_log.txt', data, 'utf8', (err) => {
    if (err) {
      console.log('Err:', err);
    }
  });
}


參考連結:

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

返回頂端