Sample code for 30+ languages & platforms
DataFlex

Get the Type of an Active SSH Channel

See more SSH Examples

Demonstrates the Chilkat Ssh.GetChannelType method, which returns the type of the channel at a zero-based enumeration index. The only argument is the index — an enumeration position, not a channel number. Values include session, x11, forwarded-tcpip, and direct-tcpip.

Background: Paired with GetChannelNumber, this turns the channel collection into a readable inventory of what a connection is currently doing — which channels are running commands (session) versus tunneling TCP traffic (direct-tcpip). That is valuable for diagnostics and for application code that manages several channel kinds at once. An out-of-range index causes the string-returning method to report failure.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoSsh
    Integer iSshPort
    String sPassword
    Integer iChannelNum
    Integer n
    Integer i
    Integer iChNum
    String sChType
    String sTemp1
    Boolean bTemp1

    Move False To iSuccess

    //  Demonstrates the Ssh.GetChannelType method, which returns the type of the channel at a
    //  zero-based enumeration index.  The only argument is the index, which is an enumeration
    //  position and not a channel number.  Values include "session" and "direct-tcpip".

    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

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

    //  Enumerate the active channels and report each one's number and type.
    Get ComNumOpenChannels Of hoSsh To n

    For i From 0 To (n - 1)
        Get ComGetChannelNumber Of hoSsh i To iChNum
        Get ComGetChannelType Of hoSsh i To sChType
        Get ComLastMethodSuccess Of hoSsh To bTemp1
        If (bTemp1 = False) Begin
            Get ComLastErrorText Of hoSsh To sTemp1
            Showln sTemp1
            Procedure_Return
        End

        Showln "Channel " iChNum " is of type " sChType
    Loop

    Send ComDisconnect To hoSsh


End_Procedure