Discuss this help topic in SecureBlackbox Forum

Execute a command and get its result in one call with TElSimpleSSHClient

In the previous article you've learned how to execute one command with TElSimpleSSHClient. You can easily extend your configuration to launch several commands withing the same session. TElSimpleSSHClient offers a simplified command execution functionality for scenarios where no user interaction is required for the command (e.g. 'ls'). ExecuteCommand() method performs all the necessary I/O operations for you, taking the command on input and returning its output as a result.

To execute a command in a 'simple' way, do the following:

  1. Configure your TElSimpleSSHClient component by adjusting algorithms, host and authentication parameters, and event handlers as needed, but do not call Open() (as the connection will be opened by ExecuteCommand itself):
  2. Call ExecuteCommand() method, passing your command to it:


client.Address = "";
client.Port = 22;
client.Username = "user";
client.Password = "pass";
// Call ExecuteCommand() method, passing your command to it:
byte[] output = client.ExecuteCommand("ls -l");

After ExecuteCommand() finishes, you will receive the output of the command in the 'output' variable. If any problem occurs during the command execution, ExecuteCommand() throws an exception. If the problem happens on the SSH protocol layer, OnError event is also invoked.

Note: there are several overloads of ExecuteCommand() available. You can use the one that suits your environment the most. The overloads offer the following functionality:

  • RedirectStdErr parameter tells the component whether it should direct all stderr channel output it receives to the main (stdout) output. By setting it to true you will have the command's stdout and stderr channels mixed up in the same variable.
  • Overloads taking by-reference StdErrData parameter allow you to catch the stderr output in a separate variable.
  • KeepConnectionOpen parameter tells the component not to close the SSH connection after ExecuteCommand() returns. This might be useful if you need to perform several simple commands in a row and don't want to establish a new connection for each command.

How To articles about SSH client

Discuss this help topic in SecureBlackbox Forum