Sample code for 30+ languages & platforms
PureBasic

Wait for a Message on Any SSH Channel

See more SSH Examples

Demonstrates the Chilkat Ssh.WaitForChannelMessage method, which waits for the next SSH channel message on any channel. The only argument is the poll timeout: 0 for a nonblocking check, a positive value to wait that long, or a negative value to wait with no method-level timeout. It returns the channel number the message belongs to, -1 if nothing arrived before the timeout, or -2 on another error such as a lost connection.

Background: When several channels are active at once, waiting on one specific channel would block while others sit ready. This method is the multiplexed alternative — it returns as soon as any channel has activity and tells you which one, so a single loop can service them all. The explicit timeout argument governs the wait; shorter ReadTimeoutMs values do not cut it short.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkSsh.pb"

Procedure ChilkatExample()

    success.i = 0

    ;  Demonstrates the Ssh.WaitForChannelMessage method, which waits for the next SSH channel
    ;  message on ANY channel.  The only argument is the poll timeout in milliseconds: 0 for a
    ;  nonblocking check, a positive value to wait that long, or a negative value to wait with no
    ;  method-level timeout.

    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

    success = CkSsh::ckSendReqExec(ssh,channelNum,"ls -l /tmp")
    If success = 0
        Debug CkSsh::ckLastErrorText(ssh)
        CkSsh::ckDispose(ssh)
        ProcedureReturn
    EndIf

    ;  Wait up to 5 seconds for the next channel message.  The return value is the channel number
    ;  the message belongs to, -1 if nothing arrived before the timeout, or -2 on another error.
    pollTimeoutMs.i = 5000
    result.i = CkSsh::ckWaitForChannelMessage(ssh,pollTimeoutMs)

    If result = -2
        Debug CkSsh::ckLastErrorText(ssh)
        CkSsh::ckDispose(ssh)
        ProcedureReturn
    EndIf

    If result = -1
        Debug "No channel message arrived before the timeout."
    EndIf

    If result >= 0
        Debug "Processed a message for channel " + Str(result)
    EndIf

    CkSsh::ckDisconnect(ssh)


    CkSsh::ckDispose(ssh)


    ProcedureReturn
EndProcedure