Sample code for 30+ languages & platforms
PureBasic

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

PureBasic
IncludeFile "CkSsh.pb"

Procedure ChilkatExample()

    success.i = 0

    ;  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".

    ssh.i = CkSsh::ckCreate()
    If ssh.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    sshPort.i = 22
    success = CkSsh::ckConnect(ssh,"ssh.example.com",sshPort)
    If success = 0
        Debug CkSsh::ckLastErrorText(ssh)
        CkSsh::ckDispose(ssh)
        ProcedureReturn
    EndIf

    ;  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.
    password.s = "mySshPassword"

    success = CkSsh::ckAuthenticatePw(ssh,"mySshLogin",password)
    If success = 0
        Debug CkSsh::ckLastErrorText(ssh)
        CkSsh::ckDispose(ssh)
        ProcedureReturn
    EndIf

    channelNum.i = CkSsh::ckOpenSessionChannel(ssh)
    If channelNum < 0
        Debug CkSsh::ckLastErrorText(ssh)
        CkSsh::ckDispose(ssh)
        ProcedureReturn
    EndIf

    ;  Enumerate the active channels and report each one's number and type.
    n.i = CkSsh::ckNumOpenChannels(ssh)
    i.i
    For i = 0 To n - 1
        chNum.i = CkSsh::ckGetChannelNumber(ssh,i)
        chType.s = CkSsh::ckGetChannelType(ssh,i)
        If CkSsh::ckLastMethodSuccess(ssh) = 0
            Debug CkSsh::ckLastErrorText(ssh)
            CkSsh::ckDispose(ssh)
            ProcedureReturn
        EndIf

        Debug "Channel " + Str(chNum) + " is of type " + chType
    Next

    CkSsh::ckDisconnect(ssh)


    CkSsh::ckDispose(ssh)


    ProcedureReturn
EndProcedure