Sample code for 30+ languages & platforms
Xojo Plugin

Check if the SSH Tunnel's Transport is Connected

See more SSH Tunnel Examples

Demonstrates the Chilkat SshTunnel.IsSshConnected method, which returns whether the SSH transport used by the tunnel is connected. It takes no arguments.

Background: A tunnel has two independent parts: the SSH transport to the server and the local listener that accepts connections to forward. This method reports only the first. The distinction matters because the listener can keep accepting local connections even after the SSH transport has dropped — those connections would then fail to forward — so a health check on a long-running tunnel should verify the SSH side explicitly.

Chilkat Xojo Plugin Downloads

Xojo Plugin
Dim success As Boolean
success = False

//  Demonstrates the SshTunnel.IsSshConnected method, which returns whether the SSH transport used
//  by the tunnel is connected.  It takes no arguments.

Dim tunnel As New Chilkat.SshTunnel

//  The tunnel forwards accepted local connections to this destination through the SSH server.
tunnel.DestHostname = "db.internal.example.com"
tunnel.DestPort = 5432

//  Connect to the SSH server.  This performs the TCP/proxy connection and SSH handshake, but
//  does not authenticate yet.
Dim sshPort As Int32
sshPort = 22
success = tunnel.Connect("ssh.example.com",sshPort)
If (success = False) Then
    System.DebugLog(tunnel.LastErrorText)
    Return
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.
Dim password As String
password = "mySshPassword"

success = tunnel.AuthenticatePw("mySshLogin",password)
If (success = False) Then
    System.DebugLog(tunnel.LastErrorText)
    Return
End If

//  Check whether the SSH transport is connected.  This is independent of the local listener
//  state -- the listener can still be accepting local connections even when this is 0,
//  though those connections would fail to forward.
If (tunnel.IsSshConnected()) Then
    System.DebugLog("The SSH transport is connected.")
Else
    System.DebugLog("The SSH transport is not connected.")
End If

Dim waitForThreadExit As Boolean
waitForThreadExit = True
success = tunnel.CloseTunnel(waitForThreadExit)
If (success = False) Then
    System.DebugLog(tunnel.LastErrorText)
    Return
End If