Overview

LuaJIT FFI wrapper for libssh

Synopsis

-- Load module
local SSHConnection = require("ssh").SSHConnection
-- Load config from private.lua
local priv = loadfile("private.lua")()
-- Creating connection
local c, err = SSHConnection.open(priv.params)

if c == nil then
   print ("ERROR: " .. (err or "nil"))
else
   -- Authenticating
   c:auth("password", priv.password)
   -- Getting banner (strange function)
   local banner = c:get_issue_banner()
   print(banner)
   -- Getting last 50 messages from dmesg on remote machine
   print (c:request_exec("dmesg | tail -50"))
   -- Getting /etc/passwd using callback
   print (c:request_exec("cat /etc/passwd", io.write))
   -- Closing connection
   c:close()
end

Status

You can connect to remote host using password, execute some commands and disconnect :) I want to add a bit more complete support, especially key-based authentication, but have no time, so any help is appreciated.

API

SSHConnection class

SSHConnection:open(params)

SSHConnection:set_option(option, value)

Options: host port user ssh_dir identity known_hosts timeout ssh1 * ssh2

Implementation progress:

SSH_OPTIONS_HOST = 0 // host
SSH_OPTIONS_PORT = 1 // port
SSH_OPTIONS_PORT_STR = 2 // not implemented
SSH_OPTIONS_FD = 3 // not implemented
SSH_OPTIONS_USER = 4 // user
SSH_OPTIONS_SSH_DIR = 5 // ssh_dir
SSH_OPTIONS_IDENTITY = 6 // identity
SSH_OPTIONS_ADD_IDENTITY = 7 // not implemented
SSH_OPTIONS_KNOWNHOSTS = 8 //known_hosts
SSH_OPTIONS_TIMEOUT = 9 // timeout
SSH_OPTIONS_TIMEOUT_USEC = 10 // not implemented
SSH_OPTIONS_SSH1 = 11 // ssh1
SSH_OPTIONS_SSH2 = 12 // ssh2
SSH_OPTIONS_LOG_VERBOSITY = 13 // not implemented
SSH_OPTIONS_LOG_VERBOSITY_STR = 14 // not implemented
SSH_OPTIONS_CIPHERS_C_S = 15 // not implemented
SSH_OPTIONS_CIPHERS_S_C  = 16 // not implemented
SSH_OPTIONS_COMPRESSION_C_S = 17 // not implemented
SSH_OPTIONS_COMPRESSION_S_C = 18 // not implemented
SSH_OPTIONS_PROXYCOMMAND = 19 // not implemented
SSH_OPTIONS_BINDADDR = 20 // not implemented
SSH_OPTIONS_STRICTHOSTKEYCHECK = 21 // not implemented
SSH_OPTIONS_COMPRESSION = 22 // not implemented
SSH_OPTIONS_COMPRESSION_LEVEL = 23 // not implemented

SSHConnection:request_exec(cmd, callback)

Executes cmd (should be a string) on remote server. If callback is given, executes it for each chunk of size 1024 read, otherwise returns complete output as string.