interfaces 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. local list = util.exec('batctl if')
  2. local wireless = {}
  3. local tunnel = {}
  4. local other = {}
  5. local function get_address(t, ifname)
  6. pcall(
  7. function()
  8. table.insert(t, util.trim(fs.readfile('/sys/class/net/' .. ifname .. '/address')))
  9. end
  10. )
  11. end
  12. local function is_wireless(ifname)
  13. local type = fs.stat('/sys/class/net/' .. ifname .. '/wireless', 'type')
  14. return type == 'directory'
  15. end
  16. local function is_tuntap(ifname)
  17. local type = fs.stat('/sys/class/net/' .. ifname .. '/tun_flags', 'type')
  18. return type == 'regular'
  19. end
  20. local function nil_table(t)
  21. if next(t) ~= nil then
  22. return t
  23. else
  24. return nil
  25. end
  26. end
  27. for _, line in ipairs(util.split(list)) do
  28. local ifname = line:match('^(.-):')
  29. if ifname ~= nil then
  30. if is_wireless(ifname) then
  31. get_address(wireless, ifname)
  32. elseif is_tuntap(ifname) then
  33. get_address(tunnel, ifname)
  34. else
  35. get_address(other, ifname)
  36. end
  37. end
  38. end
  39. return {
  40. wireless = nil_table(wireless),
  41. tunnel = nil_table(tunnel),
  42. other = nil_table(other)
  43. }