Sample code for 30+ languages & platforms
AutoIt

Close an SSH Tunnel (CloseTunnel)

See more SSH Tunnel Examples

Demonstrates the Chilkat SshTunnel.CloseTunnel method, which disconnects active tunnel clients and stops the tunnel-manager thread. The only argument (waitForThreads) selects whether to wait for the worker threads to exit.

Background: This is the full shutdown — it stops the listener, disconnects clients, and ends the background threads, so it is what you call when the tunnel is no longer needed. Passing true for waitForThreads makes the call block until the workers have actually exited, which matters before the program terminates or reuses the object; if a worker fails to stop, the method returns false and LastErrorText explains why.

Chilkat AutoIt Downloads

AutoIt
Local $bSuccess = False

;  Demonstrates the SshTunnel.CloseTunnel method, which disconnects active tunnel clients and stops
;  the tunnel-manager thread.  The only argument (waitForThreads) selects whether to wait for the
;  worker 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

;  ... the tunnel runs ...

;  Shut everything down: disconnect clients and stop the tunnel-manager thread.  With
;  waitForThreads True, the method waits for the worker threads to exit.
Local $bWaitForThreads = True
$bSuccess = $oTunnel.CloseTunnel($bWaitForThreads)
If ($bSuccess = False) Then
    ConsoleWrite($oTunnel.LastErrorText & @CRLF)
    Exit
EndIf

ConsoleWrite("Tunnel closed." & @CRLF)