Tcl
Tcl
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 Tcl Downloads
load ./chilkat.dll
set success 0
# 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.
set tunnel [new_CkSshTunnel]
CkSshTunnel_put_DestHostname $tunnel "db.internal.example.com"
CkSshTunnel_put_DestPort $tunnel 5432
set sshPort 22
set success [CkSshTunnel_Connect $tunnel "ssh.example.com" $sshPort]
if {$success == 0} then {
puts [CkSshTunnel_lastErrorText $tunnel]
delete_CkSshTunnel $tunnel
exit
}
# 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.
set password "mySshPassword"
set success [CkSshTunnel_AuthenticatePw $tunnel "mySshLogin" $password]
if {$success == 0} then {
puts [CkSshTunnel_lastErrorText $tunnel]
delete_CkSshTunnel $tunnel
exit
}
set listenPort 1080
set success [CkSshTunnel_BeginAccepting $tunnel $listenPort]
if {$success == 0} then {
puts [CkSshTunnel_lastErrorText $tunnel]
delete_CkSshTunnel $tunnel
exit
}
# ... the tunnel runs for a while ...
# Stop accepting NEW connections, but leave already-connected clients running.
set waitForThread 1
set success [CkSshTunnel_StopAccepting $tunnel $waitForThread]
if {$success == 0} then {
puts [CkSshTunnel_lastErrorText $tunnel]
delete_CkSshTunnel $tunnel
exit
}
puts "The listener has stopped; existing clients continue."
set waitForThreadExit 1
set success [CkSshTunnel_CloseTunnel $tunnel $waitForThreadExit]
if {$success == 0} then {
puts [CkSshTunnel_lastErrorText $tunnel]
delete_CkSshTunnel $tunnel
exit
}
delete_CkSshTunnel $tunnel