Sample code for 30+ languages & platforms
PowerBuilder

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 PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Ssh
integer li_SshPort
string ls_Password
integer li_ChannelNum
integer li_PollTimeoutMs
integer li_Result

li_Success = 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.

loo_Ssh = create oleobject
li_rc = loo_Ssh.ConnectToNewObject("Chilkat.Ssh")
if li_rc < 0 then
    destroy loo_Ssh
    MessageBox("Error","Connecting to COM object failed")
    return
end if

li_SshPort = 22
li_Success = loo_Ssh.Connect("ssh.example.com",li_SshPort)
if li_Success = 0 then
    Write-Debug loo_Ssh.LastErrorText
    destroy loo_Ssh
    return
end if

//  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.
ls_Password = "mySshPassword"

li_Success = loo_Ssh.AuthenticatePw("mySshLogin",ls_Password)
if li_Success = 0 then
    Write-Debug loo_Ssh.LastErrorText
    destroy loo_Ssh
    return
end if

li_ChannelNum = loo_Ssh.OpenSessionChannel()
if li_ChannelNum < 0 then
    Write-Debug loo_Ssh.LastErrorText
    destroy loo_Ssh
    return
end if

li_Success = loo_Ssh.SendReqExec(li_ChannelNum,"ls -l /tmp")
if li_Success = 0 then
    Write-Debug loo_Ssh.LastErrorText
    destroy loo_Ssh
    return
end if

//  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.
li_PollTimeoutMs = 5000
li_Result = loo_Ssh.WaitForChannelMessage(li_PollTimeoutMs)

if li_Result = -2 then
    Write-Debug loo_Ssh.LastErrorText
    destroy loo_Ssh
    return
end if

if li_Result = -1 then
    Write-Debug "No channel message arrived before the timeout."
end if

if li_Result >= 0 then
    Write-Debug "Processed a message for channel " + string(li_Result)
end if

loo_Ssh.Disconnect()


destroy loo_Ssh