DataFlex
DataFlex
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 DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoSsh
Integer iPort
String sPassword
String sStrOutput
String sTemp1
Boolean bTemp1
Move False To iSuccess
// 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.
Get Create (RefClass(cComChilkatSsh)) To hoSsh
If (Not(IsComObjectCreated(hoSsh))) Begin
Send CreateComObject of hoSsh
End
Move 22 To iPort
Get ComConnect Of hoSsh "ssh.example.com" iPort 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
// Each QuickCommand call runs one command on its own session channel and returns its stdout.
Get ComQuickCommand Of hoSsh "df" "utf-8" To sStrOutput
Get ComLastMethodSuccess Of hoSsh To bTemp1
If (bTemp1 = False) Begin
Get ComLastErrorText Of hoSsh To sTemp1
Showln sTemp1
Procedure_Return
End
Showln "---- df ----"
Showln sStrOutput
Get ComQuickCommand Of hoSsh "echo hello world" "utf-8" To sStrOutput
Get ComLastMethodSuccess Of hoSsh To bTemp1
If (bTemp1 = False) Begin
Get ComLastErrorText Of hoSsh To sTemp1
Showln sTemp1
Procedure_Return
End
Showln "---- echo hello world ----"
Showln sStrOutput
Get ComQuickCommand Of hoSsh "date" "utf-8" To sStrOutput
Get ComLastMethodSuccess Of hoSsh To bTemp1
If (bTemp1 = False) Begin
Get ComLastErrorText Of hoSsh To sTemp1
Showln sTemp1
Procedure_Return
End
Showln "---- date ----"
Showln sStrOutput
Send ComDisconnect To hoSsh
End_Procedure