Sample code for 30+ languages & platforms
AutoIt

Stop an SSH Tunnel Listener (StopAccepting)

See more SSH Tunnel Examples

Demonstrates the Chilkat SshTunnel.StopAccepting method, which stops the local listener so no new client connections are accepted. The only argument (waitForThread) selects whether to wait for the listener thread to exit. Existing forwarded clients remain connected.

Background: The distinction from CloseTunnel is the point: StopAccepting closes only the front door, leaving already-connected clients to finish their transfers undisturbed. That makes it the graceful way to drain a tunnel — stop taking new work, let existing work complete, then close — rather than cutting everyone off at once.

Chilkat AutoIt Downloads

AutoIt
Local $bSuccess = False

;  Demonstrates the SshTunnel.StopAccepting method, which stops the local listener so that no new
;  client connections are accepted.  The only argument (waitForThread) selects whether to wait for
;  the listener thread to exit.  Existing forwarded clients remain connected.

$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 for a while ...

;  Stop accepting NEW connections, but leave already-connected clients running.
Local $bWaitForThread = True
$bSuccess = $oTunnel.StopAccepting($bWaitForThread)
If ($bSuccess = False) Then
    ConsoleWrite($oTunnel.LastErrorText & @CRLF)
    Exit
EndIf

ConsoleWrite("The listener has stopped; existing clients continue." & @CRLF)

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