Sample code for 30+ languages & platforms
Lua

Route Socket Connections through an SSH Object

See more Socket/SSL/TLS Examples

Demonstrates Socket.UseSsh, which configures a socket to route subsequent Connect calls through an already connected and authenticated Ssh object using SSH port forwarding.

Background. After UseSsh, the destination host and port passed to Connect are reached through the SSH tunnel rather than by a direct TCP connection. This is one of several SSH-tunneling approaches the Socket class supports.

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

local ssh = chilkat.Ssh.new()

-- Connect and authenticate a standalone SSH object.
local success = ssh:connect("ssh.example.com", 22)
if not success then
    print(ssh.last_error_text)
    return
end

-- The SSH password should come from a secure source rather than being hard-coded.
local sshPassword = "mySshPassword"
success = ssh:authenticate_pw("sshUser", sshPassword)
if not success then
    print(ssh.last_error_text)
    return
end

local socket = chilkat.Socket.new()

-- Route subsequent Connect calls through the already connected and authenticated SSH object.  The
-- destination is reached by SSH port forwarding rather than by a direct TCP connection.
success = socket:use_ssh(ssh)
if not success then
    print(socket.last_error_text)
    return
end

-- Connect to the destination through the SSH tunnel.
local bTls = false
local maxWaitMs = 5000
success = socket:connect("db.internal", 5432, bTls, maxWaitMs)
if not success then
    print(socket.last_error_text)
    return
end

print("Connected to the destination through the SSH tunnel.")