drop nixio dependency from bat-hosts.lua

This commit is contained in:
jahead 2013-09-22 18:17:02 -03:00 committed by Gui Iribarren
parent 393bc88a13
commit fe76518e48

View file

@ -1,23 +1,50 @@
#!/usr/bin/lua
local nxo = require "nixio"
local type_id = 64 -- bat-hosts
function get_hostname()
local hostfile = io.open("/proc/sys/kernel/hostname", "r")
local ret_string = hostfile:read("*a")
ret_string = string.gsub(ret_string, "\n", "")
hostfile:close()
return ret_string
end
function get_interfaces_names()
local i, ret
i = 0
ret = {}
for name in io.popen("ls -1 /sys/class/net/"):lines() do
if name ~= "lo" then
i = i + 1
ret[i] = name
end
end
return ret
end
function get_interface_address(name)
local addressfile = io.open("/sys/class/net/"..name.."/address", "r")
local ret_string = addressfile:read("*a")
ret_string = string.gsub(ret_string, "\n", "")
addressfile:close()
return ret_string
end
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
local hostname = get_hostname()
-- 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
for n, i in ipairs(get_interfaces_names()) do
local address = get_interface_address(i)
ifaces[address] = i
end
for mac, iname in pairs(ifaces) do