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 ++++++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) (limited to 'bgp-model.lua') 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 -- cgit v1.2.3