Lua
Lua
SSH Tunnel Through an Outbound SOCKS Proxy
See more SSH Tunnel Examples
Demonstrates reaching the SSH server through an outbound SOCKS proxy using the SocksVersion, SocksHostname, SocksPort, SocksUsername, and SocksPassword properties.
Background: This is the reverse direction from dynamic forwarding: here a SOCKS proxy is used to reach the SSH server, for networks where outbound connections must pass through one.
SocksVersion is the switch — 0 means no SOCKS, while 4 or 5 selects the protocol. SOCKS5 supports username/password authentication and remote DNS; SOCKS4 supports only a username.Chilkat Lua Downloads
local chilkat = require("chilkat")
-- Demonstrates reaching the SSH server through an outbound SOCKS proxy using the SshTunnel
-- properties SocksVersion, SocksHostname, SocksPort, SocksUsername, and SocksPassword.
local tunnel = chilkat.SshTunnel.new()
-- Set SocksVersion to 4 or 5 to route the outbound connection to the SSH server through a SOCKS
-- proxy. A value of 0 (the default) means no SOCKS proxy.
tunnel.socks_version = 5
tunnel.socks_hostname = "socks.example.com"
tunnel.socks_port = 1080
-- SOCKS4 supports only a username; SOCKS5 supports username and password.
tunnel.socks_username = "myProxyLogin"
tunnel.socks_password = "myProxyPassword"
tunnel.dest_hostname = "db.internal.example.com"
tunnel.dest_port = 5432
-- Connect to the SSH server through the SOCKS proxy configured above.
local sshPort = 22
local success = tunnel:connect("ssh.example.com", sshPort)
if not success then
print(tunnel.last_error_text)
return
end
-- Normally you would not hard-code the password in source. You should instead obtain it
-- from an interactive prompt, environment variable, or a secrets vault.
local password = "mySshPassword"
success = tunnel:authenticate_pw("mySshLogin", password)
if not success then
print(tunnel.last_error_text)
return
end
local listenPort = 1080
success = tunnel:begin_accepting(listenPort)
if not success then
print(tunnel.last_error_text)
return
end
print("Tunnel established through the SOCKS proxy.")
local waitForThreadExit = true
success = tunnel:close_tunnel(waitForThreadExit)
if not success then
print(tunnel.last_error_text)
return
end