Skip to content

Instantly share code, notes, and snippets.

@highgain86j
Forked from shiwork/mydns.lua
Created January 28, 2017 05:31
Show Gist options
  • Save highgain86j/5a5e50a5dd94de9c76dc7bc4a15e84a9 to your computer and use it in GitHub Desktop.
Save highgain86j/5a5e50a5dd94de9c76dc7bc4a15e84a9 to your computer and use it in GitHub Desktop.
YAMAHA RTXシリーズでDDNSをLuaで更新通知するスクリプト
------------------------------------
-- Config
------------------------------------
-- MyDNS ID
mid="MyDNSID"
-- MyDNS Password
pwd="MyDNSPASSWD"
-- IPv6 GIP Interface
-- IPv6アドレスが設定されているInterfaceを指定する('lan1')
-- RTXにて`show ipv6 address lan1`の用にGIPが取れるInterfaceを指定する
-- 通知する必要がなければnil
ipv6_conf = "lan1"
-- IPv4 GIP Interface
-- IPv4のGIPが設定されているpp(Point to Point Protocol)値を設定する
-- `show status pp 01`の様にIPv4のGIPが取得できるppを指定する
ipv4_conf = "01"
------------------------------------
-- Get IPv6 Address
------------------------------------
function get_ipv6_address(lan)
local rtn, str, s1, s2, e, adr, time, i, a, max, max_n
local p_time, v
local ptn_g = "global%s+"
local t = {}
local ret
local err
rtn, str = rt.command("show ipv6 address " .. lan)
i = 1
if rtn and str then
s1, s2 = str:find(ptn_g, 0)
if s1 then
e = str:find("%/", s2)
ret = str:sub(s2+1, e-1)
else
err = "IPv6 Global IP Not Found"
end
end
return ret, err
end
----------------------------------------------------------
-- Get IPv4 address
----------------------------------------------------------
function get_ipv4_address(pp)
local rtn, str, s1, s2, e, addr
local ptn_g = "PP IP Address Local:%s+"
-- execute command --
rtn, str = rt.command("show status pp " .. pp)
if rtn and str then
s1, s2 = str:find(ptn_g, 0)
if s1 then
e = str:find(",", s2)
addr = str:sub(s2+1, e-1)
else
err = "IPv4 Global IP Not Found"
end
end
return addr, err
end
----------------------------------------------------------
-- MyDNS request table
----------------------------------------------------------
function create_reqest_table(ipv4, ipv6, mid, pwd)
local url
url = "http://www.mydns.jp/directip.html?MID="..mid.."&PWD="..pwd
if ipv4 then
url = url .. "&IPV4ADDR=" .. ipv4
end
if ipv6 then
url = url .. "&IPV6ADDR=" .. ipv6
end
req_table = {
url = url,
method = "GET"
}
return req_table
end
----------------------------------------------------------
-- main routine
----------------------------------------------------------
if ipv6_conf then
ipv6_addr, err = get_ipv6_address(ipv6_conf)
end
if ipv4_conf then
ipv4_addr, err = get_ipv4_address(ipv4_conf)
end
-- rt.syslog("info", ipv6_addr)
-- rt.syslog("info", ipv4_addr)
req_table = create_reqest_table(ipv4_addr, ipv6_addr, mid, pwd)
-- rt.syslog("info", req_table.url)
rsp_t = rt.httprequest(req_table)
-- rt.syslog("info", tostring(rsp_t.code))
-- rt.syslog("info", rsp_t.header)
-- rt.syslog("info", rsp_t.body)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment