顯示具有 ping 標籤的文章。 顯示所有文章
顯示具有 ping 標籤的文章。 顯示所有文章

2023年10月10日 星期二

Python 開發筆記 - Ping 與 WakeOnLan (WOL) 的應用

這假期恰逢公司機房斷電,復電後在想該怎樣把重要的機器喚起。由於 VPN 等相關機器群健康運作(如套裝 NAS 機器),因此得以遠端回去做事。

下一刻就是回顧有哪些機器要遠端啟動,這時就多虧了強者同事有良好的筆記習慣,平時有撰寫 WIKI 管控機器群(包括 MacAddress),就這樣,可在指定機器上不斷發動 WOL 來喚醒。例如有些 router 本身就提供網頁介面幫忙,可以直接靠他們。若有 Ubuntu 機器,就來個 wakeonlan 工具:

$ dpkg -l | grep wakeonlan
ii  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.json
Process: 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"
}

2016年8月19日 星期五

AWS 筆記 - 透過 VPN/IPSEC 讓 AWS VPC 與 Microsoft Azure VirtualNetwork 互通

對 Azure 來說,預設建置的 VirtualNetwork 就是 10.0.0.0 開頭的網路環境,大多可以繼續延用下去。在這延續上一篇 AWS 筆記 - 建置自己的 VPC 且透過 VPN/IPSec 讓各個 Region 的 VPC 互通 ,先簡略調整 Azure VirtualNetwork 環境,接著建置 Azure VPC 與 AWS VPC 連線機制。

假設 AWS VPC 為 10.100.0.0/16 網域,而 Azure VirtualNetwork 為 10.50.0.0/16 網域。

設置 Azure VPC:

Azure Portal -> New
-> Resource -> Create -> name=StudyVPN, Location=WestUS
-> Networking -> VirtualNetwork
-> Name=US
-> Address Space=10.50.0.0/16
-> Subnet Name=default
-> Subnet address range=10.50.0.0/24
-> Location = WestUS
-> Virtual Machines -> Ubuntu Server 14.04
-> Name=VPN
-> Location = WestUS
-> DS1_V2 Standard

請先將 AWS EC2 Security Group 跟 Azure Network security group 都設置好、開放對方 VPN IP。此例為 AWS EU-West-1 機器跟 Azure WestUS 機器,設置完防火牆後,先 ping 一下彼此吧。

在 EU-West-1 的 VPN Server 機器,其 public ip = 52.209.16.24:

$ ping 13.88.186.107

在 Azure WestUS 的 VPN Server 機器,其 public ip = 13.88.186.107:

$ ping 52.209.16.24

先設置 Azure VPN server:

$ sudo apt-get update && sudo apt-get -y upgrade && sudo apt-get -y dist-upgrade && sudo apt-get autoremove && sudo apt-get autoclean

$ sudo apt-get install openswan
$ ls /etc/ipsec*
/etc/ipsec.conf  /etc/ipsec.secrets

/etc/ipsec.d:
aacerts  cacerts  certs  crls  examples  ocspcerts  policies  private
$ sudo ipsec setup version
Linux Openswan U2.6.38/K3.19.0-65-generic (netkey)
See `ipsec --copyright' for copyright information.
$ sudo vim /etc/ipsec.d/AWS-EU-WEST-1.conf
conn Connect-to-AWS-EU-WEST-1
 type=tunnel
 authby=secret

 leftid=13.88.186.107
 left=%defaultroute
 leftsubnet=10.50.0.0/16
 leftnexthop=%defaultroute

 right=52.209.16.24
 rightsubnet=10.100.0.0/16

 pfs=yes
 auto=start
$ sudo vim /etc/ipsec.d/AWS-EU-WEST-1.secrets
13.88.186.107 52.209.16.24 : PSK "IPSEC_PASSWORD"

$ sudo vim /etc/ipsec.conf
#protostack=auto
protostack=netkey

#...
include /etc/ipsec.d/AWS-EU-WEST-1.conf

$ sudo vim /etc/ipsec.secrets
include /etc/ipsec.d/AWS-EU-WEST-1.secrets

$ sudo ipsec setup restart
ipsec_setup: Stopping Openswan IPsec...
ipsec_setup: Starting Openswan IPsec U2.6.38/K3.19.0-65-generic...


更新 AWS VPN server:

$ sudo vim /etc/ipsec.d/AZURE-US-WEST.conf
conn Connect-to-AZURE-EU-WEST
 type=tunnel
 authby=secret

 leftid=52.209.16.24

 left=%defaultroute
 leftsubnet=10.100.0.0/16
 leftnexthop=%defaultroute

 right=13.88.186.107
 rightsubnet=10.50.0.0/16

 pfs=yes
 auto=start
$ sudo vim /etc/ipsec.d/AZURE-US-WEST.secrets
$ sudo ipsec setup restart
ipsec_setup: Stopping Openswan IPsec...
ipsec_setup: Starting Openswan IPsec U2.6.37/K4.4.11-23.53.amzn1.x86_64...
ipsec_setup: /usr/libexec/ipsec/addconn Non-fips mode set in /proc/sys/crypto/fips_enabled


當兩邊的設定好後,並且用 ipsec setup restart 後,另一方才可以用指令連上對方:

Azure VPN server:

$ sudo ipsec auto --up Connect-to-AWS-EU-WEST-1
117 "Connect-to-AWS-EU-WEST-1" #4: STATE_QUICK_I1: initiate
004 "Connect-to-AWS-EU-WEST-1" #4: STATE_QUICK_I2: sent QI2, IPsec SA established tunnel mode {ESP=>0x29ba22d7 <0x68b944dd xfrm=AES_128-HMAC_SHA1 NATOA=none NATD=52.209.16.24:4500 DPD=none}


AWS VPN server:

$ sudo ipsec auto --up Connect-to-AZURE-EU-WEST
117 "Connect-to-AZURE-EU-WEST" #6: STATE_QUICK_I1: initiate
004 "Connect-to-AZURE-EU-WEST" #6: STATE_QUICK_I2: sent QI2, IPsec SA established tunnel mode {ESP=>0x74246b5f <0x7156f72b xfrm=AES_128-HMAC_SHA1 NATOA=none NATD=13.88.186.107:4500 DPD=none}


並且兩邊就可以 ping 彼此的 private ip 囉!

接著還需設定 routing table 以及對 AWS VPN Server機器打開 Change Source/Dest. Check 、對 Azure VPN Server 機器的網卡打開 IP forwarding 後,兩邊機器就可以互相 ping  到啦:

對 AWS 而言,在 VPC Dashboard -> Route 中,將相關的子網域都添加一則 10.50.0.0/16 導向至 AWS VPN Server (別忘了,若第一次設置 EC2 上的 VPN Server,還要從 EC2 Dashboard 修改該機器,取消 Change Source/Dest. Check)

對 Azure 而言,Azure portal -> New -> Networking -> Route table ,建立後,再去設定

Resource -> Route table -> Settings
-> Routes -> Add -> Route name=ToAWS, Address prefix=10.100.0.0/16, Next hop type=Virtual appliance -> Next hop address=10.50.0.4 (Azure VPN Server Private IP)
-> Subnet -> Associate subnet -> 挑選 VirtualNetwork、挑選 Subnet

最後,記得 NAT 相關要設定好,像在 Azure 上的 VPN server 是從一台乾淨的 Ubuntu 14.04 裝後,還必須設定以下:

$ echo "net.ipv4.ip_forward = 1" | sudo tee -a /etc/sysctl.conf
$ echo "net.ipv4.conf.all.accept_redirects = 0" | sudo tee -a /etc/sysctl.conf
$ echo "net.ipv4.conf.all.send_redirects = 0" | sudo tee -a /etc/sysctl.conf
$ for vpn in /proc/sys/net/ipv4/conf/*; do echo 0 | sudo tee $vpn/accept_redirects; echo 0 | sudo tee $vpn/send_redirects; done
$ sudo sysctl -p


其他備註:

$ sudo ipsec setup restart
ipsec_setup: Stopping Openswan IPsec...
ipsec_setup: Starting Openswan IPsec 2.6.38...
ipsec_setup: No KLIPS support found while requested, desperately falling back to netkey
ipsec_setup: NETKEY support found. Use protostack=netkey in /etc/ipsec.conf to avoid attempts to use KLIPS. Attempting to continue with NETKEY


解法 => protostack=netkey

$ sudo ipsec auto --up Connect-to-AWS-EU-WEST-1
022 "Connect-to-AWS-EU-WEST-1": We cannot identify ourselves with either end of this connection.


解法 => 先把一些錯誤訊息修掉,如 sudo ipsec setup restart 看到的問題,接著再看 conn 的設定,當設定錯誤也會看到這段訊息。