#!/usr/bin/lua function pairsByKeys (t, f) local a = {} for n in pairs(t) do table.insert(a, n) end table.sort(a, f) local i = 0 -- iterator variable local iter = function () -- iterator function i = i + 1 if a[i] == nil then return nil else return a[i], t[a[i]] end end return iter end local f = io.popen("/usr/bin/ntpq -c readlist") local line ntp = {} while true do line = f:read("*l") -- read output of command if line == nil then break end local pl = line if string.find(line, "version=") then ntp["version"] = string.match(line, "version=\"[^\"]+\"") ntp["version"] = string.gsub(ntp["version"], "version=\"([^\"]+)\"", "%1") end if string.find(line, "processor=") then ntp["processor"] = string.match(line, "processor=\"[^\"]+\"") ntp["processor"] = string.gsub(ntp["processor"], "processor=\"([^\"]+)\"", "%1") end if string.find(line, "system=") then ntp["system"] = string.match(line, "system=\"[^\"]+\"") ntp["system"] = string.gsub(ntp["system"], "system=\"([^\"]+)\"", "%1") end if string.find(line, "reftime=") then ntp["reftime"] = string.match(line, "reftime=%x%x%x%x%x%x%x%x%.%x%x%x%x%x%x%x%x%s+%w+,[^,]+") ntp["reftime"] = string.gsub(ntp["reftime"], "reftime=(%x%x%x%x%x%x%x%x%.%x%x%x%x%x%x%x%x)%s+(%w+),%s+(.+)", "%2 %3 (%1)") end if string.find(line, "clock=") then ntp["clock"] = string.match(line, "clock=%x%x%x%x%x%x%x%x%.%x%x%x%x%x%x%x%x%s+%w+,[^,]+") ntp["clock"] = string.gsub(ntp["clock"], "clock=(%x%x%x%x%x%x%x%x%.%x%x%x%x%x%x%x%x)%s+(%w+),%s+(.+)", "%2 %3 (%1)") end if string.find(line, "precision=") then ntp["precision"] = string.match(line, "precision=[^,]+") ntp["precision"] = string.gsub(ntp["precision"], "precision=([^,]+)", "%1") end if string.find(line, "rootdelay=") then ntp["rootdelay"] = string.match(line, "rootdelay=[^,]+") ntp["rootdelay"] = string.gsub(ntp["rootdelay"], "rootdelay=([^,]+)", "%1") end if string.find(line, "rootdispersion=") then ntp["rootdispersion"] = string.match(line, "rootdispersion=[^,]+") ntp["rootdispersion"] = string.gsub(ntp["rootdispersion"], "rootdispersion=([^,]+)", "%1") end if string.find(line, "leap=") then ntp["leap"] = string.match(line, "leap=[^,]+") ntp["leap"] = string.gsub(ntp["leap"], "leap=([^,]+)", "%1") end if string.find(line, "stratum=") then ntp["stratum"] = string.match(line, "stratum=[^,]+") ntp["stratum"] = string.gsub(ntp["stratum"], "stratum=([^,]+)", "%1") end if string.find(line, "peer=") then ntp["peer"] = string.match(line, "peer=[^,]+") ntp["peer"] = string.gsub(ntp["peer"], "peer=([^,]+)", "%1") end if string.find(line, "refid=") then ntp["refid"] = string.match(line, "refid=[^,]+") ntp["refid"] = string.gsub(ntp["refid"], "refid=([^,]+)", "%1") end if string.find(line, "poll=") then ntp["poll"] = string.match(line, "poll=[^,]+") ntp["poll"] = string.gsub(ntp["poll"], "poll=([^,]+)", "%1") end if string.find(line, "offset=") then ntp["offset"] = string.match(line, "offset=[^,]+") ntp["offset"] = string.gsub(ntp["offset"], "offset=([^,]+)", "%1") end if string.find(line, "frequency=") then ntp["frequency"] = string.match(line, "frequency=[^,]+") ntp["frequency"] = string.gsub(ntp["frequency"], "frequency=([^,]+)", "%1") end if string.find(line, "jitter=") then ntp["jitter"] = string.match(line, "jitter=[^,]+") ntp["jitter"] = string.gsub(ntp["jitter"], "jitter=([^,]+)", "%1") end if string.find(line, "noise=") then ntp["noise"] = string.match(line, "noise=[^,]+") ntp["noise"] = string.gsub(ntp["noise"], "noise=([^,]+)", "%1") end if string.find(line, "stability=") then ntp["stability"] = string.match(line, "stability=[^,]+") ntp["stability"] = string.gsub(ntp["stability"], "stability=([^,]+)", "%1") end if string.find(line, "tai=") then ntp["tai"] = string.match(line, "tai=[^,]+") ntp["tai"] = string.gsub(ntp["tai"], "tai=([^,]+)", "%1") end if string.find(line, "state=") then ntp["state"] = string.match(line, "state=[^,]+") ntp["state"] = string.gsub(ntp["state"], "state=([^,]+)", "%1") end end for k,v in pairsByKeys(ntp) do print(string.format("%s%s", k, v)) end f:close()