Sample code for 30+ languages & platforms
DataFlex

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

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoSsh
    Integer iSshPort
    String sPassword
    Integer iChannelA
    Integer iChannelB
    Integer n
    Integer i
    Integer iChNum
    String sTemp1

    Move False To iSuccess

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

    Get Create (RefClass(cComChilkatSsh)) To hoSsh
    If (Not(IsComObjectCreated(hoSsh))) Begin
        Send CreateComObject of hoSsh
    End

    Move 22 To iSshPort
    Get ComConnect Of hoSsh "ssh.example.com" iSshPort To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoSsh To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  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.
    Move "mySshPassword" To sPassword

    Get ComAuthenticatePw Of hoSsh "mySshLogin" sPassword To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoSsh To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  Open a couple of channels so the collection has more than one entry.
    Get ComOpenSessionChannel Of hoSsh To iChannelA
    If (iChannelA < 0) Begin
        Get ComLastErrorText Of hoSsh To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComOpenSessionChannel Of hoSsh To iChannelB
    If (iChannelB < 0) Begin
        Get ComLastErrorText Of hoSsh To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  Enumerate the active channels.  Valid indexes are 0 through NumOpenChannels - 1.
    Get ComNumOpenChannels Of hoSsh To n
    Showln "Open channels: " n

    For i From 0 To (n - 1)
        Get ComGetChannelNumber Of hoSsh i To iChNum
        Showln "Index " i " is channel number " iChNum
    Loop

    Send ComDisconnect To hoSsh


End_Procedure