PureBasic
PureBasic
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 PureBasic Downloads
IncludeFile "CkSshTunnel.pb"
Procedure ChilkatExample()
success.i = 0
; 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.
tunnel.i = CkSshTunnel::ckCreate()
If tunnel.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkSshTunnel::setCkDestHostname(tunnel, "db.internal.example.com")
CkSshTunnel::setCkDestPort(tunnel, 5432)
sshPort.i = 22
success = CkSshTunnel::ckConnect(tunnel,"ssh.example.com",sshPort)
If success = 0
Debug CkSshTunnel::ckLastErrorText(tunnel)
CkSshTunnel::ckDispose(tunnel)
ProcedureReturn
EndIf
; 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.
password.s = "mySshPassword"
success = CkSshTunnel::ckAuthenticatePw(tunnel,"mySshLogin",password)
If success = 0
Debug CkSshTunnel::ckLastErrorText(tunnel)
CkSshTunnel::ckDispose(tunnel)
ProcedureReturn
EndIf
listenPort.i = 1080
success = CkSshTunnel::ckBeginAccepting(tunnel,listenPort)
If success = 0
Debug CkSshTunnel::ckLastErrorText(tunnel)
CkSshTunnel::ckDispose(tunnel)
ProcedureReturn
EndIf
; ... clients connect and transfer data ...
; Disconnect all current clients. The listener keeps running and will accept new clients.
waitForThreads.i = 1
success = CkSshTunnel::ckDisconnectAllClients(tunnel,waitForThreads)
If success = 0
Debug CkSshTunnel::ckLastErrorText(tunnel)
CkSshTunnel::ckDispose(tunnel)
ProcedureReturn
EndIf
Debug "All current clients disconnected; still accepting new ones."
waitForThreadExit.i = 1
success = CkSshTunnel::ckCloseTunnel(tunnel,waitForThreadExit)
If success = 0
Debug CkSshTunnel::ckLastErrorText(tunnel)
CkSshTunnel::ckDispose(tunnel)
ProcedureReturn
EndIf
CkSshTunnel::ckDispose(tunnel)
ProcedureReturn
EndProcedure