Skip to content

Instantly share code, notes, and snippets.

@Milly
Created May 13, 2014 12:01
Show Gist options
  • Save Milly/6d938b70f9ccba03bcdd to your computer and use it in GitHub Desktop.
Save Milly/6d938b70f9ccba03bcdd to your computer and use it in GitHub Desktop.
Netvolante DNS サービスを多段ルータの内側で利用する Lua スクリプト
-- netvolante-update.lua
-- Last Modified: 13 May 2014.
--[[
# brief
Netvolante DNS サービスを多段ルータの内側で利用する Lua スクリプトです。
VLAN ポートに一時的にグローバルIPを付与して Netvolante サービスに登録を行います。
## RT setting example
# VLAN の設定を任意に行う(例では実質 VLAN 未使用と同じ)
vlan port mapping lan1.1 vlan1
vlan port mapping lan1.2 vlan1
vlan port mapping lan1.3 vlan1
vlan port mapping lan1.4 vlan1
lan type lan1 port-based-option=divide-network
# 一時的に使用するポート (lan1.4) と VLAN (vlan4) にグローバルIPを付与
vlan port mapping lan1.4 vlan4
ip vlan4 address AAA.BBB.CCC.DDD/EE
# 自動更新をOFF
netvolante-dns use vlan4 server=1 off
# Netvolante サービスに登録
netvolante-dns hostname host vlan4 server=1 HOSTNAME
netvolante-dns go vlan4
# 一時使用ポートと VLAN 情報を元に戻す
no ip vlan4 address
vlan port mapping lan1.4 vlan1
# 自動起動を設定
schedule at 1 startup * lua /lua/netvolante-update.lua
--]]
--########## configuration ##########
DEBUG = false
--# Aterm BL900HW web interface
ROUTER_INFO_URL = "http://192.168.0.1/index.cgi/info_main";
ROUTER_USERNAME = "adm"
ROUTER_PASSWORD = "password"
--# temporary port and vlan
TMP_PORT = "lan1.4"
TMP_VLAN = "vlan4"
--# restore vlan
RESTORE_VLAN = "vlan1"
--# restore vlan ip (empty -> delete ip)
RESTORE_VLAN_IP = ""
-- RESTORE_VLAN_IP = "192.168.100.1/24"
--# global ip watch interval (seconds)
WATCH_INTERVAL = 3600
--########## end configuration ##########
--[[ test functions
rt = {}
function rt.httprequest(...)
local body = {}
for line in io.lines("info.html") do body[#body + 1] = line end
return { rtn1 = true, code = 200, body = table.concat(body, "\n") }
end
function rt.syslog( t, msg )
print( "["..t.."]: "..msg )
end
function rt.command( cmd )
return true, ""
end
function rt.sleep( sec )
print( "rt.sleep("..sec..")" )
os.exit()
end
--]]
function log_format( t, msg, ... )
if ... then
msg = string.format( msg, ... )
end
if DEBUG then
print( "[" .. t .. "] " .. msg )
else
rt.syslog( t, "[LUA] netvolante-update.lua: " .. msg )
end
end
function log_debug( msg, ... )
log_format( "debug", msg, ... )
end
function log_info( msg, ... )
log_format( "info", msg, ... )
end
function log_error( msg, ... )
log_format( "info", "[ERR] " .. msg, ... )
end
function run_command( cmd, ... )
if ... then
cmd = string.format( cmd, ... )
end
log_debug( "[rt.command] %s", cmd )
return cmd, rt.command( cmd )
end
function get_router_ip()
log_debug( "GET %s", ROUTER_INFO_URL )
local res = rt.httprequest({
url = ROUTER_INFO_URL,
method = "GET",
auth_type = "basic",
auth_name = ROUTER_USERNAME,
auth_pass = ROUTER_PASSWORD,
})
if not ( res.rtn1 and res.code == 200 ) then
log_error( "router cannot access (%s)", res.err )
os.exit(1)
end
local s = { string.find( res.body, "<h2>WAN.-'WAN.-IP.->([0-9./]+)<" ) }
log_debug( "router ip = %s", s[3] )
return s[3]
end
function is_ip_changed( new_ip )
local old_ip = os.getenv("GLOBALIP")
if old_ip == new_ip then
return false
end
return true
end
function update_netvolante( new_ip )
local cmd, ret, msg
cmd, ret, msg = run_command( "vlan port mapping %s %s", TMP_PORT, TMP_VLAN )
if not ret then
log_error( "\"%s\" -> \"%s\"", cmd, msg )
return false
end
cmd, ret, msg = run_command( "ip %s address %s", TMP_VLAN, new_ip )
if not ret then
log_error( "\"%s\" -> \"%s\"", cmd, msg )
return false
end
cmd, ret, msg = run_command( "netvolante-dns go %s", TMP_VLAN )
if not ret then
log_error( "\"%s\" -> \"%s\"", cmd, msg )
return false
end
log_info( "netvolante-dns updated to %s", new_ip )
run_command( "set GLOBALIP=%s", new_ip )
if RESTORE_VLAN_IP and 0 < #RESTORE_VLAN_IP then
run_command( "ip %s address %s", TMP_VLAN, RESTORE_VLAN_IP )
else
run_command( "no ip %s address", TMP_VLAN )
end
run_command( "vlan port mapping %s %s", TMP_PORT, RESTORE_VLAN )
return true
end
function main()
log_info( "starting" )
while true do
local new_ip = get_router_ip()
if is_ip_changed(new_ip) then
local ret = update_netvolante( new_ip )
if not ret then os.exit(1) end
end
log_debug( "sleep %d sec...", WATCH_INTERVAL )
rt.sleep( WATCH_INTERVAL )
end
end
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment