Sample code for 30+ languages & platforms
PureBasic

Start a Remote Shell with QuickShell

See more SSH Examples

Demonstrates the Chilkat Ssh.QuickShell method, which starts a remote shell using the simplified sequence OpenSessionChannelSendReqPtySendReqShell. It takes no arguments and returns the shell channel number, or -1 on failure. The default PTY size is 80 columns by 24 rows.

Background: Note that QuickShell does allocate a PTY, so unlike a bare SendReqShell the remote shell runs interactively: it prints a command prompt, echoes the commands you send, and runs interactive login scripts. That is convenient for terminal-style work but means the output contains prompts and echoed input that scripted parsing must tolerate. The method returns as soon as the shell request is accepted — it does not consume the initial login text or prompt, so read and discard that before relying on command output.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkSsh.pb"

Procedure ChilkatExample()

    success.i = 0

    ;  Demonstrates the Ssh.QuickShell method, which starts a remote shell using the simplified
    ;  sequence OpenSessionChannel, SendReqPty, and SendReqShell.  It takes no arguments and returns
    ;  the shell channel number, or -1 on failure.  The default PTY size is 80 columns by 24 rows.

    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

    ;  Start the shell.  Note that QuickShell allocates a PTY, so the remote shell runs
    ;  interactively: it prints a command prompt and echoes the commands that are sent to it.
    channelNum.i = CkSsh::ckQuickShell(ssh)
    If channelNum < 0
        Debug CkSsh::ckLastErrorText(ssh)
        CkSsh::ckDispose(ssh)
        ProcedureReturn
    EndIf

    ;  QuickShell returns as soon as the shell request is accepted -- it does not wait for or
    ;  consume the initial login text or prompt.
    success = CkSsh::ckChannelSendString(ssh,channelNum,"uname -a; echo DONE_MARKER" + Chr(10),"utf-8")
    If success = 0
        Debug CkSsh::ckLastErrorText(ssh)
        CkSsh::ckDispose(ssh)
        ProcedureReturn
    EndIf

    ;  IMPORTANT: Set a read timeout before receiving until a match.  ReadTimeoutMs defaults to 0,
    ;  which means no limit -- without it, this call waits forever if the received data never
    ;  contains a match.
    CkSsh::setCkReadTimeoutMs(ssh, 15000)

    caseSensitive.i = 0
    success = CkSsh::ckChannelReceiveUntilMatch(ssh,channelNum,"DONE_MARKER","utf-8",caseSensitive)
    If success = 0
        Debug CkSsh::ckLastErrorText(ssh)
        CkSsh::ckDispose(ssh)
        ProcedureReturn
    EndIf

    output.s = CkSsh::ckGetReceivedText(ssh,channelNum,"utf-8")
    If CkSsh::ckLastMethodSuccess(ssh) = 0
        Debug CkSsh::ckLastErrorText(ssh)
        CkSsh::ckDispose(ssh)
        ProcedureReturn
    EndIf

    Debug output

    CkSsh::ckDisconnect(ssh)


    CkSsh::ckDispose(ssh)


    ProcedureReturn
EndProcedure