PowerBuilder
PowerBuilder
Receive from an SSH Channel Until Any Pattern Matches
See more SSH Examples
Demonstrates the Chilkat Ssh.ChannelReceiveUntilMatchN method, which works like ChannelReceiveUntilMatch but returns when any pattern in a StringArray matches. The arguments are the channel number, the pattern array, the charset, and case sensitivity. The array must contain at least one element.
Important: Set the ReadTimeoutMs property before calling this method. It defaults to 0, meaning no limit, so without it the call waits forever if the received data never contains a match.
Background: A command often has more than one possible outcome, and your code should react to whichever occurs rather than hang waiting for one specific string. A common non-interactive pattern is to have the command print one marker on success and a different marker on failure, then match on the set. (With no PTY there is no shell prompt to match against, so markers you emit yourself are the dependable signal.) The first matching content in the stream wins; array order does not override an earlier token. Empty elements are ignored and duplicates are allowed.
Chilkat PowerBuilder Downloads
integer li_rc
integer li_Success
oleobject loo_Ssh
integer li_SshPort
string ls_Password
integer li_ChannelNum
oleobject loo_Patterns
integer li_CaseSensitive
string ls_Output
li_Success = 0
// Demonstrates the Ssh.ChannelReceiveUntilMatchN method, which receives text until ANY pattern
// in a StringArray is matched. The 1st argument is the channel number, the 2nd is the array of
// patterns, the 3rd is the charset, and the 4th selects case-sensitive matching.
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
// Start a shell WITHOUT requesting a PTY. Without a PTY the shell sees it is connected to a
// raw data pipe and enters non-interactive mode: it suppresses the command prompt, does not
// echo typed commands back, and skips interactive login scripts. The output is therefore
// clean and easily parseable -- but because no prompt is ever sent, code must not wait for
// one. Instead, have each command print a predictable marker, or wait for EOF / channel close.
li_Success = loo_Ssh.SendReqShell(li_ChannelNum)
if li_Success = 0 then
Write-Debug loo_Ssh.LastErrorText
destroy loo_Ssh
return
end if
// There is no prompt to match on. Instead, run a command that prints one marker on success
// and a different marker on failure, then wait for whichever appears first.
li_Success = loo_Ssh.ChannelSendString(li_ChannelNum,"test -f /etc/hosts && echo OK_MARKER || echo ERR_MARKER~n","utf-8")
if li_Success = 0 then
Write-Debug loo_Ssh.LastErrorText
destroy loo_Ssh
return
end if
loo_Patterns = create oleobject
li_rc = loo_Patterns.ConnectToNewObject("Chilkat.StringArray")
loo_Patterns.Append("OK_MARKER")
loo_Patterns.Append("ERR_MARKER")
// 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 = 0
li_Success = loo_Ssh.ChannelReceiveUntilMatchN(li_ChannelNum,loo_Patterns,"utf-8",li_CaseSensitive)
if li_Success = 0 then
Write-Debug loo_Ssh.LastErrorText
destroy loo_Ssh
destroy loo_Patterns
return
end if
ls_Output = loo_Ssh.GetReceivedText(li_ChannelNum,"utf-8")
if loo_Ssh.LastMethodSuccess = 0 then
Write-Debug loo_Ssh.LastErrorText
destroy loo_Ssh
destroy loo_Patterns
return
end if
Write-Debug ls_Output
loo_Ssh.Disconnect()
destroy loo_Ssh
destroy loo_Patterns