diff options
Diffstat (limited to 'ospf-model.lua')
-rw-r--r-- | ospf-model.lua | 34 |
1 files changed, 31 insertions, 3 deletions
diff --git a/ospf-model.lua b/ospf-model.lua index 27d779a..ebe82b7 100644 --- a/ospf-model.lua +++ b/ospf-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/ospfd.conf" local processname = "ospfd" local packagename = "quagga" +local portnumber = 2604 -- ################################################################################ -- LOCAL FUNCTIONS @@ -33,12 +35,37 @@ 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 ospf route\nquit\n" - local result, errtxt = modelfunctions.run_executable({"nc", "localhost", "ospfd"}, false, cmd) - if result == "" then + local cmd = {configfile.password, "show ip ospf route", "quit"} + local result = send_to_event_socket(cmd) + if not result or result == "" then result = "Failed to find routes" end local startout, stopout @@ -52,6 +79,7 @@ local function telnetshowip() end return table.concat(output,"\n",startout,stopout) end + -- ################################################################################ -- PUBLIC FUNCTIONS |