PowerBuilder
PowerBuilder
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 PowerBuilder Downloads
integer li_rc
integer li_Success
oleobject loo_Ssh
integer li_Port
string ls_Password
integer li_ChannelNum
oleobject loo_SbCommands
string ls_Commands
integer li_CaseSensitive
string ls_SessionOutput
li_Success = 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.
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_Port = 22
li_Success = loo_Ssh.Connect("ssh.example.com",li_Port)
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
// Start the shell session. A negative return value indicates failure.
li_ChannelNum = loo_Ssh.QuickShell()
if li_ChannelNum < 0 then
Write-Debug loo_Ssh.LastErrorText
destroy loo_Ssh
return
end if
// Build the commands, one per line. Line endings matter: Unix/Linux servers typically expect
// a bare LF, while Windows servers may require CRLF.
loo_SbCommands = create oleobject
li_rc = loo_SbCommands.ConnectToNewObject("Chilkat.StringBuilder")
loo_SbCommands.Append("echo hello world~n")
loo_SbCommands.Append("date~n")
loo_SbCommands.Append("df~n")
// 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.
loo_SbCommands.Append("echo THIS 'IS' THE END OF THE SCRIPT~n")
ls_Commands = loo_SbCommands.GetAsString()
li_Success = loo_Ssh.ChannelSendString(li_ChannelNum,ls_Commands,"utf-8")
if li_Success = 0 then
Write-Debug loo_Ssh.LastErrorText
destroy loo_Ssh
destroy loo_SbCommands
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
li_Success = loo_Ssh.ChannelReceiveUntilMatch(li_ChannelNum,"THIS IS THE END OF THE SCRIPT","utf-8",li_CaseSensitive)
if li_Success = 0 then
Write-Debug loo_Ssh.LastErrorText
destroy loo_Ssh
destroy loo_SbCommands
return
end if
// Indicate that no more commands will be sent, then close the channel. Close only after the
// desired output has been received.
li_Success = loo_Ssh.ChannelSendEof(li_ChannelNum)
if li_Success = 0 then
Write-Debug loo_Ssh.LastErrorText
destroy loo_Ssh
destroy loo_SbCommands
return
end if
li_Success = loo_Ssh.ChannelSendClose(li_ChannelNum)
if li_Success = 0 then
Write-Debug loo_Ssh.LastErrorText
destroy loo_Ssh
destroy loo_SbCommands
return
end if
// Collect any remaining output.
li_Success = loo_Ssh.ChannelReceiveToClose(li_ChannelNum)
if li_Success = 0 then
Write-Debug loo_Ssh.LastErrorText
destroy loo_Ssh
destroy loo_SbCommands
return
end if
ls_SessionOutput = loo_Ssh.GetReceivedText(li_ChannelNum,"utf-8")
if loo_Ssh.LastMethodSuccess = 0 then
Write-Debug loo_Ssh.LastErrorText
destroy loo_Ssh
destroy loo_SbCommands
return
end if
Write-Debug "--- output ----"
Write-Debug ls_SessionOutput
loo_Ssh.Disconnect()
destroy loo_Ssh
destroy loo_SbCommands