luci.fs = require "luci.fs"
luci.ip = require "luci.ip"
-local tonumber, ipairs, pairs, pcall = tonumber, ipairs, pairs, pcall
+local tonumber, ipairs, pairs, pcall, type =
+ tonumber, ipairs, pairs, pcall, type
--- LuCI Linux and POSIX system utilities.
-- @return Table containing all variables if no variable name is given
getenv = posix.getenv
---- Determine the current hostname.
+--- Get or set the current hostname.
+-- @param String containing a new hostname to set (optional)
-- @return String containing the system hostname
-function hostname()
- return posix.uname("%n")
+function hostname(newname)
+ if type(newname) == "string" and #newname > 0 then
+ luci.fs.writefile( "/proc/sys/kernel/hostname", newname .. "\n" )
+ return newname
+ else
+ return posix.uname("%n")
+ end
end
--- Returns the contents of a documented referred by an URL.
-- { "source", "dest", "nexthop", "metric", "refcount", "usecount",
-- "flags", "device" }
function net.defaultroute6()
- local route = nil
+ local route = nil
local routes6 = net.routes6()
- if not routes6 then
- return nil
- end
- for _, r in pairs(routes6) do
- if r.dest:prefix() == 0 and (not route or route.metric > r.metric) then
- route = r
+ if routes6 then
+ for _, r in pairs(routes6) do
+ if r.dest:prefix() == 0 and
+ (not route or route.metric > r.metric)
+ then
+ route = r
+ end
end
end
return route
-- { "source", "dest", "nexthop", "metric", "refcount", "usecount",
-- "flags", "device" }
function net.routes6()
- local routes = { }
-
- if not luci.fs.access("/proc/net/ipv6_route", "r") then
- return nil
- end
+ if luci.fs.access("/proc/net/ipv6_route", "r") then
+ local routes = { }
+
+ for line in io.lines("/proc/net/ipv6_route") do
+
+ local dst_ip, dst_prefix, src_ip, src_prefix, nexthop,
+ metric, refcnt, usecnt, flags, dev = line:match(
+ "([a-f0-9]+) ([a-f0-9]+) " ..
+ "([a-f0-9]+) ([a-f0-9]+) " ..
+ "([a-f0-9]+) ([a-f0-9]+) " ..
+ "([a-f0-9]+) ([a-f0-9]+) " ..
+ "([a-f0-9]+) +([^%s]+)"
+ )
- for line in io.lines("/proc/net/ipv6_route") do
+ src_ip = luci.ip.Hex(
+ src_ip, tonumber(src_prefix, 16), luci.ip.FAMILY_INET6, false
+ )
- local dst_ip, dst_prefix, src_ip, src_prefix, nexthop,
- metric, refcnt, usecnt, flags, dev = line:match(
- "([a-f0-9]+) ([a-f0-9]+) " ..
- "([a-f0-9]+) ([a-f0-9]+) " ..
- "([a-f0-9]+) ([a-f0-9]+) " ..
- "([a-f0-9]+) ([a-f0-9]+) " ..
- "([a-f0-9]+) +([^%s]+)"
- )
+ dst_ip = luci.ip.Hex(
+ dst_ip, tonumber(dst_prefix, 16), luci.ip.FAMILY_INET6, false
+ )
- src_ip = luci.ip.Hex(
- src_ip, tonumber(src_prefix, 16), luci.ip.FAMILY_INET6, false
- )
+ nexthop = luci.ip.Hex( nexthop, 128, luci.ip.FAMILY_INET6, false )
- dst_ip = luci.ip.Hex(
- dst_ip, tonumber(dst_prefix, 16), luci.ip.FAMILY_INET6, false
- )
+ routes[#routes+1] = {
+ source = src_ip,
+ dest = dst_ip,
+ nexthop = nexthop,
+ metric = tonumber(metric, 16),
+ refcount = tonumber(refcnt, 16),
+ usecount = tonumber(usecnt, 16),
+ flags = tonumber(flags, 16),
+ device = dev
+ }
+ end
- nexthop = luci.ip.Hex( nexthop, 128, luci.ip.FAMILY_INET6, false )
-
- routes[#routes+1] = {
- source = src_ip,
- dest = dst_ip,
- nexthop = nexthop,
- metric = tonumber(metric, 16),
- refcount = tonumber(refcnt, 16),
- usecount = tonumber(usecnt, 16),
- flags = tonumber(flags, 16),
- device = dev
- }
+ return routes
end
-
- return routes
end
--- Tests whether the given host responds to ping probes.
--- Get iwconfig output for all wireless devices.
-- @return Table of tables containing the iwconfing output for each wifi device
function wifi.getiwconfig()
- local cnt = luci.util.exec("/usr/sbin/iwconfig 2>/dev/null")
+ local cnt = luci.util.exec("PATH=/sbin:/usr/sbin iwconfig 2>/dev/null")
local iwc = {}
for i, l in pairs(luci.util.split(luci.util.trim(cnt), "\n\n")) do
return iface and (iws[iface] or {}) or iws
end
+--- Get available channels from given wireless iface.
+-- @param iface Wireless interface (optional)
+-- @return Table of available channels
+function wifi.channels(iface)
+ local cmd = "iwlist " .. ( iface or "" ) .. " freq 2>/dev/null"
+ local cns = { }
+
+ local fd = io.popen(cmd)
+ if fd then
+ local ln, c, f
+ while true do
+ ln = fd:read("*l")
+ if not ln then break end
+ c, f = ln:match("Channel (%d+) : (%d+%.%d+) GHz")
+ if c and f then
+ cns[tonumber(c)] = tonumber(f)
+ end
+ end
+ fd:close()
+ end
+
+ if not ((pairs(cns))(cns)) then
+ cns = {
+ 2.412, 2.417, 2.422, 2.427, 2.432, 2.437,
+ 2.442, 2.447, 2.452, 2.457, 2.462
+ }
+ end
+
+ return cns
+end
+
--- LuCI system utilities / init related functions.
-- @class module