這假期恰逢公司機房斷電,復電後在想該怎樣把重要的機器喚起。由於 VPN 等相關機器群健康運作(如套裝 NAS 機器),因此得以遠端回去做事。
下一刻就是回顧有哪些機器要遠端啟動,這時就多虧了強者同事有良好的筆記習慣,平時有撰寫 WIKI 管控機器群(包括 MacAddress),就這樣,可在指定機器上不斷發動 WOL 來喚醒。例如有些 router 本身就提供網頁介面幫忙,可以直接靠他們。若有 Ubuntu 機器,就來個 wakeonlan 工具:
$ dpkg -l | grep wakeonlanii wakeonlan 0.41-10 Sends 'magic packets' to wake-on-LAN enabled ethernet adapters
在這邊 ubuntu.pkgs.org/20.04/ubuntu-main-arm64/wakeonlan_0.41-12_all.deb.html 也能查看到,甚至瀏覽一下工具原始碼 github.com/jpoliv/wakeonlan ,而 WOL 的原理可以查看 WIKI 就習得實作技能 en.wikipedia.org/wiki/Wake-on-LAN 或詢問 chatGPT 也會很佛心。
假期期間,就弄個 python 工具 ping-before-wakeonlan ,目的是為了記錄此次喚起了幾台機器,可以限縮每次喚起的機器數量,避免一堆機器一起起來而造成電力需求過大而跳電,另外再加入把清單亂數排一下,避免前面清單的機器都無法喚起,又讓清單後面的機器沒機會被叫起來:
使用方式,主要為 `pip install ping-before-wakeonlan` 即可,以下是 venv 用法:
% python3 -m venv venv% source venv/bin/activate% pip install ping-before-wakeonlan% ./venv/bin/ping-before-wakeonlan% cat /tmp/device.json[{"ip": "192.168.1.1","mac_address": "00:00:00:00:00:01"},{"ip": "192.168.1.2","mac_address": "00:00:00:00:00:02"},{"ip": "192.168.1.3","mac_address": "00:00:00:00:00:03"}, ...]% ping-before-wakeonlan --device-info /tmp/device.jsonProcess: 1 / 7: Device: {'ip': '192.168.1.3', 'mac_address': '00:00:00:00:00:03'}Process: 2 / 7: Device: {'ip': '192.168.1.2', 'mac_address': '00:00:00:00:00:02'}Process: 3 / 7: Device: {'ip': '192.168.1.1', 'mac_address': '00:00:00:00:00:01'}Process: 4 / 7: Device: {'ip': '192.168.1.4', 'mac_address': '00:00:00:00:00:04'}Process: 5 / 7: Device: {'ip': '192.168.1.5', 'mac_address': '00:00:00:00:00:05'}{"count": 5,"device": {"failed": [],"handled": [{"ip": "192.168.1.3","mac_address": "00:00:00:00:00:03"},{"ip": "192.168.1.2","mac_address": "00:00:00:00:00:02"},...],"input": [{"ip": "192.168.1.3","mac_address": "00:00:00:00:00:03"}, ...],"online": [],"skip": [{"ip": "192.168.1.6","mac_address": "00:00:00:00:00:06"}, ...]},"info": ["..."],"maxCount": 5,"ping": "ping -c 1 -W 3","status": true,"version": "1.0.0"}
沒有留言:
張貼留言