[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Reusing ssh connection in my Lua script
- From: Gunnar Zötl <gz@...>
- Date: Sat, 16 Oct 2010 20:54:08 +0200
Apologies for blowing my own horn here... If you're on a unix system, you may want to look at lpty. That's exactly the kind of thing I wrote it for.
Gunnar
Es ist keine Schande, vor Angst zu verblöden.
Am 16.10.2010 um 20:13 schrieb Alexander Gladysh <agladysh@gmail.com>:
> 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")
> -- ...
>