Sample code for 30+ languages & platforms
PureBasic

Open a Custom SSH Channel Type

See more SSH Examples

Demonstrates the Chilkat Ssh.OpenCustomChannel method, which requests a channel whose application-defined type is supplied as the only argument. It returns the channel number, or -1 if the server rejects the channel. The server must implement that channel type.

Background: The SSH protocol allows vendor- and application-specific channel types beyond the standard session and direct-tcpip kinds; by convention these carry an @domain suffix to avoid name collisions. This method is the escape hatch for talking to a server that implements such a type. On rejection, inspect ChannelOpenFailCode to learn why.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkSsh.pb"

Procedure ChilkatExample()

    success.i = 0

    ;  Demonstrates the Ssh.OpenCustomChannel method, which requests a custom SSH channel whose
    ;  application-defined type is supplied as the only argument.  It returns the channel number,
    ;  or -1 if the server rejects the channel.  The server must implement that channel type.

    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

    ;  Request an application-defined channel type.  The server must support it.
    channelType.s = "my-custom-channel@example.com"
    channelNum.i = CkSsh::ckOpenCustomChannel(ssh,channelType)
    If channelNum < 0
        Debug CkSsh::ckLastErrorText(ssh)
        CkSsh::ckDispose(ssh)
        ProcedureReturn
    EndIf

    Debug "Opened custom channel: " + Str(channelNum)

    ;  The channel is used with the normal channel send, receive, EOF, CLOSE, and release methods.

    CkSsh::ckDisconnect(ssh)


    CkSsh::ckDispose(ssh)


    ProcedureReturn
EndProcedure