Sample code for 30+ languages & platforms
PowerBuilder

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 PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Ssh
integer li_SshPort
string ls_Password
integer li_ChannelA
integer li_ChannelB
integer n
integer i
integer li_ChNum

li_Success = 0

//  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.

loo_Ssh = create oleobject
li_rc = loo_Ssh.ConnectToNewObject("Chilkat.Ssh")
if li_rc < 0 then
    destroy loo_Ssh
    MessageBox("Error","Connecting to COM object failed")
    return
end if

li_SshPort = 22
li_Success = loo_Ssh.Connect("ssh.example.com",li_SshPort)
if li_Success = 0 then
    Write-Debug loo_Ssh.LastErrorText
    destroy loo_Ssh
    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.
ls_Password = "mySshPassword"

li_Success = loo_Ssh.AuthenticatePw("mySshLogin",ls_Password)
if li_Success = 0 then
    Write-Debug loo_Ssh.LastErrorText
    destroy loo_Ssh
    return
end if

//  Open a couple of channels so the collection has more than one entry.
li_ChannelA = loo_Ssh.OpenSessionChannel()
if li_ChannelA < 0 then
    Write-Debug loo_Ssh.LastErrorText
    destroy loo_Ssh
    return
end if

li_ChannelB = loo_Ssh.OpenSessionChannel()
if li_ChannelB < 0 then
    Write-Debug loo_Ssh.LastErrorText
    destroy loo_Ssh
    return
end if

//  Enumerate the active channels.  Valid indexes are 0 through NumOpenChannels - 1.
n = loo_Ssh.NumOpenChannels
Write-Debug "Open channels: " + string(n)

for i = 0 to n - 1
    li_ChNum = loo_Ssh.GetChannelNumber(i)
    Write-Debug "Index " + string(i) + " is channel number " + string(li_ChNum)
next

loo_Ssh.Disconnect()


destroy loo_Ssh