diff options
author | Ted Trask <ttrask01@yahoo.com> | 2016-03-11 20:26:35 +0000 |
---|---|---|
committer | Ted Trask <ttrask01@yahoo.com> | 2016-03-11 20:26:35 +0000 |
commit | 206e868531f180cfadaae1d185b5f4559d209ce8 (patch) | |
tree | 64587e94d00e8a284074798ad7e713de24202b2d /zebra-model.lua | |
parent | efe888917f517a85548d87e530d77bba16186ba2 (diff) | |
download | acf-quagga-206e868531f180cfadaae1d185b5f4559d209ce8.tar.bz2 acf-quagga-206e868531f180cfadaae1d185b5f4559d209ce8.tar.xz |
Replace nc executable with luasocket library for quagga status
nc executable blocked forever in more recent versions
Diffstat (limited to 'zebra-model.lua')
-rw-r--r-- | zebra-model.lua | 35 |
1 files changed, 31 insertions, 4 deletions
diff --git a/zebra-model.lua b/zebra-model.lua index 3dcf53b..07950a0 100644 --- a/zebra-model.lua +++ b/zebra-model.lua @@ -3,6 +3,7 @@ local mymodule = {} -- Load libraries modelfunctions = require("modelfunctions") format = require("acf.format") +socket = require("socket") -- Set variables local path="PATH=/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin " @@ -10,6 +11,7 @@ local path="PATH=/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin " local configfile = "/etc/quagga/zebra.conf" local processname = "zebra" local packagename = "quagga" +local portnumber = 2601 -- ################################################################################ -- LOCAL FUNCTIONS @@ -33,13 +35,38 @@ local function parseconfigfile() return conf end +-- Use socket.protect to create a function that will not throw exceptions and cleans itself up +local send_to_event_socket = socket.protect(function(cmd) + -- connect to freeswitch + local conn = socket.try(socket.connect("127.0.0.1", portnumber)) + conn:settimeout(1) -- timeout of 1 second for response to each command + -- create a try function that closes 'conn' on error + local try = socket.newtry(function() conn:close() end) + -- do everything reassured conn will be closed + local out = {} + local o + repeat + o,e = conn:receive() + out[#out+1] = o + until o == nil + for i,c in ipairs(cmd) do + try(conn:send(c.."\n")) + repeat + o,e = conn:receive() + out[#out+1] = o + until o == nil + end + conn:close() + return table.concat(out, "\n") or "" +end) + local function telnetshowip() local output = {} local configfile = parseconfigfile() or {} - local cmd = (configfile.password or "") .. "\nshow ip route\nquit\n" - local result, errtxt = modelfunctions.run_executable({"nc", "localhost", "zebra"}, false, cmd) + local cmd = {configfile.password, "show ip route", "quit"} + local result = send_to_event_socket(cmd) local validoutput - if result == "" then + if not result or result == "" then result = "Failed to find routes" end local startout, stopout @@ -49,7 +76,7 @@ local function telnetshowip() startout = #output+1 elseif (string.find(line, "^Codes:")) then startout = #output - elseif (string.find(line, "> quit")) then + elseif (string.find(line, "^quit")) then stopout = #output-1 end end |