Sample code for 30+ languages & platforms
Xojo Plugin

Enumerate Active SSH Channel Numbers

See more SSH Examples

Demonstrates the Chilkat Ssh.GetChannelNumber method, which returns the channel number at a zero-based enumeration index in the active channel collection. The only argument is the index — an enumeration position, not itself a channel number. Valid indexes run from 0 through NumOpenChannels - 1, and out-of-range indexes return -1.

Background: One SSH connection can carry many simultaneous channels, and this method lets you walk the live set rather than tracking every channel number yourself. The index-versus-channel-number distinction is the key point: index 0 is simply the first channel in the collection and may well be channel number 3. As channels open and close the enumeration shifts, so treat an index as valid only for the moment you read it.

Chilkat Xojo Plugin Downloads

Xojo Plugin
Dim success As Boolean
success = False

//  Demonstrates the Ssh.GetChannelNumber method, which returns the channel number at a
//  zero-based enumeration index in the active channel collection.  The only argument is the
//  index, which is an enumeration position and not itself a channel number.

Dim ssh As New Chilkat.Ssh

Dim sshPort As Int32
sshPort = 22
success = ssh.Connect("ssh.example.com",sshPort)
If (success = False) Then
    System.DebugLog(ssh.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 = ssh.AuthenticatePw("mySshLogin",password)
If (success = False) Then
    System.DebugLog(ssh.LastErrorText)
    Return
End If

//  Open a couple of channels so the collection has more than one entry.
Dim channelA As Int32
channelA = ssh.OpenSessionChannel()
If (channelA < 0) Then
    System.DebugLog(ssh.LastErrorText)
    Return
End If

Dim channelB As Int32
channelB = ssh.OpenSessionChannel()
If (channelB < 0) Then
    System.DebugLog(ssh.LastErrorText)
    Return
End If

//  Enumerate the active channels.  Valid indexes are 0 through NumOpenChannels - 1.
Dim n As Int32
n = ssh.NumOpenChannels
System.DebugLog("Open channels: " + Str(n))
Dim i As Int32
For i = 0 To n - 1
    Dim chNum As Int32
    chNum = ssh.GetChannelNumber(i)
    System.DebugLog("Index " + Str(i) + " is channel number " + Str(chNum))
Next

ssh.Disconnect