[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Reusing ssh connection in my Lua script
- From: Alexander Gladysh <agladysh@...>
- Date: Sat, 16 Oct 2010 22:13:46 +0400
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?
Thanks,
Alexander.
----------------------
local shell_exec = function(command, ...)
return assert(os.execute(table.concat({command, ...}, " ")))
end
local shell_read = function(command, ...)
local f = assert(io.popen(table.concat({command, ...}, " ")))
local result = f:read("*a")
f:close()
f = nil
return result
end
local shell_exec_remote = function(host, command, ...)
return shell_exec(
"ssh", host, Q(table.concat({command, ...}, " ")) -- Q is a
quotation function
)
end
local shell_read_remote = function(host, command, ...)
return shell_read(
"ssh", host, Q(table.concat({command, ...}, " "))
)
end
shell_exec_remote("my.host", "echo", "command1")
shell_exec_remote("my.host", "echo", "command2")
shell_exec_remote("my.host", "echo", "command2")
-- ...