Sample code for 30+ languages & platforms
PureBasic

Request an SSH Subsystem

See more SSH Examples

Demonstrates the Chilkat Ssh.SendReqSubsystem method, which requests a predefined subsystem on a session channel. The first argument is the channel number and the second is the subsystem name. This example requests the sftp subsystem.

Background: A subsystem is a named service the server exposes over a channel instead of a shell command — sftp is the familiar example, and netconf is common on network devices. After acceptance, the channel carries that protocol's data, exchanged with the ordinary channel send and receive methods. If the server rejects the subsystem, the channel normally stays open and can be reused for another request.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkSsh.pb"

Procedure ChilkatExample()

    success.i = 0

    ;  Demonstrates the Ssh.SendReqSubsystem method, which requests a predefined subsystem on a
    ;  session channel.  The 1st argument is the channel number and the 2nd is the subsystem name.

    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

    ;  Open a session channel.  A negative return value indicates failure.
    channelNum.i = CkSsh::ckOpenSessionChannel(ssh)
    If channelNum < 0
        Debug CkSsh::ckLastErrorText(ssh)
        CkSsh::ckDispose(ssh)
        ProcedureReturn
    EndIf

    ;  Request the "sftp" subsystem.  A 1 return means the server accepted the request;
    ;  subsequent protocol data is exchanged with the normal channel send and receive methods.
    success = CkSsh::ckSendReqSubsystem(ssh,channelNum,"sftp")
    If success = 0
        Debug CkSsh::ckLastErrorText(ssh)
        CkSsh::ckDispose(ssh)
        ProcedureReturn
    EndIf

    Debug "Subsystem request accepted."

    CkSsh::ckDisconnect(ssh)


    CkSsh::ckDispose(ssh)


    ProcedureReturn
EndProcedure