0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Lua on RTX1210 (5) - DDNS(NoIP)更新

Posted at

検証環境

RTX1210 BootROM Ver. 1.04
RTX1210 FlashROM Table Ver. 1.00
RTX1210 Rev.14.01.38 (Fri Jul 10 09:41:02 2020)

経緯

非法人NURO回線では、YAMAHA謹製のネットボランチサービスを頼れない。
No-IP は無料かつRESTで更新できる DDNSなので使ってみる。可用性がどの程度かは知らない。

前提

update_ddns.lua

update_ddns.lua
--
-- update_ddns.lua
-- DDNS update via No-IP
--

require "strict"
local slack = require "slack"

local inet_url = os.getenv("INET_URL") or "http://inet-ip.info/ip"
local noip_url = os.getenv("NOIP_URL") or "http://dynupdate.no-ip.com/nic/update"
local noip_hostname = os.getenv("NOIP_HOSTNAME") or "example.ddns.net"
local noip_name = os.getenv("NOIP_NAME") or "example@example.com"
local noip_password = os.getenv("NOIP_PASSWORD") or "xxxxxxxx"

local function log(priority, message)
  if priority == "DEBUG" then
    rt.syslog("notice", message)
  else
    rt.syslog("info", message)
    slack.post(priority, message)
  end
end

local function get_remote_ip()
  local request_inet = {
    url = inet_url,
    method = "GET"
  }
  local response_inet = rt.httprequest(request_inet)
  if response_inet.rtn1 then
    local ip_address = response_inet.body
    log("DEBUG", "(update_ddns.lua) Global IP Address from inet-ip.info: " .. ip_address)
    return ip_address
  else
    log("ERROR", "(update_ddns.lua) Failed to fetch Global IP Address.")
  end
end

local function update_ddns(ip_address)
  local request_noip = {
    url = $[[${noip_url}?hostname=${noip_hostname}&myip={ip_address}]],
    method = "GET",
    auth_type = "basic",
    auth_name = noip_name,
    auth_pass = noip_password
  }
  local response_noip = rt.httprequest(request_noip)
  if response_noip.code == 200 then
    log("INFO", "(update_ddns.lua) Successfully updated: dynupdate.no-ip.com -> " .. response_noip.body)
  else
    log("ERROR", "(update_ddns.lua) Failed to update DDNS.")
  end
end

local current_ip = os.getenv("GLOBAL_IP")
local remote_ip = get_remote_ip()

if remote_ip then
  if current_ip then
    if current_ip ~= remote_ip then
      update_ddns(remote_ip)
    else
      log("DEBUG", "(update_ddns.lua) No need to update.")
    end
  else
    log("INFO", "(update_ddns.lua) GLOBAL_IP was not set.")
  end
  rt.command("set GLOBAL_IP=" .. remote_ip)
end

No-IPの情報を下記のようにして環境変数に設定。

set NOIP_URL="http://dynupdate.no-ip.com/nic/update"
set NOIP_HOSTNAME="example.ddns.net"
set NOIP_NAME="example@example.com"
set NOIP_PASSWORD="xxxxxxxx"

RESTでグローバルIPを取得できるサービスはいくつかあるが、inet-ip.info が一番安定しているように思う。

スケジュール

NUROは滅多にIPアドレスが変わらないので毎時で十分。

schedule at 3 */* *:00 * * lua /lua/update_ddns.lua

あとは拠点間なりリモートアクセスなり煮なり焼くなり。

所感

良い意味でLuaの言語仕様が乏しいおかげで全くの未経験でもだいたいやりたいことができた。
勘所を掴んでるうちにWiresharkやVLCへ組み込んでみよう。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?