drop nixio dependency from bat-hosts.lua
This commit is contained in:
parent
393bc88a13
commit
fe76518e48
1 changed files with 35 additions and 8 deletions
|
@ -1,23 +1,50 @@
|
||||||
#!/usr/bin/lua
|
#!/usr/bin/lua
|
||||||
|
|
||||||
local nxo = require "nixio"
|
|
||||||
|
|
||||||
local type_id = 64 -- bat-hosts
|
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()
|
local function generate_bat_hosts()
|
||||||
-- get hostname and interface macs/names
|
-- get hostname and interface macs/names
|
||||||
-- then return a table containing valid bat-hosts lines
|
-- then return a table containing valid bat-hosts lines
|
||||||
local n, i
|
local n, i
|
||||||
local ifaces, ret = {}, {}
|
local ifaces, ret = {}, {}
|
||||||
|
|
||||||
local hostname = nxo.uname().nodename
|
local hostname = get_hostname()
|
||||||
|
|
||||||
-- skip loopback ("lo") mac (00:00:00:00:00:00)
|
-- skip loopback ("lo") mac (00:00:00:00:00:00)
|
||||||
for n, i in ipairs(nxo.getifaddrs()) do
|
for n, i in ipairs(get_interfaces_names()) do
|
||||||
if i.addr:match("%x%x:%x%x:%x%x:%x%x:%x%x:%x%x")
|
local address = get_interface_address(i)
|
||||||
and not i.addr:match("00:00:00:00:00:00") then
|
ifaces[address] = i
|
||||||
ifaces[i.addr] = i.name:match("[^:]+")
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
for mac, iname in pairs(ifaces) do
|
for mac, iname in pairs(ifaces) do
|
||||||
|
|
Loading…
Reference in a new issue