Bundle a lua script for generating a bat-hosts
This commit is contained in:
parent
0a66a7a0c2
commit
393bc88a13
2 changed files with 83 additions and 1 deletions
|
@ -12,7 +12,7 @@ include $(TOPDIR)/rules.mk
|
||||||
#
|
#
|
||||||
PKG_NAME:=alfred
|
PKG_NAME:=alfred
|
||||||
PKG_VERSION:=2013.3.0
|
PKG_VERSION:=2013.3.0
|
||||||
PKG_RELEASE:=1
|
PKG_RELEASE:=2
|
||||||
PKG_MD5SUM:=018ef6262cdd11e900af31d71a864b13
|
PKG_MD5SUM:=018ef6262cdd11e900af31d71a864b13
|
||||||
|
|
||||||
PKG_SOURCE:=$(PKG_NAME)-$(PKG_VERSION).tar.gz
|
PKG_SOURCE:=$(PKG_NAME)-$(PKG_VERSION).tar.gz
|
||||||
|
@ -74,6 +74,8 @@ define Package/alfred/install
|
||||||
$(INSTALL_BIN) ./files/alfred.init $(1)/etc/init.d/alfred
|
$(INSTALL_BIN) ./files/alfred.init $(1)/etc/init.d/alfred
|
||||||
$(INSTALL_DIR) $(1)/etc/config
|
$(INSTALL_DIR) $(1)/etc/config
|
||||||
$(INSTALL_DATA) ./files/alfred.config $(1)/etc/config/alfred
|
$(INSTALL_DATA) ./files/alfred.config $(1)/etc/config/alfred
|
||||||
|
$(INSTALL_DIR) $(1)/etc/alfred
|
||||||
|
$(INSTALL_BIN) ./files/bat-hosts.lua $(1)/etc/alfred/bat-hosts.lua
|
||||||
endef
|
endef
|
||||||
|
|
||||||
$(eval $(call BuildPackage,alfred))
|
$(eval $(call BuildPackage,alfred))
|
||||||
|
|
80
alfred/files/bat-hosts.lua
Normal file
80
alfred/files/bat-hosts.lua
Normal file
|
@ -0,0 +1,80 @@
|
||||||
|
#!/usr/bin/lua
|
||||||
|
|
||||||
|
local nxo = require "nixio"
|
||||||
|
|
||||||
|
local type_id = 64 -- bat-hosts
|
||||||
|
|
||||||
|
local function generate_bat_hosts()
|
||||||
|
-- get hostname and interface macs/names
|
||||||
|
-- then return a table containing valid bat-hosts lines
|
||||||
|
local n, i
|
||||||
|
local ifaces, ret = {}, {}
|
||||||
|
|
||||||
|
local hostname = nxo.uname().nodename
|
||||||
|
|
||||||
|
-- skip loopback ("lo") mac (00:00:00:00:00:00)
|
||||||
|
for n, i in ipairs(nxo.getifaddrs()) do
|
||||||
|
if i.addr:match("%x%x:%x%x:%x%x:%x%x:%x%x:%x%x")
|
||||||
|
and not i.addr:match("00:00:00:00:00:00") then
|
||||||
|
ifaces[i.addr] = i.name:match("[^:]+")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
for mac, iname in pairs(ifaces) do
|
||||||
|
table.insert(ret, mac.." "..hostname.."_"..iname.."\n")
|
||||||
|
end
|
||||||
|
|
||||||
|
return ret
|
||||||
|
end
|
||||||
|
|
||||||
|
local function publish_bat_hosts()
|
||||||
|
-- pass a raw chunk of data to alfred
|
||||||
|
local fd = io.popen("alfred -s " .. type_id, "w")
|
||||||
|
if fd then
|
||||||
|
local ret = generate_bat_hosts()
|
||||||
|
if ret then
|
||||||
|
fd:write(table.concat(ret))
|
||||||
|
end
|
||||||
|
fd:close()
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local function write_bat_hosts(rows)
|
||||||
|
local content = { "### File generated by alfred-mod-bat-hosts\n" }
|
||||||
|
|
||||||
|
-- merge the chunks from all nodes, de-escaping newlines
|
||||||
|
for _, row in ipairs(rows) do
|
||||||
|
local node, value = unpack(row)
|
||||||
|
table.insert(content, "# Node ".. node .. "\n")
|
||||||
|
table.insert(content, value:gsub("\x0a", "\n") .. "\n")
|
||||||
|
end
|
||||||
|
|
||||||
|
-- write parsed content down to disk
|
||||||
|
local fd = io.open("/tmp/bat-hosts", "w")
|
||||||
|
if fd then
|
||||||
|
fd:write(table.concat(content))
|
||||||
|
fd:close()
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local function receive_bat_hosts()
|
||||||
|
-- read raw chunks from alfred, convert them to a nested table and call write_bat_hosts
|
||||||
|
|
||||||
|
local fd = io.popen("alfred -r " .. type_id)
|
||||||
|
--[[ this command returns something like
|
||||||
|
{ "54:e6:fc:b9:cb:37", "00:11:22:33:44:55 ham_wlan0\x0a00:22:33:22:33:22 ham_eth0\x0a" },
|
||||||
|
{ "90:f6:52:bb:ec:57", "00:22:33:22:33:23 spam\x0a" },
|
||||||
|
]]--
|
||||||
|
|
||||||
|
if fd then
|
||||||
|
local output = fd:read("*a")
|
||||||
|
if output then
|
||||||
|
assert(loadstring("rows = {" .. output .. "}"))()
|
||||||
|
write_bat_hosts(rows)
|
||||||
|
end
|
||||||
|
fd:close()
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
publish_bat_hosts()
|
||||||
|
receive_bat_hosts()
|
Loading…
Reference in a new issue