lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

Hi, list!

I have a complex Lua "shell" script tool which executes a lot of
commands remotely (see code below).

Now, I've come to the point where reistablishing ssh connection for
each command costs too much time.

I wonder, if there is a way to safely reuse the connection?

I thought about setting up "ssh <host> bash" and piping commands to it
-- but io.popen has only one direction... Also, what if I want to pipe
in some data? It feels fragile.

I'm looking for a solution that will allow me to keep the interface of
shell_exec_remote and shell_read_remote functions (if they'll start
using upvalues, that's ok). (But if I can't keep the interface, no
problem, I'll cope with that.)

Any advice?



local shell_exec = function(command, ...)
  return assert(os.execute(table.concat({command, ...}, " ")))

local shell_read = function(command, ...)
  local f = assert(io.popen(table.concat({command, ...}, " ")))
  local result = f:read("*a")
  f = nil
  return result

local shell_exec_remote = function(host, command, ...)
  return shell_exec(
      "ssh", host, Q(table.concat({command, ...}, " ")) -- Q is a
quotation function

local shell_read_remote = function(host, command, ...)
  return shell_read(
      "ssh", host, Q(table.concat({command, ...}, " "))

shell_exec_remote("", "echo", "command1")
shell_exec_remote("", "echo", "command2")
shell_exec_remote("", "echo", "command2")
-- ...