Sample code for 30+ languages & platforms
Visual FoxPro

SSH Quick/Simple Shell Session

See more SSH Examples

Demonstrates the simplified way to run multiple commands in a shell session using QuickShell, which combines OpenSessionChannel, SendReqPty, and SendReqShell into one call. The commands are built in a StringBuilder, sent together, and the output is read up to a marker echoed by the final command.

Background: Note that QuickShell does allocate a PTY, so the remote shell runs interactively: it prints a prompt and echoes back every command sent to it, and both the echo and the real output appear in what is received. That is what motivates the quoting trick on the final marker — writing echo THIS 'IS' THE END means the echoed command line contains the quotes while the command's actual output does not, so matching the unquoted text reliably matches the real output instead of the echo. For clean, prompt-free output, request a shell without a PTY instead.

Chilkat Visual FoxPro Downloads

Visual FoxPro
LOCAL lnSuccess
LOCAL loSsh
LOCAL lnPort
LOCAL lcPassword
LOCAL lnChannelNum
LOCAL loSbCommands
LOCAL lcCommands
LOCAL lnCaseSensitive
LOCAL lcSessionOutput

lnSuccess = 0

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

*  Demonstrates the simplified way to run multiple commands in a shell session using QuickShell.
*  
*  Note: QuickShell internally does OpenSessionChannel, SendReqPty, and SendReqShell, so it DOES
*  allocate a pseudo-terminal.  The remote shell therefore runs interactively: it prints a
*  command prompt and echoes back the commands that are sent to it.  Both the echoed input and
*  the real output appear in what is received.

loSsh = CreateObject('Chilkat.Ssh')

lnPort = 22
lnSuccess = loSsh.Connect("ssh.example.com",lnPort)
IF (lnSuccess = 0) THEN
    ? loSsh.LastErrorText
    RELEASE loSsh
    CANCEL
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.
lcPassword = "mySshPassword"

lnSuccess = loSsh.AuthenticatePw("mySshLogin",lcPassword)
IF (lnSuccess = 0) THEN
    ? loSsh.LastErrorText
    RELEASE loSsh
    CANCEL
ENDIF

*  Start the shell session.  A negative return value indicates failure.
lnChannelNum = loSsh.QuickShell()
IF (lnChannelNum < 0) THEN
    ? loSsh.LastErrorText
    RELEASE loSsh
    CANCEL
ENDIF

*  Build the commands, one per line.  Line endings matter: Unix/Linux servers typically expect
*  a bare LF, while Windows servers may require CRLF.
loSbCommands = CreateObject('Chilkat.StringBuilder')
loSbCommands.Append("echo hello world" + CHR(10))
loSbCommands.Append("date" + CHR(10))
loSbCommands.Append("df" + CHR(10))

*  The final command echoes a marker used to detect the end of the output.  The single quotes
*  around 'IS' are a trick: the terminal echo of the typed command includes the quotes, while
*  the command's actual output does not.  Matching the unquoted form therefore matches the real
*  output rather than the echo.
loSbCommands.Append("echo THIS 'IS' THE END OF THE SCRIPT" + CHR(10))

lcCommands = loSbCommands.GetAsString()
lnSuccess = loSsh.ChannelSendString(lnChannelNum,lcCommands,"utf-8")
IF (lnSuccess = 0) THEN
    ? loSsh.LastErrorText
    RELEASE loSsh
    RELEASE loSbCommands
    CANCEL
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.
loSsh.ReadTimeoutMs = 15000

lnCaseSensitive = 1
lnSuccess = loSsh.ChannelReceiveUntilMatch(lnChannelNum,"THIS IS THE END OF THE SCRIPT","utf-8",lnCaseSensitive)
IF (lnSuccess = 0) THEN
    ? loSsh.LastErrorText
    RELEASE loSsh
    RELEASE loSbCommands
    CANCEL
ENDIF

*  Indicate that no more commands will be sent, then close the channel.  Close only after the
*  desired output has been received.
lnSuccess = loSsh.ChannelSendEof(lnChannelNum)
IF (lnSuccess = 0) THEN
    ? loSsh.LastErrorText
    RELEASE loSsh
    RELEASE loSbCommands
    CANCEL
ENDIF

lnSuccess = loSsh.ChannelSendClose(lnChannelNum)
IF (lnSuccess = 0) THEN
    ? loSsh.LastErrorText
    RELEASE loSsh
    RELEASE loSbCommands
    CANCEL
ENDIF

*  Collect any remaining output.
lnSuccess = loSsh.ChannelReceiveToClose(lnChannelNum)
IF (lnSuccess = 0) THEN
    ? loSsh.LastErrorText
    RELEASE loSsh
    RELEASE loSbCommands
    CANCEL
ENDIF

lcSessionOutput = loSsh.GetReceivedText(lnChannelNum,"utf-8")
IF (loSsh.LastMethodSuccess = 0) THEN
    ? loSsh.LastErrorText
    RELEASE loSsh
    RELEASE loSbCommands
    CANCEL
ENDIF

? "--- output ----"
? lcSessionOutput

loSsh.Disconnect()

RELEASE loSsh
RELEASE loSbCommands