Sample code for 30+ languages & platforms
VB.NET

Check Whether an SSH Channel is Open

See more SSH Examples

Demonstrates the Chilkat Ssh.ChannelIsOpen method, which reports whether a channel number identifies a channel Chilkat currently considers open for application I/O. The only argument is the channel number. This example checks the channel before and after sending CLOSE.

Background: Because one SSH connection can carry many channels with independent lifetimes, code that manages several needs a way to ask whether a given one is still usable. This returns false for invalid or nonexistent channel numbers, after a local Disconnect, once remote closure is processed, and immediately after ChannelSendClose — which marks the channel locally closed without waiting for the remote side.

Chilkat VB.NET Downloads

VB.NET
Dim success As Boolean = False

'  Demonstrates the Ssh.ChannelIsOpen method, which reports whether a channel number identifies
'  a channel Chilkat currently considers open for application I/O.  The only argument is the
'  channel number.

Dim ssh As New Chilkat.Ssh

Dim sshPort As Integer = 22
success = ssh.Connect("ssh.example.com",sshPort)
If (success = False) Then
    Debug.WriteLine(ssh.LastErrorText)
    Exit Sub
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 = "mySshPassword"

success = ssh.AuthenticatePw("mySshLogin",password)
If (success = False) Then
    Debug.WriteLine(ssh.LastErrorText)
    Exit Sub
End If


'  Open a session channel.  A negative return value indicates failure.
Dim channelNum As Integer = ssh.OpenSessionChannel()
If (channelNum < 0) Then
    Debug.WriteLine(ssh.LastErrorText)
    Exit Sub
End If


If (ssh.ChannelIsOpen(channelNum)) Then
    Debug.WriteLine("The channel is open.")
Else
    Debug.WriteLine("The channel is not open.")
End If


'  Send CLOSE, which immediately marks the channel locally closed.
success = ssh.ChannelSendClose(channelNum)
If (success = False) Then
    Debug.WriteLine(ssh.LastErrorText)
    Exit Sub
End If


If (ssh.ChannelIsOpen(channelNum)) Then
    Debug.WriteLine("Still open.")
Else
    Debug.WriteLine("The channel is now closed.")
End If


ssh.Disconnect()