DataFlex
DataFlex
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 DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoSsh
Integer iSshPort
String sPassword
Integer iChannelNum
Variant vPatterns
Handle hoPatterns
Boolean iCaseSensitive
String sOutput
String sTemp1
Boolean bTemp1
Move False To iSuccess
// 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.
Get Create (RefClass(cComChilkatSsh)) To hoSsh
If (Not(IsComObjectCreated(hoSsh))) Begin
Send CreateComObject of hoSsh
End
Move 22 To iSshPort
Get ComConnect Of hoSsh "ssh.example.com" iSshPort To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoSsh To sTemp1
Showln sTemp1
Procedure_Return
End
// 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.
Move "mySshPassword" To sPassword
Get ComAuthenticatePw Of hoSsh "mySshLogin" sPassword To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoSsh To sTemp1
Showln sTemp1
Procedure_Return
End
Get ComOpenSessionChannel Of hoSsh To iChannelNum
If (iChannelNum < 0) Begin
Get ComLastErrorText Of hoSsh To sTemp1
Showln sTemp1
Procedure_Return
End
// 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.
Get ComSendReqShell Of hoSsh iChannelNum To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoSsh To sTemp1
Showln sTemp1
Procedure_Return
End
// 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.
Get ComChannelSendString Of hoSsh iChannelNum "test -f /etc/hosts && echo OK_MARKER || echo ERR_MARKER" + (character(10)) "utf-8" To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoSsh To sTemp1
Showln sTemp1
Procedure_Return
End
Get Create (RefClass(cComCkStringArray)) To hoPatterns
If (Not(IsComObjectCreated(hoPatterns))) Begin
Send CreateComObject of hoPatterns
End
Get ComAppend Of hoPatterns "OK_MARKER" To iSuccess
Get ComAppend Of hoPatterns "ERR_MARKER" To iSuccess
// 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.
Set ComReadTimeoutMs Of hoSsh To 15000
Move False To iCaseSensitive
Get pvComObject of hoPatterns to vPatterns
Get ComChannelReceiveUntilMatchN Of hoSsh iChannelNum vPatterns "utf-8" iCaseSensitive To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoSsh To sTemp1
Showln sTemp1
Procedure_Return
End
Get ComGetReceivedText Of hoSsh iChannelNum "utf-8" To sOutput
Get ComLastMethodSuccess Of hoSsh To bTemp1
If (bTemp1 = False) Begin
Get ComLastErrorText Of hoSsh To sTemp1
Showln sTemp1
Procedure_Return
End
Showln sOutput
Send ComDisconnect To hoSsh
End_Procedure