Xbase++
Xbase++
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 Xbase++ Downloads
LOCAL nSuccess
LOCAL oSsh
LOCAL oSaPrompts
LOCAL cHostname
LOCAL nPort
LOCAL cPassword
LOCAL nChannelNum
LOCAL cTermType
LOCAL nWidthInChars
LOCAL nHeightInChars
LOCAL nPixWidth
LOCAL nPixHeight
LOCAL nCaseSensitive
LOCAL cCmdOutput
nSuccess := 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.
oSsh := CreateObject("Chilkat.Ssh")
// The remote shell prompt may be any of these, depending on the device.
oSaPrompts := CreateObject("Chilkat.StringArray")
oSaPrompts:Append("~$")
oSaPrompts:Append("mars#")
oSaPrompts:Append("jupiter%")
oSaPrompts:Append("chilkat$")
oSaPrompts:Append("admin>")
cHostname := "ssh.example.com"
nPort := 22
nSuccess := oSsh:Connect(cHostname, nPort)
IF (nSuccess == 0)
? oSsh:LastErrorText
oSsh:destroy()
oSaPrompts:destroy()
RETURN
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.
cPassword := "mySshPassword"
nSuccess := oSsh:AuthenticatePw("mySshLogin", cPassword)
IF (nSuccess == 0)
? oSsh:LastErrorText
oSsh:destroy()
oSaPrompts:destroy()
RETURN
ENDIF
nChannelNum := oSsh:OpenSessionChannel()
IF (nChannelNum < 0)
? oSsh:LastErrorText
oSsh:destroy()
oSaPrompts:destroy()
RETURN
ENDIF
// 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.
cTermType := "dumb"
nWidthInChars := 120
nHeightInChars := 40
nPixWidth := 0
nPixHeight := 0
nSuccess := oSsh:SendReqPty(nChannelNum, cTermType, nWidthInChars, nHeightInChars, nPixWidth, nPixHeight)
IF (nSuccess == 0)
? oSsh:LastErrorText
oSsh:destroy()
oSaPrompts:destroy()
RETURN
ENDIF
nSuccess := oSsh:SendReqShell(nChannelNum)
IF (nSuccess == 0)
? oSsh:LastErrorText
oSsh:destroy()
oSaPrompts:destroy()
RETURN
ENDIF
// 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.
oSsh:ReadTimeoutMs := 15000
nCaseSensitive := 1
// Read the initial login text up to the first prompt.
nSuccess := oSsh:ChannelReceiveUntilMatchN(nChannelNum, oSaPrompts, "utf-8", nCaseSensitive)
IF (nSuccess == 0)
? oSsh:LastErrorText
oSsh:destroy()
oSaPrompts:destroy()
RETURN
ENDIF
cCmdOutput := oSsh:GetReceivedText(nChannelNum, "utf-8")
IF (oSsh:LastMethodSuccess == 0)
? oSsh:LastErrorText
oSsh:destroy()
oSaPrompts:destroy()
RETURN
ENDIF
? cCmdOutput
// ---- 1st command ----
nSuccess := oSsh:ChannelSendString(nChannelNum, "cd /tmp" + Chr(10), "utf-8")
IF (nSuccess == 0)
? oSsh:LastErrorText
oSsh:destroy()
oSaPrompts:destroy()
RETURN
ENDIF
nSuccess := oSsh:ChannelReceiveUntilMatchN(nChannelNum, oSaPrompts, "utf-8", nCaseSensitive)
IF (nSuccess == 0)
? oSsh:LastErrorText
oSsh:destroy()
oSaPrompts:destroy()
RETURN
ENDIF
// 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.
cCmdOutput := oSsh:GetReceivedText(nChannelNum, "utf-8")
IF (oSsh:LastMethodSuccess == 0)
? oSsh:LastErrorText
oSsh:destroy()
oSaPrompts:destroy()
RETURN
ENDIF
? cCmdOutput
// ---- 2nd command ----
nSuccess := oSsh:ChannelSendString(nChannelNum, "ls" + Chr(10), "utf-8")
IF (nSuccess == 0)
? oSsh:LastErrorText
oSsh:destroy()
oSaPrompts:destroy()
RETURN
ENDIF
nSuccess := oSsh:ChannelReceiveUntilMatchN(nChannelNum, oSaPrompts, "utf-8", nCaseSensitive)
IF (nSuccess == 0)
? oSsh:LastErrorText
oSsh:destroy()
oSaPrompts:destroy()
RETURN
ENDIF
cCmdOutput := oSsh:GetReceivedText(nChannelNum, "utf-8")
IF (oSsh:LastMethodSuccess == 0)
? oSsh:LastErrorText
oSsh:destroy()
oSaPrompts:destroy()
RETURN
ENDIF
? cCmdOutput
// Finish the session.
nSuccess := oSsh:ChannelSendEof(nChannelNum)
IF (nSuccess == 0)
? oSsh:LastErrorText
oSsh:destroy()
oSaPrompts:destroy()
RETURN
ENDIF
nSuccess := oSsh:ChannelReceiveToClose(nChannelNum)
IF (nSuccess == 0)
? oSsh:LastErrorText
oSsh:destroy()
oSaPrompts:destroy()
RETURN
ENDIF
cCmdOutput := oSsh:GetReceivedText(nChannelNum, "utf-8")
IF (oSsh:LastMethodSuccess == 0)
? oSsh:LastErrorText
oSsh:destroy()
oSaPrompts:destroy()
RETURN
ENDIF
? cCmdOutput
oSsh:Disconnect()
oSsh:destroy()
oSaPrompts:destroy()