Module eco.ssh
SSH support (libssh2).
This module provides a coroutine-friendly SSH client session with helpers for:
- executing a remote command
- SCP receive/send (string or file)
The underlying implementation uses non-blocking libssh2 calls. When libssh2
returns EAGAIN, the current coroutine yields until the socket becomes
readable/writable.
Note: currently only password authentication is attempted (when the server advertises it). Other auth methods (public key, agent, keyboard- interactive) are not handled by this Lua wrapper.
Functions
| new (ipaddr, port, username[, password]) | Create a new SSH session. |
Class session
| session:exec (cmd[, timeout]) | Execute a command on the remote host. |
| session:scp_recv (source[, dest]) | Receive a remote file via SCP. |
| session:scp_send (data, dest) | Send data to the remote host via SCP. |
| session:scp_sendfile (source, dest) | Send a local file to the remote host via SCP. |
| session:disconnect ([reaason[, description]]) | Disconnect the SSH session. |
| session:free () | Close and free the session. |
Functions
- new (ipaddr, port, username[, password])
-
Create a new SSH session.
This call:
- connects to the remote TCP endpoint
- performs SSH handshake
- attempts password authentication if the server advertises it
Parameters:
- ipaddr string Remote IP address (IPv4/IPv6).
- port int Remote port.
- username string Username.
- password string Password (used when password auth is supported). (optional)
Returns:
-
session
Session object.
Or
- nil On failure.
- string Error message.
Class session
Instances are returned by ssh.new. The object supports the Lua 5.4 <close>
attribute (calls session:free automatically).
- session:exec (cmd[, timeout])
-
Execute a command on the remote host.
The returned output is a concatenation of stdout and stderr.
Parameters:
- cmd string Command string.
- timeout number Timeout in seconds for reading output. (optional)
Returns:
Or
- nil On failure.
- string Error message.
Usage:
local ssh = require 'eco.ssh' local eco = require 'eco' eco.run(function() local session<close>, err = ssh.new('127.0.0.1', 22, 'root', 'password') assert(session, err) local out, code, signal = session:exec('uptime') assert(out, code) print('exit:', code, 'signal:', signal) print(out) end) eco.loop()
- session:scp_recv (source[, dest])
-
Receive a remote file via SCP.
If
destis provided, the remote file will be stored to that local path and this method returns the number of bytes written.If
destis omitted, the whole file content is returned as a Lua string.Parameters:
Returns:
-
any
result When
destis not provided: file content (string). Whendestis provided: bytes written (int).Or
- nil On failure.
- string Error message.
Usage:
local ssh = require 'eco.ssh' local eco = require 'eco' eco.run(function() local session<close> = assert(ssh.new('127.0.0.1', 22, 'root', 'password')) local data = assert(session:scp_recv('/etc/os-release')) print(data) assert(session:scp_recv('/etc/os-release', '/tmp/os-release')) end) eco.loop()
- session:scp_send (data, dest)
-
Send data to the remote host via SCP.
Creates/overwrites
deston the remote host.Parameters:
Returns:
-
boolean
true On success.
Or
- nil On failure.
- string Error message.
Usage:
local ok, err = session:scp_send('hello\n', '/tmp/hello') assert(ok, err)
- session:scp_sendfile (source, dest)
-
Send a local file to the remote host via SCP.
Parameters:
Returns:
-
boolean
true On success.
Or
- nil On failure.
- string Error message.
- session:disconnect ([reaason[, description]])
-
Disconnect the SSH session.
Parameters:
- reaason int Disconnect reason code (libssh2 constant). (optional)
- description string Disconnect description. (optional)
Returns:
-
boolean
true On success.
Or
- nil On failure.
- string Error message.
- session:free ()
-
Close and free the session.
This is also used as the
__gcand__closemetamethod.