diff --git a/files/usr/lib/lua/aredn/utils.lua b/files/usr/lib/lua/aredn/utils.lua index 3359c51d..3d6601e3 100755 --- a/files/usr/lib/lua/aredn/utils.lua +++ b/files/usr/lib/lua/aredn/utils.lua @@ -304,6 +304,42 @@ function getTraceroute(target) return routes end +------------------------------------- +-- Returns traceroute +------------------------------------- +function getPing(target) + local pings = {} + local summary = { tx = -1, rx = -1, lossPercentage = -1, ip = "not found", minMs = -1, maxMs = -1, avgMs = -1 } + local output = capture("/bin/ping -w 10 " .. target) + local foundip = "unknown" + for _, line in ipairs(output:splitNewLine()) + do + local ip = line:match("^PING %S+ %(([%d%.]+)%):") + if ip then + summary.ip = ip + else + local ip, seq, ttl, time = line:match("bytes from ([%d%.]+): seq=(%d+) ttl=(%d+) time=(%S+) ms") + if ip then + pings[#pings + 1] = { ip = ip, seq = seq, ttl = ttl, timeMs = time } + else + local tx, rx, loss = line:match("^(%d+) packets transmitted, (%d+) packets received, (%d+)%% packet loss") + if tx then + summary.tx = tx + summary.rx = rx + summary.lossPercentage = loss + else + local min, avg, max = line:match("min/avg/max = ([%d%.]+)/([%d%.]+)/([%d%.]+) ms") + if min then + summary.minMs = min + summary.maxMs = max + summary.avgMs = avg + end + end + end + end + end + return { summary = summary, pings = pings } +end function file_trim(filename, maxl) local lines={} diff --git a/files/www/cgi-bin/api b/files/www/cgi-bin/api index 2de5ae1f..60801a90 100755 --- a/files/www/cgi-bin/api +++ b/files/www/cgi-bin/api @@ -44,7 +44,7 @@ require("nixio") local json = require("luci.jsonc") require("iwinfo") -local API_VERSION="1.4" +local API_VERSION="1.5" -- Function extensions os.capture = capture @@ -378,6 +378,15 @@ for page, comps in pairs(qsset) do info['pages'][page][tonode]="Invalid input!" end end + elseif page=="ping" then + for i,tonode in pairs(comps:split(',')) do + -- Validate that input as ip or hostname inside the mesh + if tonode:match("^[%d%.]+$") or tonode:match("^[%d%a%-%.%_]+$") then + info['pages'][page][tonode]=getPing(tonode) + else + info['pages'][page][tonode]="Invalid input!" + end + end elseif page=="mesh" then for i,comp in pairs(comps:split(',')) do if comp=="sysinfo" then