Reboot Wi-Fi router Buffalo AirStation WZR-HP-G301NH (JP)

Đoạn mã nguồn shellscript khởi động lại Wi-Fi router Buffalo AirStation model WZR-HP-G301NH từ máy trạm.

Reboot Wi-Fi router Buffalo AirStation WZR-HP-G301NH (JP)
Thiết bị router Wi-Fi Buffalo AirStation WZR-HP-G301NH

Tôi mua thiết bị Wi-Fi Buffalo AirStation bên Nhật từ năm 2012, đây là một thiết bị cho thị trường Nhật nên từ giao diện đến lệnh đều bằng tiếng Nhật hết.

WZR-HP-G301NH là một thiết bị tiết kiệm năng lượng, hỗ trợ nhiều tính năng security cao cấp cũng như vài dịch vụ tôi cần như NAS, BitTorrent hay Media Server. Với lưu lượng traffic lớn như vậy nên không tránh khỏi sau vài ngày Wi-Fi có xu hướng …treo.

Việc reset thiết bị này tương đối mất thời giờ vì thời gian khởi động có khi mất đến một phút chưa gồm các bước để đi đến nút khởi động lại. Vì thế tôi làm một script nhỏ bằng Bash Shell rồi cho vào cron của máy tính, tự động reset lúc 6 giờ sáng – khi tôi ngủ dậy.

#!/bin/bash

router_ip='192.168.11.1'
username='root'
password=''

export LANG=ja_JP.UTF-8

# CGI URL
cgi="http://${router_ip}/cgi-bin/cgi"
init="${cgi}?req=frm&frm=init.html"
reboot="${cgi}?req=inp&res=waiting_page.html"

for i in $(seq 5); do
    # Whether or not the router is online
    ping -q -w 1 -c 1 ${router_ip} >/dev/null || exit

    # Obtain the authentication session key
    session="$(curl --max-time 5 -u ${username}:${password} ${init} 2>/dev/null | \
            grep -m 1 sWebSession | \
            sed -e 's/^.*sWebSessionnum\ value=\([0-9\-]*\)>.*sWebSessionid\ value=\([0-9\-]*\)>.*$/\1 \2/')"
    read num id <<< "${session}"
    sleep 1

    # invalid session number -> retry
    [[ ${num} =~ ^-?[0-9]+$ ]] || continue

    # send command 再起動 (reboot)
    reboot_data="reboot=%E5%86%8D%E8%B5%B7%E5%8B%95&sWebSessionnum=${num}&sWebSessionid=${id}"
    curl -u "${username}:${password}" \
         -d "${reboot_data}" \
         -X POST "${reboot}" &> /dev/null
    sleep 6
done

Phản hồi về bài viết

Cùng thảo luận chút nhỉ!

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.