PowerBuilder
PowerBuilder
SSH Execute Remote Commands
See more SSH Examples
Demonstrates running several commands on an SSH server using QuickCommand, which performs the entire open-channel, exec, receive, and retrieve sequence in a single call and returns the command's stdout.
Background:
QuickCommand is the simplest way to run one-off commands: each call uses its own session channel on the same authenticated connection, so several commands can be issued in sequence without managing channels yourself. Because no PTY is involved the output is clean, with no prompt or echoed input to strip. Reach for the individual channel methods only when you need stderr separately, the exit status, or a long-lived interactive session.Chilkat PowerBuilder Downloads
integer li_rc
integer li_Success
oleobject loo_Ssh
integer li_Port
string ls_Password
string ls_StrOutput
li_Success = 0
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// Demonstrates running several commands on an SSH server using QuickCommand, which handles the
// whole open-channel, exec, receive, and retrieve sequence in a single call.
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
// Each QuickCommand call runs one command on its own session channel and returns its stdout.
ls_StrOutput = loo_Ssh.QuickCommand("df","utf-8")
if loo_Ssh.LastMethodSuccess = 0 then
Write-Debug loo_Ssh.LastErrorText
destroy loo_Ssh
return
end if
Write-Debug "---- df ----"
Write-Debug ls_StrOutput
ls_StrOutput = loo_Ssh.QuickCommand("echo hello world","utf-8")
if loo_Ssh.LastMethodSuccess = 0 then
Write-Debug loo_Ssh.LastErrorText
destroy loo_Ssh
return
end if
Write-Debug "---- echo hello world ----"
Write-Debug ls_StrOutput
ls_StrOutput = loo_Ssh.QuickCommand("date","utf-8")
if loo_Ssh.LastMethodSuccess = 0 then
Write-Debug loo_Ssh.LastErrorText
destroy loo_Ssh
return
end if
Write-Debug "---- date ----"
Write-Debug ls_StrOutput
loo_Ssh.Disconnect()
destroy loo_Ssh