From 206e868531f180cfadaae1d185b5f4559d209ce8 Mon Sep 17 00:00:00 2001 From: Ted Trask Date: Fri, 11 Mar 2016 20:26:35 +0000 Subject: Replace nc executable with luasocket library for quagga status nc executable blocked forever in more recent versions --- bgp-model.lua | 33 ++++++++++++++++++++++++++++++--- ospf-model.lua | 34 +++++++++++++++++++++++++++++++--- zebra-model.lua | 35 +++++++++++++++++++++++++++++++---- 3 files changed, 92 insertions(+), 10 deletions(-) diff --git a/bgp-model.lua b/bgp-model.lua index c380163..9333c05 100644 --- a/bgp-model.lua +++ b/bgp-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/bgpd.conf" local processname = "bgpd" local packagename = "quagga" +local portnumber = 2605 -- ################################################################################ -- 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 telnetshowipbgp() local output = {} local configfile = parseconfigfile() or {} - local cmd = (configfile.password or "") .. "\nshow ip bgp\nquit\n" - local result, errtxt = modelfunctions.run_executable({"nc", "localhost", "bgpd"}, false, cmd) - if result == "" then + local cmd = {configfile.password, "show ip bgp", "quit"} + local result = send_to_event_socket(cmd) + if not result or result == "" then result = "Failed to find routes" end local startout, stopout 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 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 -- cgit v1.2.3