Classic ASP
Classic ASP
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 Classic ASP Downloads
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<%
success = 0
' 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.
set tunnel = Server.CreateObject("Chilkat.SshTunnel")
tunnel.DestHostname = "db.internal.example.com"
tunnel.DestPort = 5432
sshPort = 22
success = tunnel.Connect("ssh.example.com",sshPort)
If (success = 0) Then
Response.Write "<pre>" & Server.HTMLEncode( tunnel.LastErrorText) & "</pre>"
Response.End
End If
' 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 = "mySshPassword"
success = tunnel.AuthenticatePw("mySshLogin",password)
If (success = 0) Then
Response.Write "<pre>" & Server.HTMLEncode( tunnel.LastErrorText) & "</pre>"
Response.End
End If
listenPort = 1080
success = tunnel.BeginAccepting(listenPort)
If (success = 0) Then
Response.Write "<pre>" & Server.HTMLEncode( tunnel.LastErrorText) & "</pre>"
Response.End
End If
' ... the tunnel runs ...
' Shut everything down: disconnect clients and stop the tunnel-manager thread. With
' waitForThreads 1, the method waits for the worker threads to exit.
waitForThreads = 1
success = tunnel.CloseTunnel(waitForThreads)
If (success = 0) Then
Response.Write "<pre>" & Server.HTMLEncode( tunnel.LastErrorText) & "</pre>"
Response.End
End If
Response.Write "<pre>" & Server.HTMLEncode( "Tunnel closed.") & "</pre>"
%>
</body>
</html>