Sample code for 30+ languages & platforms
AutoIt

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 AutoIt Downloads

AutoIt
Local $bSuccess = False

;  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.

$oTunnel = ObjCreate("Chilkat.SshTunnel")

$oTunnel.DestHostname = "db.internal.example.com"
$oTunnel.DestPort = 5432

Local $iSshPort = 22
$bSuccess = $oTunnel.Connect("ssh.example.com",$iSshPort)
If ($bSuccess = False) Then
    ConsoleWrite($oTunnel.LastErrorText & @CRLF)
    Exit
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.
Local $sPassword = "mySshPassword"

$bSuccess = $oTunnel.AuthenticatePw("mySshLogin",$sPassword)
If ($bSuccess = False) Then
    ConsoleWrite($oTunnel.LastErrorText & @CRLF)
    Exit
EndIf

Local $iListenPort = 1080
$bSuccess = $oTunnel.BeginAccepting($iListenPort)
If ($bSuccess = False) Then
    ConsoleWrite($oTunnel.LastErrorText & @CRLF)
    Exit
EndIf

;  ... clients connect and transfer data ...

;  Disconnect all current clients.  The listener keeps running and will accept new clients.
Local $bWaitForThreads = True
$bSuccess = $oTunnel.DisconnectAllClients($bWaitForThreads)
If ($bSuccess = False) Then
    ConsoleWrite($oTunnel.LastErrorText & @CRLF)
    Exit
EndIf

ConsoleWrite("All current clients disconnected; still accepting new ones." & @CRLF)

Local $bWaitForThreadExit = True
$bSuccess = $oTunnel.CloseTunnel($bWaitForThreadExit)
If ($bSuccess = False) Then
    ConsoleWrite($oTunnel.LastErrorText & @CRLF)
    Exit
EndIf