Discuss this help topic in SecureBlackbox Forum

Obtain the return code after executing a command with TElSimpleSSHClient

The OS and third-party console commands often return integer result codes to the shell, which allow to identify the general outcome of command execution (e.g. success / failure). In particular, return codes are a good mechanism for implementing conditional execution in batch files.

SSH provides means for obtaining the return code when executing a command over the SSH channel. You can read the code from the ExitStatus property of the TElSimpleSSHClient component.

C#:


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");

The return code should be checked after execution of the command has been completed - i.e. after calling the Close() method (in 'normal' mode of operation), or after ExecuteCommand() method returns (in 'simplified' mode). Note that there is no sense in reading ExitStatus if the command execution was not completed correctly, and was terminated as a result of a protocol or network error.

In addition to exit status, some operating systems may return information about command closure reasons if it was terminated erroneously. Some Unix derivatives provide exit signal (a OS signal which caused the program to terminate, e.g. SIGSEGV) and exit message (e.g. 'Segmentation fault'). These values, if provided, can be read from ExitSignal and ExitMessage properties.

How To articles about SSH client

Discuss this help topic in SecureBlackbox Forum