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:
client.Address = "192.168.5.5"; 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: