Sample code for 30+ languages & platforms
PowerBuilder

SSH ChannelReceiveUntilMatchN (Match Any of Several Prompts)

See more SSH Examples

Demonstrates ChannelReceiveUntilMatchN, which receives until any one of several patterns is matched. This is useful when driving an interactive session where the shell prompt differs from device to device.

Note: This example deliberately requests a PTY. Matching a shell prompt only works in an interactive session — without a PTY the shell runs non-interactively and never prints a prompt at all. For scripted use, omit the PTY and match on a marker your own command echoes.

Background: Real sessions have more than one possible next output, so waiting for one specific string risks hanging when something else appears. Matching a set lets the code proceed on whichever actually occurs — a user prompt, a root prompt, or a device-specific one. A "dumb" terminal type is requested so the remote side does not mix ANSI escape sequences into the output, which would otherwise complicate matching. Remember that retrieving text clears the receive buffer; leaving a prompt buffered would make the next receive match immediately and return the wrong output.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Ssh
oleobject loo_SaPrompts
string ls_Hostname
integer li_Port
string ls_Password
integer li_ChannelNum
string ls_TermType
integer li_WidthInChars
integer li_HeightInChars
integer li_PixWidth
integer li_PixHeight
integer li_CaseSensitive
string ls_CmdOutput

li_Success = 0

//  This example requires the Chilkat API to have been previously unlocked.
//  See Global Unlock Sample for sample code.

//  Demonstrates ChannelReceiveUntilMatchN, which receives until ANY one of several patterns is
//  matched.  This is useful when driving an interactive session where the shell prompt varies
//  from device to device.
//  
//  Note: This example deliberately DOES request a PTY.  Matching a shell prompt only works in an
//  interactive session -- without a PTY the shell runs non-interactively and never prints a
//  prompt at all.  For scripted use where no prompt is needed, omit the PTY and match on a
//  marker your own command echoes instead.

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

//  The remote shell prompt may be any of these, depending on the device.
loo_SaPrompts = create oleobject
li_rc = loo_SaPrompts.ConnectToNewObject("Chilkat.StringArray")

loo_SaPrompts.Append("~~$")
loo_SaPrompts.Append("mars#")
loo_SaPrompts.Append("jupiter%")
loo_SaPrompts.Append("chilkat$")
loo_SaPrompts.Append("admin>")

ls_Hostname = "ssh.example.com"
li_Port = 22
li_Success = loo_Ssh.Connect(ls_Hostname,li_Port)
if li_Success = 0 then
    Write-Debug loo_Ssh.LastErrorText
    destroy loo_Ssh
    destroy loo_SaPrompts
    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
    destroy loo_SaPrompts
    return
end if

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

//  Request a pseudo-terminal so the shell runs interactively and prints prompts.  A "dumb"
//  terminal is used so that ANSI escape sequences are not mixed into the output.
ls_TermType = "dumb"
li_WidthInChars = 120
li_HeightInChars = 40
li_PixWidth = 0
li_PixHeight = 0
li_Success = loo_Ssh.SendReqPty(li_ChannelNum,ls_TermType,li_WidthInChars,li_HeightInChars,li_PixWidth,li_PixHeight)
if li_Success = 0 then
    Write-Debug loo_Ssh.LastErrorText
    destroy loo_Ssh
    destroy loo_SaPrompts
    return
end if

li_Success = loo_Ssh.SendReqShell(li_ChannelNum)
if li_Success = 0 then
    Write-Debug loo_Ssh.LastErrorText
    destroy loo_Ssh
    destroy loo_SaPrompts
    return
end if

//  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.
loo_Ssh.ReadTimeoutMs = 15000

li_CaseSensitive = 1

//  Read the initial login text up to the first prompt.
li_Success = loo_Ssh.ChannelReceiveUntilMatchN(li_ChannelNum,loo_SaPrompts,"utf-8",li_CaseSensitive)
if li_Success = 0 then
    Write-Debug loo_Ssh.LastErrorText
    destroy loo_Ssh
    destroy loo_SaPrompts
    return
end if

ls_CmdOutput = loo_Ssh.GetReceivedText(li_ChannelNum,"utf-8")
if loo_Ssh.LastMethodSuccess = 0 then
    Write-Debug loo_Ssh.LastErrorText
    destroy loo_Ssh
    destroy loo_SaPrompts
    return
end if

Write-Debug ls_CmdOutput

//  ---- 1st command ----
li_Success = loo_Ssh.ChannelSendString(li_ChannelNum,"cd /tmp~n","utf-8")
if li_Success = 0 then
    Write-Debug loo_Ssh.LastErrorText
    destroy loo_Ssh
    destroy loo_SaPrompts
    return
end if

li_Success = loo_Ssh.ChannelReceiveUntilMatchN(li_ChannelNum,loo_SaPrompts,"utf-8",li_CaseSensitive)
if li_Success = 0 then
    Write-Debug loo_Ssh.LastErrorText
    destroy loo_Ssh
    destroy loo_SaPrompts
    return
end if

//  Retrieving the text also clears the receive buffer, which matters: if a prompt were left
//  buffered, the next receive would match it immediately and return the wrong output.
ls_CmdOutput = loo_Ssh.GetReceivedText(li_ChannelNum,"utf-8")
if loo_Ssh.LastMethodSuccess = 0 then
    Write-Debug loo_Ssh.LastErrorText
    destroy loo_Ssh
    destroy loo_SaPrompts
    return
end if

Write-Debug ls_CmdOutput

//  ---- 2nd command ----
li_Success = loo_Ssh.ChannelSendString(li_ChannelNum,"ls~n","utf-8")
if li_Success = 0 then
    Write-Debug loo_Ssh.LastErrorText
    destroy loo_Ssh
    destroy loo_SaPrompts
    return
end if

li_Success = loo_Ssh.ChannelReceiveUntilMatchN(li_ChannelNum,loo_SaPrompts,"utf-8",li_CaseSensitive)
if li_Success = 0 then
    Write-Debug loo_Ssh.LastErrorText
    destroy loo_Ssh
    destroy loo_SaPrompts
    return
end if

ls_CmdOutput = loo_Ssh.GetReceivedText(li_ChannelNum,"utf-8")
if loo_Ssh.LastMethodSuccess = 0 then
    Write-Debug loo_Ssh.LastErrorText
    destroy loo_Ssh
    destroy loo_SaPrompts
    return
end if

Write-Debug ls_CmdOutput

//  Finish the session.
li_Success = loo_Ssh.ChannelSendEof(li_ChannelNum)
if li_Success = 0 then
    Write-Debug loo_Ssh.LastErrorText
    destroy loo_Ssh
    destroy loo_SaPrompts
    return
end if

li_Success = loo_Ssh.ChannelReceiveToClose(li_ChannelNum)
if li_Success = 0 then
    Write-Debug loo_Ssh.LastErrorText
    destroy loo_Ssh
    destroy loo_SaPrompts
    return
end if

ls_CmdOutput = loo_Ssh.GetReceivedText(li_ChannelNum,"utf-8")
if loo_Ssh.LastMethodSuccess = 0 then
    Write-Debug loo_Ssh.LastErrorText
    destroy loo_Ssh
    destroy loo_SaPrompts
    return
end if

Write-Debug ls_CmdOutput

loo_Ssh.Disconnect()


destroy loo_Ssh
destroy loo_SaPrompts