Sample code for 30+ languages & platforms
Xbase++

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 Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oSsh
LOCAL nSshPort
LOCAL cPassword
LOCAL nChannelNum
LOCAL n
LOCAL i
LOCAL nChNum
LOCAL cChType

nSuccess := 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".

oSsh := CreateObject("Chilkat.Ssh")

nSshPort := 22
nSuccess := oSsh:Connect("ssh.example.com", nSshPort)
IF (nSuccess == 0)
    ? oSsh:LastErrorText
    oSsh:destroy()
    RETURN
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.
cPassword := "mySshPassword"

nSuccess := oSsh:AuthenticatePw("mySshLogin", cPassword)
IF (nSuccess == 0)
    ? oSsh:LastErrorText
    oSsh:destroy()
    RETURN
ENDIF

nChannelNum := oSsh:OpenSessionChannel()
IF (nChannelNum < 0)
    ? oSsh:LastErrorText
    oSsh:destroy()
    RETURN
ENDIF

//  Enumerate the active channels and report each one's number and type.
n := oSsh:NumOpenChannels

FOR i := 0 TO n - 1
    nChNum := oSsh:GetChannelNumber(i)
    cChType := oSsh:GetChannelType(i)
    IF (oSsh:LastMethodSuccess == 0)
        ? oSsh:LastErrorText
        oSsh:destroy()
        RETURN
    ENDIF

    ? "Channel " + Str(nChNum) + " is of type " + cChType
NEXT

oSsh:Disconnect()

oSsh:destroy()