Lua
Lua
Disconnect All SSH Tunnel Clients
See more SSH Tunnel Examples
Demonstrates the Chilkat SshTunnel.DisconnectAllClients method, which disconnects all active tunnel clients while leaving the listener and SSH connection available. The only argument (waitForThreads) selects whether to wait for the client threads to exit.
Background: This is the complement of
StopAccepting: it clears out current clients but keeps accepting new ones, so the listener and SSH transport stay up. It is useful for forcibly resetting stuck or unwanted connections without tearing down the whole tunnel — for example, dropping all sessions before a maintenance action while leaving the tunnel ready to serve fresh connections immediately afterward.Chilkat Lua Downloads
local chilkat = require("chilkat")
-- Demonstrates the SshTunnel.DisconnectAllClients method, which disconnects all active tunnel
-- clients while leaving the listener and SSH connection available. The only argument
-- (waitForThreads) selects whether to wait for the client threads to exit.
local tunnel = chilkat.SshTunnel.new()
tunnel.dest_hostname = "db.internal.example.com"
tunnel.dest_port = 5432
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
-- ... clients connect and transfer data ...
-- Disconnect all current clients. The listener keeps running and will accept new clients.
local waitForThreads = true
success = tunnel:disconnect_all_clients(waitForThreads)
if not success then
print(tunnel.last_error_text)
return
end
print("All current clients disconnected; still accepting new ones.")
local waitForThreadExit = true
success = tunnel:close_tunnel(waitForThreadExit)
if not success then
print(tunnel.last_error_text)
return
end